Summary of methods to improve mysql count

Summary of methods to improve mysql count

I believe many programmers are familiar with MySQL. Many people are struggling with the usage of count and how to get the best query results. Let’s talk about some of my opinions today, for reference only.

1. Let's first build a table and prepare test data to facilitate subsequent testing steps

Take the InnoDB engine table as an example

The table creation statement is as follows

CREATE TABLE test.test

(

    a VARCHAR(50) NOT NULL COMMENT 'ddfdf',

    b VARCHAR(15) NOT NULL COMMENT 'fds',

    c VARCHAR(20) NOT NULL COMMENT 'asda',

    d VARCHAR(8) NOT NULL COMMENT 'ads',

    e longblob NOT NULL COMMENT 'asda',

    f VARCHAR(2000) COMMENT 'ads',

    g VARCHAR(8) NOT NULL COMMENT 'assd',

    h DATE NOT NULL COMMENT 'adsad',

    z VARCHAR(10) NOT NULL COMMENT 'adsd'

)

ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Log in to MySQL and change the database

Execute the table creation statement as shown below

3. Then prepare the test data and simply check whether there is data, as shown in the figure below

4. Next, start testing

In the absence of a where condition

Some people think count(*) is faster than count(field), while others think count(field) is faster than count(*)?

So which one is faster? Let's try it and see. Please see the picture below.

According to the results in the figure, count(field) is obviously faster.

5. What about the case where there is a where condition ? Which is faster, count(*) or count(field)?

Please see the execution effect in the figure below

count(*) is faster, but the data size may be too small to see obvious results.

6. Of course, you can analyze it by viewing the execution plan

Just add desc or explain before the executed SQL, as shown in the following figure

Finally, let's summarize 1. If there is no where condition, it is recommended to count(field) 2. If there is a where condition, it is recommended to count(*) Finally, let's summarize 1. If there is no where condition, it is recommended to count(field) 2. If there is a where condition, it is recommended to count(*)

You may also be interested in:
  • Optimized implementation of count() for large MySQL tables
  • Usage and performance optimization techniques of aggregate function count in MySQL
  • Sharing on count optimization issues in innodb in mysql
  • Let's talk about the performance of MySQL's COUNT(*)
  • Detailed explanation of the correct use of the count function in MySQL
  • A brief discussion on MySQL count of rows
  • Detailed explanation of count without filter conditions in MySQL
  • Summary of the differences between count(*), count(1) and count(col) in MySQL
  • Detailed explanation of mySQL count data examples in multiple tables
  • Use and optimization of MySQL COUNT function

<<:  WeChat Mini Program to Implement Electronic Signature

>>:  Docker Compose network settings explained

Recommend

Vue implements real-time refresh of the time display in the upper right corner

This article example shares the specific code of ...

CSS sample code with search navigation bar

This article shows you how to use CSS to create a...

What is this in JavaScript point by point series

Understand this Perhaps you have seen this in oth...

Vue implements a simple marquee effect

This article shares the specific code of Vue to a...

Detailed explanation of generic cases in TypeScript

Definition of Generics // Requirement 1: Generics...

How to quickly import data into MySQL

Preface: In daily study and work, we often encoun...

Make your website run fast

Does performance really matter? Performance is im...

Several mistakes that JavaScript beginners often make

Table of contents Preface Confusing undefined and...

Three ways to jump to a page by clicking a button tag in HTML

Method 1: Using the onclick event <input type=...

Getting Started Guide to MySQL Sharding

Preface Relational databases are more likely to b...

Introduction to Docker containers

Docker Overview Docker is an open source software...

Implementing form submission without refreshing the page based on HTML

Using ajax to implement form submission without re...