mysql innodb count_mysql中innodb表的count

其实就一个问题:count(1),count(*),count(主键),count(非主键字段)哪个更快?

于是建了一张测试表,插入了600多万条记录。表结构如下:

CREATE TABLE `test` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`c1` varchar(10) NOT NULL,

`c2` varchar(10) NOT NULL,

`c3` varchar(10) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDBAUTO_INCREMENT=6003060DEFAULTCHARSET=utf8

看看下面三条sql:

1.select count(1) from test;

2.select count(*) from test;

3.select count(id) from test;

4.select count(c1) from test;

下面是命令行下的执行情况:

mysql>select count(1) from test;

+----------+

| count(1) |

+----------+

|  6003058 |

+----------+

1 row in set (1.74 sec)

mysql>select count(*) from test;

+----------+

| count(*) |

+----------+

|  6003058 |

+----------+

1 row in set (1.75 sec)

mysql>select count(id) from test;

+-----------+

| count(id) |

+-----------+

|   6003058 |

+-----------+

1 row in set (1.87 sec)

mysql>select count(c1) from test;

+-----------+

| count(c1) |

+-----------+

|   6003058 |

+-----------+

1 row in set (2.02 sec)

从上面的执行时间看,前两条相差不多,第三条次之,最差的是第四条,看看explain的结果:

mysql>explain select count(1) from test;

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

|  1 | SIMPLE      | test  | index | NULL          | PRIMARY | 4       | NULL | 6003369 | Using index |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

1 row in set (0.00 sec)

mysql>explain select count(*) from test;

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

|  1 | SIMPLE      | test  | index | NULL          | PRIMARY | 4       | NULL | 6003369 | Using index |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

1 row in set (0.00 sec)

mysql>explain select count(id) from test;

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

|  1 | SIMPLE      | test  | index | NULL          | PRIMARY | 4       | NULL | 6003369 | Using index |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

1 row in set (0.00 sec)

mysql>explain select count(c1) from test;

+----+-------------+-------+------+---------------+------+---------+------+---------+-------+

| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra |

+----+-------------+-------+------+---------------+------+---------+------+---------+-------+

|  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL | 6003369 |       |

+----+-------------+-------+------+---------------+------+---------+------+---------+-------+

1 row in set (0.00 sec)

由上可以看出,前三者的执行计划是一样的,第四条与前三者不同,没有使用覆盖索引,从执行时间上排序为:

count(1)=count(*) < count(主键) < count(非主键字段)

上面是未指定条件的情况,如果加上条件呢,看看下面三条语句:

1.select count(1) from test where id>0;

2.select count(*) from test where id>0;

3.select count(id) from test where id>0;

4.select count(c1) from test where id>0;

下面是命令行下的执行情况:

mysql>select count(1) from test where id>0;

+----------+

| count(1) |

+----------+

|  6003058 |

+----------+

1 row in set (2.07 sec)

mysql>select count(*) from test where id>0;

+----------+

| count(*) |

+----------+

|  6003058 |

+----------+

1 row in set (2.07 sec)

mysql>select count(id) from test where id>0;

+-----------+

| count(id) |

+-----------+

|   6003058 |

+-----------+

1 row in set (2.07 sec)

mysql>select count(c1) from test where id>0;

+-----------+

| count(c1) |

+-----------+

|   6003058 |

+-----------+

1 row in set (2.28 sec)

从上面的执行时间可以看到:

1.count(1),count(*),count(主键)在执行时间上是差不多的,没有明显的差别

2.count(非主键字段)的执行时间明显要比前三者长。

再看一下explain的结果:

mysql>explain select count(1) from test where id>0;

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra                    |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

|  1 | SIMPLE      | test  | range | PRIMARY       | PRIMARY | 4       | NULL | 3001684 | Using where; Using index |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

1 row in set (0.00 sec)

mysql>explain select count(*) from test where id>0;

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra                    |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

|  1 | SIMPLE      | test  | range | PRIMARY       | PRIMARY | 4       | NULL | 3001684 | Using where; Using index |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

1 row in set (0.00 sec)

mysql>explain select count(id) from test where id>0;

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra                    |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

|  1 | SIMPLE      | test  | range | PRIMARY       | PRIMARY | 4       | NULL | 3001684 | Using where; Using index |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+--------------------------+

1 row in set (0.00 sec)

mysql>explain select count(c1) from test where id>0;

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

|  1 | SIMPLE      | test  | range | PRIMARY       | PRIMARY | 4       | NULL | 3001684 | Using where |

+----+-------------+-------+-------+---------------+---------+---------+------+---------+-------------+

1 row in set (0.00 sec)

从上面的explain可以看到,前三种情况都会发生using index,即覆盖索引。

Using index:列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的,这发生在对表的全部的请求列都是同一个索引的部分的时候

而最后的情况不会覆盖索引,会进行表读取,故速度会比前三者要慢,耗时更长就能理解了。

结论:

1.在mysql中,对于表类型innodb的情况,如果无条件,count(1),count(*)没有区别。

2.如果有条件,则count(1),count(*),count(主键)没有区别

3.对于count(非主键字段)的情况因为没有使用覆盖索引,耗时会比前三者更长,应避免这种写法。

4.为了安全起见,建议只使用count(1)或count(*)进行统计,两者完全等价。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值