表结构如下:

mysql> show create table user\G;
*************************** 1. row ***************************
Table: user
Create Table: CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`pwd` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`phone` varchar(30) NOT NULL,
`sex` enum('F','M','N') NOT NULL DEFAULT 'N',
`addres` varchar(100) NOT NULL,
`tag` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=5000003 DEFAULT CHARSET=utf8 COMMENT='用户表'
1 row in set (0.00 sec)

下面做一下explain: 1、count(id)

mysql> select count(id) from user;
+-----------+
| count(id) |
+-----------+
| 5000002 |
+-----------+
1 row in set (1.93 sec)
mysql> explain select count(id) from user;
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type  | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | user | index | NULL | name | 152 | NULL | 4998401 | Using index |
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
1 row in set (0.05 sec)

2、count(1)

mysql> select count(1) from user;
+----------+
| count(1) |
+----------+
| 5000002 |
+----------+
1 row in set (0.90 sec)
mysql> explain select count(1) from user;
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | user | index | NULL | name | 152 | NULL | 4998401 | Using index |
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

3、count(*)

mysql> select count(*) from user;
+----------+
| count(*) |
+----------+
| 5000002 |
+----------+
1 row in set (0.87 sec)
mysql> explain select count(*) from user;
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | user | index | NULL | name | 152 | NULL | 4998401 | Using index |
+----+-------------+-------+-------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

比较三个查询,explain的结果一模一样,这说明这三个的效率是一样的吗?

再看看下面三个操作,带上where条件 sex='F',以下三个操作中间均会重启mysql服务。

1、count(id)

mysql> select count(id) from user where sex='F';
+-----------+
| count(id) |
+-----------+
| 1681259 |
+-----------+
1 row in set (18.87 sec)
mysql> explain select count(id) from user where sex='F';
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | user | ALL | NULL | NULL | NULL | NULL | 4998401 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

2、count(1)

mysql> select count(1) from user where sex='F';
+----------+
| count(1) |
+----------+
| 1681259 |
+----------+
1 row in set (4.81 sec)
mysql> explain select count(1) from user where sex='F';
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | user | ALL | NULL | NULL | NULL | NULL | 4998401 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

3、count(*)

mysql> select count(*) from user where sex='F';
+----------+
| count(*) |
+----------+
| 1681259 |
+----------+
1 row in set (4.69 sec)
mysql> explain select count(*) from user where sex='F';
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | user | ALL | NULL | NULL | NULL | NULL | 4998401 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

以上三种查询有一些差别,其中count(id)用时最长,count(*)比count(1)速度要稍微快一点。

两组查询,带条件的都没有使用到索引,扫描了全表;而没有条件的则使用了索引name。

所以在应用中尽量不使用count(*)和count(1),杜绝使用count(primary_key)。

网上有很多资料说

没有主键,count(1)比count(*)快;

有主键的话,count(primary_key)最快,但是在上面的测试中发现,count(primary_key)是最慢的,难道是测试不准确?这个有待验证。

如果表只有一个字段,则count(*)是最快的。

说明:

count(1)中的1并不是指第一个column;

count(*)和count(1)一样,包括对值为NULL的统计;

count(column)不包括对值为NULL的统计,这里的column指的不是primary_key;

本文出自 寄凡,http://www.hblpf.com/?post=55

