The difference between Select count(*) Select count(1) Select count(field)

 

The first thing to know COUNT(1) and  COUNT(*)express is to directly query the number of rows in the database table that meets the conditions, and it COUNT(列名/字段)means to query the number of rows where the value of the eligible column is not NULL.

So here is a conclusion:

COUNT(1) and COUNT(*): including statistics on NULL

COUNT (field): Does not include statistics for NULL

Secondly, there are many statements about the different performance of COUNT(1) and COUNT(*) (COUNT(1) has better performance than COUNT(*)), but in fact, there is no difference in implementation between these two query methods, and the efficiency is the same.

to sum up:

COUNT(1) and COUNT(*): include the statistics on NULL; COUNT (field): do not include the statistics on NULL.

There is no difference between COUNT(*) and COUNT(1) in implementation, and the efficiency is the same, but COUNT (field) requires non-NULL judgment of the field, so the efficiency will be lower.

Guess you like

Origin blog.csdn.net/qq_44624536/article/details/114992244