当前位置:网站首页>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
边栏推荐
- 12306 ticket system crawling - 1. Saving and reading of city code data
- keepalived双机热备
- Kotlin function
- Flutter distribution
- 23.5 event listeners of application events and listeners
- [untitled]
- Fluent custom popupmenubutton
- Lesson 3: gcc compiler
- Analysis on the query method and efficiency of Oracle about date type
- Cadence (x) wiring skills and precautions
猜你喜欢

Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式

pl/sql之集合
![[untitled]](/img/3e/fb7c4d901643e6ea88a7d90ead045c.png)
[untitled]

Kotlin function

Pxe原理和概念

Transfer guide printing system based on C language design

基于Raft共识协议的KV数据库

6、 Pinda general permission system__ pd-tools-log

Flitter imitates wechat long press pop-up copy recall paste collection and other custom customization

Super potential public chain dfinity -- the best time for DFI developers to enter
随机推荐
Redis advanced
Flutter text is left aligned with no blank space in the middle
Poor English, Oracle OCP or MySQL OCP exam can also get a high score of 80 points
[suggestions collection] summary of MySQL 30000 word essence - locking mechanism and performance tuning (IV) [suggestions collection]
MySQL 8.0 OCP (1z0-908) has a Chinese exam
Cve-2021-3156 duplicate of sudo heap overflow privilege raising vulnerability
Install HR schema, example, and Scott schema on Oracle and MySQL
Flutter WebView three fingers rush or freeze the screen
Use index to optimize SQL query "suggestions collection"
23.5 event listeners of application events and listeners
keepalived双机热备
Solve the problem of C # calling form controls across threads
Automation and disconnection monitoring of video addition
sklearn 机器学习基础(线性回归、欠拟合、过拟合、岭回归、模型加载保存)
【加密周报】加密市场有所回温?寒冬仍未解冻 盘点上周加密市场发生的重大事件
23.2 customizing the banner control display hidden banner modify banner
Flutter custom player progress bar
Analysis on the query method and efficiency of Oracle about date type
Run file command
Xtrabackup appears' flush no '_ WRITE_ TO_ BINLOG TABLES‘: 1205 (HY000) Lock wait timeout exceeded;