当前位置:网站首页>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(*)
边栏推荐
- C language to achieve mine sweeping game (full version)
- 【毕业季·进击的技术er】再见了,我的学生时代
- Cloud native trend in 2022
- 简单理解ES6的Promise
- [中国近代史] 第九章测验
- 6.函数的递归
- 重载和重写的区别
- FAQs and answers to the imitation Niuke technology blog project (II)
- ArrayList的自动扩容机制实现原理
- [graduation season · advanced technology Er] goodbye, my student days
猜你喜欢

Cloud native trend in 2022

(super detailed II) detailed visualization of onenet data, how to plot with intercepted data flow

最新坦克大战2022-全程开发笔记-1

西安电子科技大学22学年上学期《射频电路基础》试题及答案

Caching mechanism of leveldb
![[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission

9. Pointer (upper)

C语言入门指南

(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。

MPLS experiment
随机推荐
5月14日杂谈
6.函数的递归
【九阳神功】2018复旦大学应用统计真题+解析
7.数组、指针和数组的关系
C语言入门指南
[graduation season · advanced technology Er] goodbye, my student days
凡人修仙学指针-1
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
View UI plus released version 1.2.0 and added image, skeleton and typography components
2. C language matrix multiplication
4. Binary search
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
Wei Pai: the product is applauded, but why is the sales volume still frustrated
View UI plus released version 1.3.0, adding space and $imagepreview components
List set map queue deque stack
5. Download and use of MSDN
FileInputStream和BufferedInputStream的比较
string