Count(*), Count(1), Count(0) difference and execution efficiency comparison

the difference

  • count(*) includes all the columns, which is equivalent to the number of rows. When counting the results, the column value will not be ignored.
  • count(1) includes ignoring all columns. 1 represents the code line. When counting the results, the column value will not be ignored.
  • The count (column name) only includes the column name. When counting the results, the column value will be ignored (the empty string here is not just an empty string or 0, but represents a null) count, that is, a certain field value When NULL, no statistics.

effectiveness

  • The column name is the primary key, count (column name) will be faster than count(1)

  • The column name is not the primary key, count(1) will be faster than count(column name)

  • If the table does not have a primary key and has multiple columns, the execution efficiency of count(1) is better than count(*)

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

  • If there is a primary key, the execution efficiency of count (primary key) is optimal

  • When there is a where condition, count (*) is much faster than count (column names are not primary keys)

  • In general, count(*) is an excellent solution. Unless count (primary key)

Guess you like

Origin blog.csdn.net/weixin_44322234/article/details/108528371