Detailed explanation of COUNT() function

COUNT, as the name implies, means counting. This function is useful for counting the number of records in the table.

 

Introduction to MySQL COUNT() function

COUNT()The function returns the number of rows in the table. COUNT()The function allows you to count all rows in the table that meet certain conditions.

COUNT()The syntax of the function is as follows −

  COUNT(expression)


COUNT()函数的返回类型为BIGINT。 如果没有找到匹配的行,则COUNT()函数返回0

COUNTFunction of several forms: COUNT(*), COUNT (. 1) , COUNT ( column )COUNT (the DISTINCT  column ) and COUNT (expression The) .

COUNT(*) function

COUNT(*)The function returns the number of rows in the result set returned by the SELECT statement. COUNT(*)The function calculates the rows that contain NULLand is not a NULLvalue, that is: all rows.

If you use a COUNT(*)function to count the number rows in the table without using the WHERE clause to select other columns, its execution speed is very fast.

This optimization only applies to MyISAM tables, because the number of rows of a MyISAM table is stored in the columns of the information_schemadatabase tablestable table_rows; therefore, MySQL can retrieve it very quickly.

COUNT(1) function

The usage of count(1) is the same as the result of count(*).

 

COUNT(DISTINCT COLUMN)

MySQL COUNT(DISTINCT column)Returns the NULLnumber of unique rows that do not contain a value.

 

COUNT(COLUMN)

MySQL COUNT(column)Returns the NULLnumber of all rows that do not contain a value.

COUNT(expression)

COUNT(expression)Returns the NULLnumber of rows that do not contain a value, and expression  is an expression.

 

Let's look at an example:

Original table data:

 

1、count(*)

 

SELECT COUNT(*) count FROM t_iov_help_feedback

 

result:

As you can see, as long as there are records in the table, no matter whether there is a NULL field, it will be counted out, and the number of queries will be 4 (all) .

 

2、count(1)

 

SELECT COUNT(1) count FROM t_iov_help_feedback

 

 

result:

As you can see, the usage of count(1) is the same as the result of count(*), and the number of queries is 4 (all) .

If the table does not have a primary key, then count(1) is faster than count(*);

If there is a primary key, then count (primary key, combined primary key) is faster than count (*);

If the table has only one field, count(*) is the fastest.

count(1) is the same as count (primary key), only the primary key is scanned. Count(*) is the same as count (non-primary key), which scans the entire table. Obviously the former is faster.

Performance issues:
1. In any case, SELECT COUNT(*) FROM tablename is the best choice, (referring to the absence of where);
2. Minimize such queries as SELECT COUNT(*) FROM tablename WHERE COL ='value';
3 . Eliminate the occurrence of SELECT COUNT(COL) FROM tablename WHERE COL2 ='value'.

 

 

3、count(column)

 

SELECT COUNT(UPLOAD_PICTURES) count FROM t_iov_help_feedback; 

result:

 

As you can see, the records whose UPLOAD_PICTURES field is NULL are not counted. One record is NULL, so the number of queries is 3.

 

4、count(DISTINCT column)

 

SELECT COUNT(DISTINCT UPLOAD_PICTURES) count FROM t_iov_help_feedback;

result:

 

 

 

As you can see, the records whose UPLOAD_PICTURES field is NULL are not counted, and the duplicate record is only counted as one.

One record is NULL, and two records have duplicate UPLOAD_PICTURES field values ​​(both are "s"), so the number of queries is 2.

 

5、count(expression)

expression represents an expression, which means that we can get the number of records that meet the characteristic conditions in count.

Example 1, to query the number of is_reply=0:

 

SELECT COUNT(IF(is_reply=0,1,NULL)) count FROM t_iov_help_feedback;

Result: It

can be seen that there are 3 records with is_reply=0, so the number of queries is 3.

 

Example 2: Query the number of is_reply=1:

 

SELECT COUNT(IF(is_reply=1,1,NULL)) count FROM t_iov_help_feedback;

Result: As

you can see, there is 1 record with is_reply=1, so the number of queries is 1.

 

COUNT(IF(is_reply=1,1,NULL)) statement analysis:

IF (expression 1, expression 2, expression 3),

The IF() function is a simple determiner, it will determine whether the first parameter is true, if it is true, the IF function will return expression 2 ; otherwise, it will return expression 3 .

This means: if is_reply=1, the IF function will return 1, otherwise it will return NULLCOUNTThe function only counts 1, not the NULLvalue, so the query returns the number of records based on the corresponding status.

 

 

 

OK,That's all.

 

Welcome to follow the WeChat public account (Java Practice Notes):

Focus on the accumulation of Java technology, share free Java technology dry goods, study notes, study materials, etc., and strive to make this a Java knowledge station.

 

Guess you like

Origin blog.csdn.net/u012660464/article/details/78623038