当前位置:网站首页>Implementation of count (*) in MySQL
Implementation of count (*) in MySQL
2022-07-06 13:42:00 【Wake up duck, did you program today?】
stay MySQL in , Different storage engines ,count(*) There are different implementations .
MyISAM The engine stores the total row number of a table on disk , So execute count(*) It will return this number directly , It's very efficient
and InnoDB The engine is troublesome , It performs count(*) When , You need to read the data line by line from the engine , And then the cumulative count
All the above are without filter conditions count(*), If you add where conditions MyISAM Tables can't return so fast .
We also know that show table status If you order , It will be found that there is also a TABLE_ROWS Used to show how many rows this table currently has , This order is fast , So this one TABLE_ROWS Can replace count(*) Do you ?
answer : Of course not ,TABLE_ROWS It is estimated by sampling , So it's not very accurate , Official documents mention that the error may reach 40%~50%, therefore show table status The number of lines displayed by the command cannot be used directly
Summary :MyISAM Although the table count(*) Soon , But it doesn't support transactions
show table status The command returns quickly , But it's not accurate
InnoDB The table directly count(*) Will traverse the entire table , Although the results are accurate , But it will lead to performance problems
If you now have a page that often shows the total number of operation records of the trading system, what should you do ?
The answer is that we can only count by ourselves .
- Save the count with the cache system
- Save the count in the database
The way count(*)、count( Primary key id)、count( Field )、count(1) What are the differences in performance ?
- count( Primary key id),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 .
- count(1),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 these two count(1) than count( Primary key id) Be quick , Because back from the engine id It's going to involve parsing the data lines , And copy field values .
- count( Field )
- 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
- count(*)、 It doesn't take all the fields out , It's optimized , No value .count(*) Definitely not null, Add by line
Conclusion : In order of efficiency ,count( Field ) < count( Primary key id) < count(1) About equal to count(*), So try to use count(*)
边栏推荐
- String abc = new String(“abc“),到底创建了几个对象
- Pit avoidance Guide: Thirteen characteristics of garbage NFT project
- Zatan 0516
- 5月14日杂谈
- 【手撕代码】单例模式及生产者/消费者模式
- The latest tank battle 2022 - full development notes-3
- 4.分支语句和循环语句
- string
- ArrayList的自动扩容机制实现原理
- Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
猜你喜欢
随机推荐
【九阳神功】2017复旦大学应用统计真题+解析
C language Getting Started Guide
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
MPLS experiment
Why use redis
强化学习系列(一):基本原理和概念
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
西安电子科技大学22学年上学期《射频电路基础》试题及答案
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
Implement queue with stack
The latest tank battle 2022 - Notes on the whole development -2
Change vs theme and set background picture
4.二分查找
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
[中国近代史] 第九章测验
[graduation season · advanced technology Er] goodbye, my student days
View UI plus released version 1.3.0, adding space and $imagepreview components
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
【九阳神功】2016复旦大学应用统计真题+解析