1. COUNT(*) and COUNT(COL) COUNT(*) usually performs an index scan on the primary key, but COUNT(COL) is not necessarily the case. In addition, the former counts the total number of all matching records in the statistical table, while the latter counts the number of all matching COL records in the calculation table. There are differences. 1. SELECT COUNT(*) FROM tablename is the best choice in any case; 2. Try to reduce queries like SELECT COUNT(*) FROMtablename WHERE COL = 'value'; 3. Prevent the occurrence of SELECT COUNT(COL) FROM tablename WHERE COL2 ='value'. 2. COUNT(*) or COUNT(id) According to my understanding, it should be faster to use COUNT(id) because if my id is an auto-incrementing primary key, then calculating its number obviously consumes fewer resources than calculating the number of all fields. But I have seen more than one similar article on how to speed up MySQL queries, which recommends that we use SELECT COUNT(*) instead of directly COUNT the primary key. Why is that? It seems that this is because the table using the MyISAM engine stores the total number of entries. If there is no WHERE or the WHERE is always true (such as WHERE 1), then COUNT(*) can directly return the total number of entries. In addition, it is obvious that COUNT(*) does not mean "calculate all fields". Obviously, MySQL will interpret * as "a piece of data". Test data, simple comparison, no further testing: #0.817-Query time for one million records select count(*) from student ; #0.789-Query time for one million records select count(id) from student; #1.011-Query time for one million records select count(name) from student; #1.162-Query time for one million recordsSELECT COUNT(*) FROM student WHERE namelike '%xiaoli%';#By default, the primary key index is used for query, but the index becomes invalid after adding the like condition Summarize In general, using COUNT(id) is faster. Here is a simple comparison for your reference. The above is all the content of this article about MySQL optimization summary - total number of queries. I hope it will be helpful to everyone. Interested friends can refer to: MySQL optimization using joins instead of subqueries, MYSQL subquery and nested query optimization example analysis, MySQL in statement subquery efficiency optimization skills examples, etc. If there are deficiencies, please leave a message to point them out. Thank you friends for supporting this site! You may also be interested in:
|
<<: js implements single click to modify the table
>>: How to build php+nginx+swoole+mysql+redis environment with docker
What is vuex vuex: is a state manager developed s...
Sometimes we need to control whether HTML elements...
The first type: full CSS control, layer floating ...
1. Basic use <!DOCTYPE html> <html lang=...
The docker create command can create a container ...
The table is as follows: HTML source code Display...
Table of contents Show Me The Code Test the effec...
Development Trends: html (Hypertext Markup Languag...
The <tfoot> tag is used to define the style...
I have never used apache. After I started working...
1 Pull the image from hup docker pull nginx 2 Cre...
tomcat server configuration When everyone is lear...
Without further ado, let me give you the code. Th...
For example, to query yesterday's newly regis...
Table of contents Review of Object.defineProperty...