mysql中的count(primary_key)、count(1)、count(*)的区别的更多相关文章

  1. MySQL 中 key, primary key ,unique key,index的区别

    一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id int(11) NOT NULL auto_increment, user_n ...

  2. 搞定面试官 - 可以介绍一下在 MySQL 中你平时是怎么使用 COUNT() 的嘛?

    大家好,我是程序员啊粥. 相信在大家的工作中,有很多的功能都需要用到 count(*) 来统计表中的数据行数.同时,对于一些大数据的表,用 count 都是瑟瑟发抖,往往会结合缓存等进行处理. 那么, ...

  3. 【转】Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...

  4. mysql中时间类型datetime,timestamp与int的区别

    在mysql中存储时间,我们可以用datetime 格式,timestamp格式,也可以用int格式.那么我们设计的时候该如何考虑呢? 首先,我觉得应该明白这几个格式究竟是如何的,然后看看他们的区别, ...

  5. Mysql中的排序规则utf8_unicode_ci、utf8_general_ci的区别总结

    Mysql中utf8_general_ci与utf8_unicode_ci有什么区别呢?在编程语言中,通常用unicode对中文字符做处理,防止出现乱码,那么在MySQL里,为什么大家都使用utf8_ ...

  6. MYSQL中的普通索引,主健,唯一,全文索引区别

    MYSQL索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的记 ...

  7. 浅谈SQL Server、MySQL中char,varchar,nchar,nvarchar区别

    最近一次的面试中,被面试官问到varchar和nvarchar的区别,脑海里记得是定长和可变长度的区别,但却没能说出来.后来,在网上找了下网友总结的区别.在这里做个备忘录: 一,SQL Server中 ...

  8. mysql 中execute、executeQuery和executeUpdate之间的区别

    在用纯JSP做一个页面报警功能的时候习惯性的用executeQuery来执行SQL语句,结果执行update时就遇到问题,语句能执行,但返回结果出现问题,另外还忽略了executeUpdate的返回值 ...

  9. mysql中char,varchar与text类型的区别和选用

    关于char,varchar与text平时没有太在意,一般来说,可能现在大家都是用varchar.但是当要存储的内容比较大时,究竟是选择varchar还是text呢?不知道...... 于是去查阅了一 ...

  10. mysql中几个日期时间类型之间的区别和使用

    MySQL中有如下几个时间类型:date.time.datetime.timestamp.year MySQL数据类型           含义 date                     只存 ...

随机推荐

  1. 一入python深似海--dict(字典)的一种实现

    以下是python中字典的一种实现.用list数据结构实现字典.详细是这种:[[(key1,value1),(key2,value2),...],[],[],...] 内部每个hash地址是一个lis ...

  2. 如何在Swift里用UnsafeMutablePointer

    下午在适配iPadUI的时候,用到了UIPopoverPresentationController,然后在转屏的时候需要调用UIPopoverPresentationControllerDelegat ...

  3. Python的基础--对象 转

      对象(Objects)是python中数据的抽象,python中所有的数据均可以用对象或者是对象之间的关系来表示.每个对象均有标识符(identity).类型(type).值(value). 标识 ...

  4. focusky 购买指南

    升级Focusky动画演示大师 所有版本一次购买,终身使用,无限制作,免费升级.支付方式:支付宝.淘宝.银行转账.支付宝付款:点击表格中的“立即购买“进入购买页面->选择版本.数量,并填写详细的 ...

  5. jquery 设置style:display 其实很方便的

    ("#id").css('display','none'); $("#id").css('display','block'); 或 $("#id&qu ...

  6. K.Bro Sorting

    Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total Submissio ...

  7. A - Robot Bicorn Attack

    Description Vasya plays Robot Bicorn Attack. The game consists of three rounds. For each one a non-n ...

  8. VC操作Excel之基本操作

    // 变量的定义 _Application app; Workbooks books; _Workbook book; Worksheets sheets; _Worksheet sheet; Ran ...

  9. [未完成]关于JavaScript总结

    jsp服务端,js客户端. javascript 是基于对象和事件驱动的脚本语言. 特点: 交互性 安全性(不允许直接访问本地硬盘) 跨平台性(只要是可以解析java的浏览器都可以执行,和平台无关) ...

  10. Linux下is not in the sudoers file解决方法

    最近在学习linux,在某个用户(xxx)下使用sudo的时候,提示以下错误:xxx is not in the sudoers file. This incident will be reporte ...