当前位置:网站首页>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(*)
边栏推荐
- Miscellaneous talk on May 27
- C语言入门指南
- (ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
- C语言实现扫雷游戏(完整版)
- hashCode()与equals()之间的关系
- 【手撕代码】单例模式及生产者/消费者模式
- MySQL lock summary (comprehensive and concise + graphic explanation)
- Cloud native trend in 2022
- C语言入门指南
- C language Getting Started Guide
猜你喜欢
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
[面试时]——我如何讲清楚TCP实现可靠传输的机制
Cloud native trend in 2022
The latest tank battle 2022 full development notes-1
fianl、finally、finalize三者的区别
仿牛客技术博客项目常见问题及解答(二)
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
Leetcode.3 无重复字符的最长子串——超过100%的解法
2. Preliminary exercises of C language (2)
canvas基础2 - arc - 画弧线
随机推荐
MPLS experiment
hashCode()与equals()之间的关系
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
Redis cache obsolescence strategy
魏牌:产品叫好声一片,但为何销量还是受挫
A piece of music composed by buzzer (Chengdu)
4.二分查找
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
Questions and answers of "basic experiment" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
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
String abc = new String(“abc“),到底创建了几个对象
6.函数的递归
Aurora system model of learning database
PriorityQueue (large root heap / small root heap /topk problem)
(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
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
1.C语言初阶练习题(1)
杂谈0516
View UI plus released version 1.3.1 to enhance the experience of typescript