当前位置:网站首页>What are the differences in the performance of different usages such as count (*), count (primary key ID), count (field) and count (1)? That's more efficient
What are the differences in the performance of different usages such as count (*), count (primary key ID), count (field) and count (1)? That's more efficient
2022-07-26 08:45:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Different count usage
In the comments section of the previous article , A classmate left a message and asked : 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 . Today I talked about count(*) Performance problems of , I'll take this opportunity to explain the performance differences of these usages with you in detail .
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(*).
Summary
today , I talked to you MySQL Two ways to get the number of rows in the table . 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/111647.html Link to the original text :https://javaforall.cn
边栏推荐
- 利用模m的原根存在性判断以及求解
- Implementation of Prometheus web authentication and alarm
- Neo eco technology monthly | help developers play smart contracts
- PXE principles and concepts
- 23.9 application exit application exit
- Why reserve a capacitor station on the clock output?
- 23.10 Admin features
- Oracle 19C OCP 1z0-082 certification examination question bank (19-23)
- The full name of flitter IDFA is identity for advertisers, that is, advertising identifiers. It is used to mark users. At present, it is most widely used for advertising, personalized recommendation,
- Automation and disconnection monitoring of video addition
猜你喜欢
Xshell batch send command to multiple sessions
C#入门系列(三十一) -- 运算符重载
基于C#实现的文件管理文件系统
Excel find duplicate lines
Solve the problem of C # calling form controls across threads
基于C语言的哈夫曼转化软件
Cadence (x) wiring skills and precautions
正则表达式:判断是否符合USD格式
pl/sql之集合-2
Neo eco technology monthly | help developers play smart contracts
随机推荐
Uninstallation of dual systems
Leetcode and query question summary
Kotlin function
Arbitrum launched the anytrust chain to meet the diverse needs of ecological projects
The full name of flitter IDFA is identity for advertisers, that is, advertising identifiers. It is used to mark users. At present, it is most widely used for advertising, personalized recommendation,
The data read by Flink Oracle CDC is always null. Do you know
为什么要在时钟输出上预留电容的工位?
基于Raft共识协议的KV数据库
node-v下载与应用、ES6模块导入与导出
Flutter custom player progress bar
Huffman transformation software based on C language
[freeswitch development practice] use SIP client Yate to connect freeswitch for VoIP calls
Oracle 19C OCP 1z0-082 certification examination question bank (30-35)
Excel find duplicate lines
Oracle 19C OCP 1z0-082 certification examination question bank 1
TypeScript版Snowflake主键生成器
基于C语言实现的人机交互软件
基于C语言设计的换乘指南打印系统
QSS add resource file of QT
Run file command