编程知识 cdmana.com

Usage relations and differences of count (1), count (*) and count (a field) in MySQL

One . From the execution results

count(*): All rows are counted , Include NULL That's ok 

count(1): All rows are counted , Include NULL That's ok 

count(column): Yes column Central African Null Make statistics 

Two . In terms of execution efficiency

There will be some differences between them according to different situations ,MySQL Would be right count(*) Make optimization .

(1) If the column is the primary key ,count( Name ) Efficiency is better than count(1)  

(2) If the column is not a primary key ,count(1) Efficiency is better than count( Name )  

(3) If there is a primary key in the table ,count( Primary key column name ) The efficiency is the best   

(4) If there is only one column in the table , be count(*) The efficiency is the best   

 (5) If the table has multiple columns , And there is no primary key , be count(1) Efficiency is better than count(*)

3、 ... and . count(1) principle

count(1), In fact, it is to calculate the number of qualified lines .1 It doesn't mean the first field , It's a fixed value .

In fact, it can be thought that there is such a field in the table : This field is a fixed value 1,count(1), That's how many in all 1.


Four . count(*) principle

count(*), During execution, the asterisk will be translated into the specific name of the field , The effect is the same .

But there is one more translation action , Slightly less efficient than the fixed value approach .

版权声明
本文为[Old fellow iron has dried up the code.]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145845284b.html

Scroll to Top