MySQL's COUNT statement --count (*), count (constant), count (column name)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lihuarongaini/article/details/102658504

MySQL's COUNT statement -count (*), count (constant), count (column name)

Database Query I believe many people are not familiar with, people often laugh at all the programmer is CRUD Commissioner, this so-called CRUD refers to additions and deletions to change search database.

In additions and deletions to change search the database operation, is the most frequently used queries. In all query operations, the number of statistical operations is often used.

Statistics on the number of rows in the database, either MySQL or Oracle, has a function that can be used, that is, COUNT

But is this common COUNT function, but hidden a lot of mystery, especially during the interview, believe it will be abused. Do not believe it, try to answer the following questions below:

1, COUNT there are several uses?

2, COUNT (field names) and COUNT (*) query results any different?

3. What is the difference between (*) COUNT (1) and COUNT?

4, between efficiency (*) COUNT (1) and COUNT which higher?

5. Why "Ali Baba Java Development Manual" recommends using COUNT (*)

6, MySQL's MyISAM engine for the COUNT (*) optimized to do what?

7, MySQL's InnoDB engine for COUNT (*) optimized to do what?

8, MySQL on the above-mentioned COUNT (*) is optimized to do, what is a key premise is that?

9, SELECT COUNT (*) when, plus without conditions where there is difference?

10, (1) and COUNT (field names) in the execution COUNT (*), COUNT What?

More than 10 questions, if you can answer all accurate, then it means you really understand the COUNT function, and if there is what knowledge do not understand, then this article can help you just answering questions.

1, know COUNT

About COUNT function, are detailed in the MySQL official website:

Simple translation this:

1, COUNT (expr), SELECT statement to retrieve the value returned expr row is not the number of NULL. The result is a BIGINT value.

2, if the query results did not hit any records, returns 0

3, however, it is worth noting that statistics COUNT (*), the number of rows that contain a NULL value.

create table t_count(id int,id2 int);
insert into t_count values(null,null);
insert into t_count values(1,null);
insert into t_count values(null,1);
insert into t_count values(1,null);
insert into t_count values(null,1);
insert into t_count values(1,null);
insert into t_count values(null,null);
select count(*),count(id),count(id2),count(1) from t_count;

That the following table records

root@localhost[lhrdb]> create table t_count(id int,id2 int);
Query OK, 0 rows affected (0.36 sec)
root@localhost[lhrdb]> insert into t_count values(null,null);
Query OK, 1 row affected (0.07 sec)

root@localhost[lhrdb]> insert into t_count values(1,null);
Query OK, 1 row affected (0.06 sec)

root@localhost[lhrdb]> insert into t_count values(null,1);
Query OK, 1 row affected (0.08 sec)

root@localhost[lhrdb]> insert into t_count values(1,null);
Query OK, 1 row affected (0.03 sec)

root@localhost[lhrdb]> insert into t_count values(null,1);
Query OK, 1 row affected (0.05 sec)

root@localhost[lhrdb]> insert into t_count values(1,null);
Query OK, 1 row affected (0.03 sec)

root@localhost[lhrdb]> insert into t_count values(null,null);
Query OK, 1 row affected (0.08 sec)

root@localhost[lhrdb]> 
root@localhost[lhrdb]> select * from t_count;
+------+------+
| id   | id2  |
+------+------+
| NULL | NULL |
|    1 | NULL |
| NULL |    1 |
|    1 | NULL |
| NULL |    1 |
|    1 | NULL |
| NULL | NULL |
+------+------+
7 rows in set (0.00 sec)

Use statement count (*), count (id), count (id2) query results are as follows:

root@localhost[lhrdb]> select count(*),count(id),count(id2),count(1),count(2) from t_count;

+----------+-----------+------------+----------+----------+
| count(*) | count(id) | count(id2) | count(1) | count(2) |
+----------+-----------+------------+----------+----------+
|        7 |         3 |          2 |        7 |        7 |
+----------+-----------+------------+----------+----------+

1 row in set (0.00 sec)

