【mysql】count(*), the difference between count(1) and count(column)

count(*) calculates the number of rows, including NULL
count(column) calculates the number of rows with a specific column value, excluding NULL values.
There is another way to use count(), the result of count(1) is the same as count(*).

Performance issues

1. In any case, SELECT COUNT(*) FROM tablename is the best choice;
2. Minimize such queries as SELECT COUNT(*) FROM tablename WHERE COL = 'value';
3. Avoid SELECT COUNT(COL) FROM tablename WHERE COL2 = Occurrence of 'value'.

If the table has no primary key, then count(1) is faster than count(*).
If there is a primary key, then count(primary key, union primary key) is faster than count(*).
If the table has only one field, count(*) is fastest.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325885701&siteId=291194637