当前位置:网站首页>Which of count (*), count (primary key ID), count (field) and count (1) in MySQL is more efficient? "Suggested collection"
Which of count (*), count (primary key ID), count (field) and count (1) in MySQL is more efficient? "Suggested collection"
2022-07-26 08:50:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
stay select count(?) from t In such query statements ,count(*)、count( Primary key id)、count( Field ) and count(1) And so on , What are the differences .
It should be noted that , The following discussion is based on InnoDB Engine .
here , First of all, you have to find out count() The semantics of the .count() It's an aggregate function , For the returned result set , Judge line by line , If count The argument to the function is not NULL, Add... To the cumulative value 1, Otherwise, we will not add . Finally return the cumulative value .
therefore ,count(*)、count( Primary key id) and count(1) All indicates that the total number of result sets satisfying the conditions is returned ; and count( Field ), It means to return the data line that meets the conditions , Parameters “ Field ” Not for NULL Total number of .
When it comes to analyzing performance differences , You can remember these principles :
- server Give the floor whatever you want ;
- InnoDB Give only the necessary value ;
- Now the optimizer only optimizes count(*) The meaning of is “ Take the number of lines ”, other “ Obvious ” The optimization of is not done .
What does that mean ? Next , Let's take a look one by one .
about count( Primary key id) Come on ,InnoDB The engine will traverse the entire table , Put each line of id Take out all the values , Return to server layer .server Get the floor id after , Judgment cannot be empty , Just add up by lines .
about count(1) Come on ,InnoDB The engine traverses the entire table , But no value .server Layer for each row returned , Put a number “1” go in , Judgment cannot be empty , Add by line .
Just look at the difference between these two usages , You can compare ,count(1) Perform better than count( Primary key id) fast . Because back from the engine id It's going to involve parsing the data lines , And copy field values .
about count( Field ) Come on :
- If this “ Field ” Is defined as not null Words , Read the field line by line from the record , Judgment cannot be null, Add by line ;
- If this “ Field ” The definition is allowed to be null, So when it comes to execution , To judge that it might be null, We need to take out the value and judge again , No null Just add up .
That's the first principle ,server What fields does the layer want ,InnoDB What fields are returned .
however count(*) It's an exception , It doesn't take all the fields out , It's optimized , No value .count(*) Definitely not null, Add by line .
See here , You must say , Can't the optimizer judge for itself , Primary key id It must be empty , Why can't we follow count(*) To deal with it , What a simple optimization .
Of course ,MySQL Optimized specifically for this statement , It's not impossible . But there are too many situations that require specialized optimization , and MySQL Has been optimized count(*) 了 , You can just use this usage directly .
So the conclusion is :
In order of efficiency ,count( Field )<count( Primary key id)<count(1)≈count(*), So I suggest you , Use as much as possible count(*).
We mentioned that in different engines count(*) The implementation of is different , The problems of using cache system to store count values are also analyzed .
Actually , Put the count on Redis Inside , Counting and... Cannot be guaranteed MySQL The reason why the data in the table are accurate and consistent , yes These two different storage systems , Distributed transactions are not supported , Can't get an accurate and consistent view . And put the count value in MySQL in , This solves the problem of consistent view .
InnoDB Engine support transactions , We make good use of the atomicity and isolation of transactions , You can simplify the logic in business development . This is also InnoDB One of the reasons why engines are popular .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/111646.html Link to the original text :https://javaforall.cn
边栏推荐
- File management file system based on C #
- The data read by Flink Oracle CDC is always null. Do you know
- Kotlin properties and fields
- Oracle 19C OCP 1z0-082 certification examination question bank (19-23)
- [recommended collection] summary of MySQL 30000 word essence - partitions, tables, databases and master-slave replication (V)
- Using the primitive root of module m to judge and solve
- TypeScript版加密工具PasswordEncoder
- 2000年的教训。web3是否=第三次工业革命?
- Oracle 19C OCP 1z0-083 question bank (7-12)
- 23.2 customizing the banner control display hidden banner modify banner
猜你喜欢

2000年的教训。web3是否=第三次工业革命?

Study notes of automatic control principle --- stability analysis of control system

1、 Redis data structure

海内外媒体宣发自媒体发稿要严格把握内容关

My meeting of OA project (meeting seating & submission for approval)

One click deployment of lamp and LNMP scripts is worth having

Mysql8 one master one slave +mycat2 read write separation

In the first year of L2, the upgrade of arbitrum nitro brought a more compatible and efficient development experience

六、品达通用权限系统__pd-tools-log

tcp 解决short write问题
随机推荐
Transfer guide printing system based on C language design
Oracle 19C OCP 1z0-083 question bank (1-6)
SSH,NFS,FTP
基于C语言的哈夫曼转化软件
Spark SQL common date functions
Mycat2 deploy master-slave MariaDB
Solve the problem of C # calling form controls across threads
Please tell me if there is any way to increase the write out rate when the Flink SQL client is in the sink table. When synchronizing through sink table
海内外媒体宣发自媒体发稿要严格把握内容关
Oracle 19C OCP certification examination software list
SSH,NFS,FTP
Winter vacation homework & Stamp cutting
Web概述和B/S架构
Memory management based on C language - Simulation of dynamic partition allocation
Kept dual machine hot standby
P3743 kotori的设备
The data read by Flink Oracle CDC is always null. Do you know
有限元学习知识点备案
Oracle 19C OCP 1z0-082 certification examination question bank (19-23)
基于C#实现的文件管理文件系统