In addition to COUNT (id) and COUNT ( outside), you can also use COUNT (constant) (such as COUNT (1)) to count the number of rows, then the three SQL statements What difference does it make? Which in the end more efficient? Why "Ali Baba Java Development Manual" mandated not to use the COUNT (column name) or COUNT (constant) to replace the COUNT ( ) do?

The difference between (*) COUNT (column name), COUNT (constant) and COUNT
mentioned earlier COUNT (expr) the number of lines used to make statistics, statistics is the number of rows in expr is not NULL, then the COUNT (column name), COUNT (constant) and COUNT (*) three syntax, expr are column names, constants, and *.

Then the column names, constants, and * these three conditions, the constant is a fixed value, certainly not NULL. * Query can be understood as the entire line, it is certainly not NULL, then only query result column names are likely to be a NULL.

So, COUNT (constant) and COUNT (*) indicates the number of rows in the query directly qualified database table. The COUNT (column name) represents the query qualifies for the value of the column is not NULL of the number of rows.

In addition to the query result set differently than, COUNT ( ) compared COUNT (constant) and COUNT (column name) is concerned, COUNT ( ) syntax is the number of rows SQL92 standard statistical definition, because he is the standard syntax, so MySQL database him for a lot of optimization.

SQL92, an ANSI / ISO standard database. It defines a language (SQL) databases and behavior (transaction isolation level, etc.).

Optimization COUNT (*) are
mentioned earlier COUNT (*) is the syntax of the number of rows SQL92 standard statistical definition, so MySQL database against him a lot of optimization. So, what are the specific things done it?

Presented here to distinguish between different execution engines. MySQL more commonly used execution engine is InnoDB and MyISAM.

There are many differences between MyISAM and InnoDB, which has a key difference and we are going to introduce the COUNT (*) related to that MyISAM does not support transactions, MyISAM table-level lock is in the lock; and InnoDB supports transactions, and support line level locking.

Because MyISAM locks are table-level locking, so they need the serial same table above operation, so, MyISAM made a simple optimization is that it can put the number of rows in the table separately recorded, if used from a table COUNT (*) query time, you can return directly to the recorded values ​​can, of course, the premise is not there where conditions.

MyISAM reason why the total number of rows in the table can be recorded for the COUNT (*) query uses, it is because MyISAM database table-level locking, will not have to modify the number of concurrent database rows, so the number of rows resulting from the query is accurate.

However, InnoDB, the cache can not do this operation because InnoDB supports transactions, most operations are row-level locking, so may the number of rows in the table may be concurrent modification, the number of rows cached recorded It is not accurate.

However, InnoDB or for COUNT (*) statement made some optimization.

In InnoDB, using the COUNT (*) query number of rows when the table is inevitable to conduct a sweep, then, can optimize the efficiency of the efforts to sweep the table in the process.

Starting from MySQL 8.0.13, for SELECT COUNT InnoDB's (*) FROM tbl_name statement, did do some optimization in the process of sweeping the table. Provided that the query does not contain GROUP BY or WHERE conditions.

We know, COUNT (*) is intended for statistical number of rows, so that he does not care about their own specific values ​​found, so if he can sweep the table in the process, choose a low-cost index, then, it can greatly save time.

We know, InnoDB index into a clustered index (primary key index) and non-clustered index (non-primary key index), saved leaf nodes clustered indexes are whole rows, rather than clustered index leaf nodes save is the value of the primary key rows.

So, compared to a non-clustered index is much smaller than the clustered index, so MySQL will give priority to a minimum non-clustered index to sweep table. So, when we built the table, in addition to the primary key index, create a non-primary key index is still necessary.

So far, we have introduced over the MySQL database optimization for COUNT (*), which is the premise of optimizing the query does not contain WHERE and GROUP BY condition.

COUNT (*) and COUNT (1)
introduced over COUNT (*), then look at COUNT (1), for which there is no difference between the two in the end, online saying different opinions.

Some said COUNT (*) will be converted into COUNT (1) is executed, so the COUNT (1) less conversion step, so faster.

