Efficiency comparison between mysql select count(*) and select count(id)

​Thanks to
Gary Chen
Gary Chen
, author of "The Way of MySQL DBA Cultivation". Focus on product, architecture and data.
6 people agree with the answer
1. There are also logical differences, count(*) is the number of rows recorded in the query table, and count(col) is the number of queries (not null);
2. Assuming the myisam engine, record The number is part of the structure, it has been cached in memory, and the structure can be obtained soon, but innodb still needs to calculate, if the id is the primary key index, it will undoubtedly speed up the speed;
3. You need to consider the case where the where clause is added.
Posted on 2011-05-14
6
​5 Comments
​Share
​Favorites
​Thanks to
Li Anqi Li Anqi
Chief Technology Officer of Yingqiu Industrial Investment Co., Ltd. 8 people agreed with this answer 1. Unless you want to count the total number of non-null values ​​in a certain column, any COUNT(*) is always used in all situations, and the efficiency is much higher than COUNT (column name) . 2. Unless there are special needs, otherwise COUNT( ) should not add the WHERE condition, which will seriously affect the efficiency. If the conditions COUNT( ) and COUNT (primary key) are added The efficiency is consistent, COUNT (non-primary key) is very inefficient 3. In the absence of WHERE conditions: COUNT(*) is equal to COUNT (primary key) is better than COUNT (non-primary key has an index) is better than COUNT (non-primary key has no index)





4. As long as WHERE is added, the efficiency will be reduced, even if WHERE 1=1
5. The above conclusions are obtained by personal testing on MySql and Sql Server

Guess you like

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