当前位置:网站首页>MySQL 45 lecture learning notes (XIV) count (*)
MySQL 45 lecture learning notes (XIV) count (*)
2022-07-04 06:38:00 【Tom Kong】
One .count(*) How to implement
Different MySQL engine ,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 ;
- InnoDB The engine needs to read it line by line from the engine and count it up
If you add where Conditions ,MyISAM Tables can't return so fast
Two . Why? InnoDB Not put MyISAM Save the data ?
Even in Multiple queries at the same time , because Multi version concurrency control (MVVC),InnoDB Table should How many lines back I'm not sure .
- InnoDB It's the index organization table , Primary key index tree Leaf nodes are data , And ordinary index tree Leaf nodes are primary key values .
- Ordinary index tree Than Primary key index tree Many small , Yes count(*) This kind of operation , Traverse the index tree The results are the same .
MySQL Optimizer Will find the smallest tree to traverse .
On the premise that the logic is correct , Minimize the amount of data scanned , This is database system design One of the general rules of
3、 ... and .show table status
show table status command Number of lines displayed also Can't be used directly .
Four . Database summary
- MyISAM surface although count(*) Soon , however Unsupported transaction ;
- show table status command Although it 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 Cause performance problems .
5、 ... and . Cache system save count
One . Design thinking :
- Every row inserted in this table Redis Add one to the count
- The count is reduced by one for each Deleted Row
- In this case , Read and update operations are fast
The cache system may lose updates
- A row has just been inserted into the data table ,Redis The value saved in is also added with a line , then Redis Abnormal restart , Restart from the stored redis Read the value back in the place of the data , Just add one and you will lose .
Solution :
- Redis After abnormal restart , Go to the database and execute it alone count(*) Gets the actual number of rows , Then write the value back to Redis Li will do
In a concurrent system , We are Unable to accurately control the execution time of different threads , Whether it's Write cache first and then database still Write the database first and then the cache There is no guarantee of absolute accuracy .
6、 ... and .count(*)、count( Primary key id)、count( Field ) and count(1) And so on
- about count( Primary key id) Come on ,InnoDB The engine traverses the whole table , Put each line id out ,server Layer space determination .
- about count(1) Come on ,.server Layer for returning InnoDB The engine traverses the entire table , But every line that does not take value , Put a number “1” go in , Judgment cannot be empty , Add by line .
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 , as well as Copy field values The operation of
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 .
count(*) Specially optimized
count( Field )<count( Primary key id)<count(1)==count(*)
recommend count(*)!
边栏推荐
- C réaliser des jeux de serpents gourmands
- Mysql 45讲学习笔记(十三)表数据删掉一半,表文件大小不变
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- Dimension and format of data
- Data analysis notes 09
- selenium驱动IE常见问题解决Message: Currently focused window has been closed.
- what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
- Mysql 45讲学习笔记(十)force index
- How to avoid JVM memory leakage?
- Redis面试题集
猜你喜欢

centos8安装mysql.7 无法开机启动
![[backpack DP] backpack problem](/img/7e/1ead6fd0ab61806ce971e1612b4ed6.jpg)
[backpack DP] backpack problem

Learning multi-level structural information for small organ segmentation

分布式CAP理论

Fundamentals of SQL database operation

How to avoid JVM memory leakage?

MySQL learning notes 3 - JDBC
![[March 3, 2019] MAC starts redis](/img/ff/88638fcdc8d24dc268781c224e8195.jpg)
[March 3, 2019] MAC starts redis
![[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)](/img/03/2b37e63d0d482d5020b7421ac974cb.jpg)
[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)

R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
随机推荐
2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?
ABAP:OOALV实现增删改查功能
Appium foundation - appium installation (II)
【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
The sorting in C language realizes the number sorting method from small to large
Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
GoogleChromePortable 谷歌chrome浏览器便携版官网下载方式
[Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)
The width of the picture in rich text used by wechat applet exceeds the problem
selenium驱动IE常见问题解决Message: Currently focused window has been closed.
Background and current situation of domestic CDN acceleration
ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station
C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
C language - Blue Bridge Cup - Snake filling
How to implement cross domain requests
Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
Overview of convolutional neural network structure optimization
Realize IIC data / instruction interaction with micro batg135
Tar source code analysis 9
4G wireless all network solar hydrological equipment power monitoring system bms110