count(1)、count(*)与count(列名)的执行区别
来源:互联网 发布:大数据培训多少钱 达内 编辑:程序博客网 时间:2024/06/07 18:24
执行效果:
1. count(1) and count(*)
当表的数据量大些时,对表作分析之后,使用count(1)还要比使用count(*)用时多了!
从执行计划来看,count(1)和count(*)的效果是一样的。 但是在表做过分析之后,count(1)会比count(*)的用时少些(1w以内数据量),不过差不了多少。
如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。
因为count(*),自动会优化指定到那一个字段。所以没必要去count(1),用count(*),sql会帮你完成优化的 因此:count(1)和count(*)基本没有差别!
2. count(1) and count(字段)
两者的主要区别是
(1) count(1) 会统计表中的所有的记录数,包含字段为null 的记录。
(2) count(字段) 会统计该字段在表中出现的次数,忽略字段为null 的情况。即不统计字段为null 的记录。
转自:http://www.cnblogs.com/Dhouse/p/6734837.html
count(*) 和 count(1)和count(列名)区别
执行效果上:
count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULL
count(1)包括了忽略所有列,用1代表代码行,在统计结果的时候,不会忽略列值为NULL
count(列名)只包括列名那一列,在统计结果的时候,会忽略列值为空(这里的空不是只空字符串或者0,而是表示null)的计数,即某个字段值为NULL时,不统计。
count(1)包括了忽略所有列,用1代表代码行,在统计结果的时候,不会忽略列值为NULL
count(列名)只包括列名那一列,在统计结果的时候,会忽略列值为空(这里的空不是只空字符串或者0,而是表示null)的计数,即某个字段值为NULL时,不统计。
执行效率上:
列名为主键,count(列名)会比count(1)快
列名不为主键,count(1)会比count(列名)快
如果表多个列并且没有主键,则 count(1) 的执行效率优于 count(*)
如果有主键,则 select count(主键)的执行效率是最优的
如果表只有一个字段,则 select count(*)最优。
转自:http://eeeewwwqq.iteye.com/blog/1972576
实例分析
mysql> create table counttest(name char(1), age char(2));Query OK, 0 rows affected (0.03 sec)mysql> insert into counttest values -> ('a', '14'),('a', '15'), ('a', '15'), -> ('b', NULL), ('b', '16'), -> ('c', '17'), -> ('d', null), ->('e', '');Query OK, 8 rows affected (0.01 sec)Records: 8 Duplicates: 0 Warnings: 0mysql> select * from counttest;+------+------+| name | age |+------+------+| a | 14 || a | 15 || a | 15 || b | NULL || b | 16 || c | 17 || d | NULL || e | |+------+------+8 rows in set (0.00 sec)mysql> select name, count(name), count(1), count(*), count(age), count(distinct(age)) -> from counttest -> group by name;+------+-------------+----------+----------+------------+----------------------+| name | count(name) | count(1) | count(*) | count(age) | count(distinct(age)) |+------+-------------+----------+----------+------------+----------------------+| a | 3 | 3 | 3 | 3 | 2 || b | 2 | 2 | 2 | 1 | 1 || c | 1 | 1 | 1 | 1 | 1 || d | 1 | 1 | 1 | 0 | 0 || e | 1 | 1 | 1 | 1 | 1 |+------+-------------+----------+----------+------------+----------------------+5 rows in set (0.00 sec)
额外参考资料:http://blog.csdn.net/lihuarongaini/article/details/68485838
阅读全文
0 0
- count(1)、count(*)与count(列名)的执行区别
- COUNT(*)与COUNT(列名)的区别
- Oracle ——count(*) 与count(列名)的区别
- count(*) 和 count(1)和count(列名)区别
- count(*) 和 count(1)和count(列名)区别
- select count(*) 和 count(1),count(列名)区别
- count(*) 和 count(1)和count(列名)区别
- count(1),count(*)和count(列名)三者的区别
- sql中count(0),count(1),count(),count(列名)
- count(*),count(1)和count(列)的区别
- count(列名)与count(*)说明
- count(*)与count(1)与count(column)的区别
- count(*)与count(1)的区别
- count(1)、count(*)与count(col)的区别
- java07_几张图了解count(*)与count(列名)的区别
- count(*),count(1),count(col)的区别
- count(*) count(1) count(column)的区别
- count(*)、count(1)、count(column)的区别
- 6-1 循环单链表区间删除(15 分)
- PCA数学原理
- Java 枚举
- redis在centos的安装
- APP如何适配不同的Android版本
- count(1)、count(*)与count(列名)的执行区别
- 【LOJ】 1138 Trailing Zeroes (III)
- OpenLayers3学习三:地图旋转及地图跳转到某一点的方式(以类为接口)
- 实例分析Java class文件内容
- Python:Set的运算
- 调戏源码之Spring MVC(一)
- Tensorflow Optimizer
- Windows Practice_文件_文件基础操作
- 堆排序C语言实现