当前位置:网站首页>MySQL实战45讲 学习笔记(七)
MySQL实战45讲 学习笔记(七)
2022-01-15 02:19:37 【ordinaryBlog】
MySQL学习笔记
count(*) 速度问题
- MyISAM 引擎把一个表的总行数存在了磁盘上,因此执行 count() 的时候会直接返回这个数
- InnoDB 引擎执行 count() 的时候,需要把数据一行一行地从引擎里面读出来,然后累积计数
innodb由于多版本并发控制(MVCC)的原因,多个事务count的行数不同,所以不能保存总行数。
但count(*)做了优化,引擎会选择最小的普通索引树,来计数。而不是直接统计聚集索引树。
提高count性能
用缓存系统保存计数
缺点:
- 缓存会丢失
- 缓存不准确,因为缓存计数和插入数据不是原子操作,有可能在中间过程,其他事务读取了数据。
在数据库中计数
可以解决一致性视图问题
不同count用法
- 对于 count(主键 id) 来说,InnoDB 引擎会遍历整张表,把每一行的 id 值都取出来,返回 server 层。server 层拿到 id 后,判断是不可能为空的,就按行累加。
- 对于 count(1) 来说,InnoDB 引擎遍历整张表,但不取值。server 层对于返回的每一行,放一个数字“1”进去,判断是不可能为空的,按行累加。
count(1) 执行得要比 count(主键 id) 快。从引擎返回 id 会涉及到解析数据行,以及拷贝字段值的操作 - 对于 count(字段) 来说:
如果这个“字段”是定义为 not null 的话,一行行地从记录里面读出这个字段,判断不能为 null,按行累加;
如果这个“字段”定义允许为 null,那么执行的时候,判断到有可能是 null,还要把值取出来再判断一下,不是 null 才累加。 - count(),并不会把全部字段取出来,而是专门做了优化,不取值。count() 肯定不是 null,按行累加
按照效率排序的话,count(字段)<count(主键 id<count(1)≈count(),所以建议尽量使用 count()。
版权声明
本文为[ordinaryBlog]所创,转载请带上原文链接,感谢
https://blog.csdn.net/kang2411212/article/details/120814797
边栏推荐
- Problème avec les demandes inter - domaines ne portant pas de cookies
- 数据分析八大模型:OGSM模型
- Appel asynchrone, Multithreading
- Android中的羊角符,面试看这个就够了
- Compréhension approfondie du Multithreading
- Utilisation de is et as
- Classe générique, interface générique
- Classe générique, héritage de l'interface générique, délégué
- Exercice de base de données d'accès
- Accès à la base de données SQL avec Multithreading, invoke et action
猜你喜欢
-
Écrire et tester le Protocole Modbus
-
. net how to connect to Youxuan database?
-
Splitting e-commerce system into micro service
-
Écrire un programme winform en utilisant plusieurs threads
-
Déclarations SQL couramment utilisées
-
Utilisez le texte. Json analyse le fichier json
-
Plusieurs adresses de nuget
-
Lire Modbus TCP avec nmodbus
-
Module 6 operation of the actual combat camp
-
TypeError: Object of type ‘TrackedArray‘ is not JSON serializable
随机推荐
- The world is always hostile to good people.
- Re regular matching findall (. +?) Match any content that conforms to a certain format (regular matching catch bullet screen)
- Android中的羊角符,面試看這個就够了
- 數據分析八大模型:OGSM模型
- La corne d'agneau d'Android, c'est assez pour l'interview
- Huit modèles d'analyse des données: modèle ogsm
- Exemple d'application de linq
- Utilisez S7. Net communication library
- Écrire La Bibliothèque de communication Modbus TCP
- Lire le profil INI
- Utilisez S7. Net read Siemens 1500plc
- Halcon joint C # Programming Experiment
- Utiliser nmodbus4 pour lire les données à la fois RTU et TCP
- Tiktok Data Analysis options Platform - tichoo Data
- MySQL review: create tables, MySQL data types, primary key constraints, primary key
- Linear Algebra: matrix review
- Review of Linear Algebra: determinant
- The digital RMB cross-border payment test has been continuously promoted, and mainland residents can also shop in Hong Kong in the future
- Thesis classification and writing basis
- YC Framework version update: v1.0 zero point two
- Analyse des données tichoo
- Tiktok data analysis platform
- Partage de l'industrie | tichoo Data to attend 2022 Overseas Short video Industry Summit
- [ticoo Information Station] tiktok and Cross - Border E - commerce Weekly Report
- Options d'analyse des données ticoo {infostation}
- Partage de l'industrie | Lu Shidong, PDG de tichoo Data Outlook Global Video e - commerce future Blueprint
- [ticoo Information Station]
- Noël Black Five
- YC Framework version update: v1.0 zero point two
- Lucene分词器
- Gbase 8A slow SQL optimization case
- 微服务系列--聊聊微服务治理中的一些感悟
- 线程池的经典应用场景
- [web security from getting started to giving up] 07_ Insecure file download and upload vulnerability
- 如何落地一款重试组件
- 一起聊聊设计原则
- 大话Redis系列--深入探讨多路复用(上)
- 大话Redis系列--实战案例总结(下)
- 大话Redis系列--实战案例总结(上)
- JVM系列 -- G1与低延迟垃圾收集器