Some have even said, because for MySQL COUNT ( ) to do a special optimized, so the COUNT ( ) faster.

So, in the end Which statement is right? Look MySQL official documents is how to say:

InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way. There is no performance difference.

Painting Key: same way, no performance difference. Therefore, the COUNT (1) and COUNT (*), MySQL optimization is exactly the same, faster than anyone else who does not exist!

That being COUNT (*) and COUNT (1), as recommended by Which it?

Recommended COUNT (*)! Because this is the syntax of the number of rows SQL92 standard statistical definition, and this is just based on MySQL to do the analysis on this issue in Oracle, also different opinions of it.

COUNT (field)
Finally, there is COUNT (field) we have not yet mentioned his query is relatively simple and crude, is a full table scan, and then determine the value of the specified field is not as NULL, NULL is not cumulative .

Compared COUNT ( ), COUNT (field) one more step is to determine whether the query fields is NULL, so his performance than the COUNT ( ) slow.

Summary
This article describes the use of the COUNT function, mainly for the number of tables lines. The main uses are COUNT (*), COUNT (field) and COUNT (1).

Because the COUNT ( ) syntax is the standard number of statistics rows SQL92 definition, so he had a lot of MySQL optimization, MyISAM will be directly to the number of rows in the table separately recorded for COUNT ( ) queries, InnoDB will sweep in table when selecting the smallest index to reduce costs. Of course, the premise of these optimizations are not conditional and inquiry where the group.

In InnoDB COUNT (*) and COUNT (. 1) is not implemented on the difference, and the same efficiency, but COUNT (field) field requires determination of non-NULL, so the efficiency will be lower.

Because the COUNT ( ) syntax is the number of rows SQL92 standard statistical definition, and high efficiency, so please use the COUNT ( ) query number of rows in the table!

References:

https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_count

Me the About
------------------------------------------
● author: Wheat seedlings part of finishing from the network, if infringement please contact wheat seedlings delete
● itpub paper, blog garden, CSDN and personal micro-channel public number ( xiaomaimiaolhr have on) synchronization update
● This article itpub address: http://blog.itpub.net / 26,736,162
● This article blog Park address: http://www.cnblogs.com/lhrbest
● This article CSDN address: https://blog.csdn.net/lihuarongaini
● pdf version of this article, personal profile and wheat grass cloud disk address: HTTP : //blog.itpub.net/26736162/viewspace-1624453/
● database written exam and interview answers: http://blog.itpub.net/26736162/viewspace-2134706/
● DBA today's headlines on the address book: HTTP: / /www.toutiao.com/c/user/6401772890/#mid=1564638659405826
--------------------------------- ---------
● QQ group number: 230 161 599, 618 766 405
● micro-channel group: can I micro letter, I pull everyone into the group, you are the one
● contact me please add QQ friends (646 634 621), mention the added reason
● at 2019-10-01 06:00 - 2019-10-31 24:00 completed in Xi'an
● was last modified: 2019-10-01 06:00 2019-10-31 24:00 ~
● article content derived from wheat seedlings study notes, some sort from the network, if infringement or inappropriate Please understand
● Copyright, please share this article, reproduced Please keep the source
--- ---------------------------------------
Wheat seedlings micro storehttps://weidian.com/s/793741433?wfr=c&ifr=shopdetail
Wheat seedlings series published database categoryhttp://blog.itpub.net/26736162/viewspace-2142121/
Wheat seedlings OCP, OCM, high-availability network classeshttp://blog.itpub.net/26736162/viewspace-2148098/
Wheat seedlings Tencent University home page: Https://lhr.ke.qq.com/
------------------------------------- -----
use micro-channel client scan the following QR code to focus on wheat seedlings micro-channel public number ( xiaomaimiaolhr )
and QQ group (DBA book), add wheat seedlings micro-letters, learn the most practical database technology.
Here Insert Picture Description
------------------------------------------

Guess you like

Origin blog.csdn.net/lihuarongaini/article/details/102658504