当前位置:网站首页>Mysql -count :count(1)、count(*)、count(列名)的区别
Mysql -count :count(1)、count(*)、count(列名)的区别
2022-07-26 20:20:00 【云闲不收】
先上结论
执行效果上:
- count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略为NULL的值。
- count(1)用1代表代码行,在统计结果的时候,不会忽略为NULL的值。
- count(列名)只包括列名那一列,在统计结果的时候,会忽略列值为空(这里的空不是指空字符串或者0,而是表示null)的计数,即某个字段值为NULL时,不统计。
执行效率上:
- 列名为主键,count(列名)会比count(1)快
- 列名不为主键,count(1)会比count(列名)快
- 如果表多个列并且没有主键,则 count(1)的执行效率优于 count(*)
- 如果有主键,则 select count(主键)的执行效率是最优的
- 如果表只有一个字段,则 select count(*)最优。
具体分析
1. count(1) and count(*)
从执行计划来看,count(1)和count(*)的效果是一样的。
当表的数据量大些时,对表作分析之后,使用count(1)还要比使用count(*)用时多!
当数据量在1W以内时,count(1)会比count(*)的用时少些,不过也差不了多少。
如果count(1)是聚集索引时,那肯定是count(1)快,但是差的很小。
因为count(),会自动优化指定到那一个字段。所以没必要去count(1),使用count(),sql会帮你完成优化的
因此:在有聚集索引时count(1)和count(*)基本没有差别!
2. count(1) and count(字段)
两者的主要区别是:
count(1) 会统计表中的所有的记录数,包含字段为null 的记录。
count(字段) 会统计该字段在表中出现的次数,忽略字段为null 的情况。即不统计字段为null 的记录。
边栏推荐
- 6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
- Opencv DNN deployment onnx model
- Interceptors
- How to create a static object variable of its own class?
- 测试用例千万不能随便,记录由一个测试用例异常引起的思考
- 详细图解b树及C语言实现
- 【Oracle实训】-部署号称零停机迁移的OGG
- [Oracle training] - deploy Ogg known as zero downtime migration
- Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro
- 微服务化解决文库下载业务问题实践
猜你喜欢

没有网络怎么配置传奇SF登陆器自动读取列表

Error in render: “TypeError: data.slice is not a function“

苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条
Monitor MySQL based on MySQL exporter

How to implement Devops with automation tools | including low code and Devops application practice

传奇GEE引擎版本如何封挂?通过脚本+引擎封玩家账号教程

LeetCode链表问题——19.删除链表的倒数第N个节点(一题一文学会链表)

手机\固定电话座机呼叫转移设置方法

09_ UE4 advanced_ Enter the next level and reserve the blood volume

Practice of microservice in solving Library Download business problems
随机推荐
TableWidget
2022-7-26 the seventh group of abstractions and interfaces
Today, the company came across an Alibaba P8, which was really seen as the ceiling of the foundation
What is the role of cache in the storage system of data blocks?
如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
Kotlin - coroutinecontext
修改excel默认编码
今天公司碰到一个阿里p8,算是真正见识到了基础的天花板
BTC和ETH不确定性增强 因加息逼近?美国经济将面临更多痛苦
Buu brush inscription 4
Leetcode linked list class
传奇GEE引擎版本如何封挂?通过脚本+引擎封玩家账号教程
leetcode 哈希表类
牛客刷题——Mysql系列
游览器——游览器游览器缓存
In the era of Web3.0, the technical theory of implementing a DAPP based on P2P DB
Detailed illustration of B-tree and its implementation in C language
09_ue4进阶_进入下一关并保留血量
GOM登录器配置免费版生成图文教程
AI technology, simplifying the complex world | teatalk online application practical series, issue 2