MySQL study notes: The difference between count (1), count (*), count (field)

About database statistics the number of rows, 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有几种用法?
2、COUNT(字段名)和COUNT(*)的查询结果有什么不同?
3、COUNT(1)和COUNT(*)之间有什么不同?
4、COUNT(1)和COUNT(*)之间的效率哪个更高?
5、为什么《阿里巴巴Java开发手册》建议使用COUNT(*)
6、MySQL的MyISAM引擎对COUNT(*)做了哪些优化?
7、MySQL的InnoDB引擎对COUNT(*)做了哪些优化?
8、上面提到的MySQL对COUNT(*)做的优化,有一个关键的前提是什么?
9、SELECT COUNT(*) 的时候,加不加where条件有差别吗?
10、COUNT(*)、COUNT(1)和COUNT(字段名)的执行过程是怎样的?

More than 10 questions, if you can answer all accurate, then it means you really understand the COUNTfunction of.

1. acquaintance COUNT

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

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

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

In addition to COUNT(id)and COUNT(*)outside, can also be used COUNT(常量)(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" mandatory not to use COUNT(列名)or COUNT(常量)to replace COUNT(*)it?

The difference between (*) 2.COUNT (field), COUNT (constant) and COUNT

COUNT(常量)And COUNT(*)represents the number of rows in the query directly qualified database table.

And COUNT(列名)it represents the query qualifies for the value of the column is not NULL of the number of rows.

COUNT(*)The syntax is the number of rows SQL92 standard statistical definition, because it is the standard syntax, so a lot of MySQL database optimization.

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

3.COUNT (*) optimization

MySQLThe main use of two kinds of execution engine:

  • InnoDB engine
  • MyISAM engine

MyISAM does not support transactions, MyISAM table-level lock is in the lock; and InnoDB supports transactions, and supports row-level locking.

MyISAM

MyISAM made a simple optimization, the number of rows in the table separately recorded, if the execution count(*)can be directly returned, the premise is not there where conditions. MyISAM is the table-level lock, no concurrent operation of the line, so found the results to be accurate.

InnoDB

InnoDB can not use this cache operation, because support services, most operations are row-level locking, parallel lines may be modified, then the cache records are not accurate.

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

Table index is scavenged by low-cost, rather than focus on specific contents of the table.

Saved InnoDB index into clustered index (primary key index) and a non-clustered index (non-primary key index), stored in the leaf nodes clustered index is the entire rows, rather than clustered index leaf node is the primary key values ​​of the rows.

MySQL will give priority to the smallest non-clustered index to sweep table.

Query optimization on the premise that does not contain conditions and where group by condition.

4.COUNT(*)和COUNT(1)

MySQL official documentation to say:

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

So, for count(1)and count(*), MySQL optimization is exactly the same, who simply does not exist faster!

But still it recommended count(*), as this is the syntax of the number of rows SQL92 standard statistical definition.

5.COUNT (field)

Full table scan, it is determined whether the value of the specified field NULL, not NULLthe accumulation.

Performance ratio count(1)and count(*)slower.

6. Summary

COUNTUsage of functions, mainly for the number of tables lines. The main uses are COUNT(*), COUNT(字段)and COUNT(1).

Because the COUNT(*)syntax of the number of rows SQL92 standard statistical 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(*)query, InnoDB will choose the smallest index in the table when to sweep cut costs. Of course, the premise of these optimizations are not conditional and inquiry where the group.

In InnoDB COUNT(*)and COUNT(1)did not realize the difference, and the same efficiency, but COUNT(字段)require a non-NULL fields judgment, the efficiency will be lower.

Because COUNT(*)a number of SQL92-defined standard statistical line syntax, and high efficiency, so please use the direct COUNT(*)number of rows in the lookup table!

Reference links: MySQL's COUNT statement, actually can be the interviewer child so badly! ?

Guess you like

Origin www.cnblogs.com/hider/p/11726690.html