当前位置:网站首页>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
边栏推荐
- Oracle 19C OCP 1z0-083 question bank (1-6)
- 基于Raft共识协议的KV数据库
- CIS 2020 - alternative skills against cloud WAF (pyn3rd)
- 解决C#跨线程调用窗体控件的问题
- OA项目之我的会议(查询)
- 基于C#实现的文件管理文件系统
- Web3 Games: current situation and future
- 03 exception handling, state keeping, request hook -- 04 large project structure and blueprint
- Oracle 19C OCP 1z0-082 certification examination question bank (30-35)
- One click deployment of lamp and LNMP scripts is worth having
猜你喜欢

Kotlin properties and fields

Winter vacation homework & Stamp cutting

node的js文件引入

Memory management - dynamic partition allocation simulation

Arbitrum Nova release! Create a low-cost and high-speed dedicated chain in the game social field

Excel delete blank lines

基于C语言的哈夫曼转化软件

利用模m的原根存在性判断以及求解

Kotlin属性与字段

基于C语言设计的换乘指南打印系统
随机推荐
基于C语言设计的换乘指南打印系统
Cve-2021-26295 Apache OFBiz deserialization Remote Code Execution Vulnerability recurrence
File management file system based on C #
pl/sql之集合
Set of pl/sql -2
Solve the problem of C # calling form controls across threads
Oracle 19C OCP 1z0-082 certification examination question bank 1
Number of briquettes & Birthday Candles & building blocks
Pxe原理和概念
IC's first global hacking bonus is up to US $6million, helping developers venture into web 3!
[database] gbase 8A MPP cluster v95 installation and uninstall
node的js文件引入
Dear teachers, how can sqlserver get DDL in flinkcdc?
Set of pl/sql
[recommended collection] summary of MySQL 30000 word essence - partitions, tables, databases and master-slave replication (V)
Ueditot_ JSP SSRF vulnerability recurrence
Super potential public chain dfinity -- the best time for DFI developers to enter
基于C语言的哈夫曼转化软件
【FreeSwitch开发实践】自定义模块创建与使用
Alphabetic string