MySQL select count(*)、select count(1)效率对比
2011-06-11 16:40
489 查看
在开发过程中,遇到select count优化问题,现结果记录如下:
1、不加条件的select count(*)与select count(*)对比
mysql> select count(*) from test;
+----------+
| count(*) |
+----------+
| 60900 |
+----------+
1 row in set (0.05 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 | Idx_test
| 9 | NULL | 108653 | Using index |
+----+-------------+----------------+-------+---------------+-------------------
-+---------+------+--------+-------------+
1 row in set (0.00 sec)
mysql> select count(1) from test;
+----------+
| count(1) |
+----------+
| 60900 |
+----------+
1 row in set (0.05 sec)
执行计划:
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 | Idx_test
| 9 | NULL | 108653 | Using index |
+----+-------------+----------------+-------+---------------+-------------------
-+---------+------+--------+-------------+
select count(*) 于 select count(1)在不加条件的情况下效率相同,同时使用了索引。
2、增加条件查询:
mysql> select count(*) from test where user_id = 0;
+----------+
| count(*) |
+----------+
| 34 |
+----------+
1 row in set (5.33 sec)
执行计划:
mysql> explain select count(*) from test where user_id = 0;
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | re
f | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NU
LL | 77009 | Using where |
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
1 row in set (0.00 sec)
mysql> select count(1) from test where user_id = 0;
+----------+
| count(1) |
+----------+
| 34 |
+----------+
1 row in set (5.20 sec)
执行计划:
mysql> explain select count(1) from test where user_id = 0;
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | re
f | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NU
LL | 77009 | Using where |
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
1 row in set (0.00 sec)
select count(*)与select count(1)在增加条件的情况下使用了全表扫表
3、为where条件增加索引
mysql> create index user_id on test(user_id);
mysql> select count(*) from test where user_id = 0;
+----------+
| count(*) |
+----------+
| 34 |
+----------+
1 row in set (0.00 sec)
执行计划:
mysql> explain select count(*) from test where user_id = 0;
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len |
ref | rows | Extra |
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
| 1 | SIMPLE | test | ref | user_id | user_id | 9 |
const | 34 | Using where; Using index |
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
1 row in set (0.00 sec)
mysql> select count(1) from test where user_id = 0;
+----------+
| count(1) |
+----------+
| 34 |
+----------+
1 row in set (0.00 sec)
执行计划:
mysql> explain select count(1) from test where user_id = 0;
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len |
ref | rows | Extra |
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
| 1 | SIMPLE | test | ref | user_id | user_id | 9 |
const | 34 | Using where; Using index |
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
1 row in set (0.00 sec)
select count(*) 与select count(*)在为条件建立索引的情况加,使用索引,效率提高。
总结:在不使用where的情况下默认使用主键索引,如使用where,则需合理创建索引提高效率。同时测试了select count(col),效率相差不大
1、不加条件的select count(*)与select count(*)对比
mysql> select count(*) from test;
+----------+
| count(*) |
+----------+
| 60900 |
+----------+
1 row in set (0.05 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 | Idx_test
| 9 | NULL | 108653 | Using index |
+----+-------------+----------------+-------+---------------+-------------------
-+---------+------+--------+-------------+
1 row in set (0.00 sec)
mysql> select count(1) from test;
+----------+
| count(1) |
+----------+
| 60900 |
+----------+
1 row in set (0.05 sec)
执行计划:
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 | Idx_test
| 9 | NULL | 108653 | Using index |
+----+-------------+----------------+-------+---------------+-------------------
-+---------+------+--------+-------------+
select count(*) 于 select count(1)在不加条件的情况下效率相同,同时使用了索引。
2、增加条件查询:
mysql> select count(*) from test where user_id = 0;
+----------+
| count(*) |
+----------+
| 34 |
+----------+
1 row in set (5.33 sec)
执行计划:
mysql> explain select count(*) from test where user_id = 0;
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | re
f | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NU
LL | 77009 | Using where |
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
1 row in set (0.00 sec)
mysql> select count(1) from test where user_id = 0;
+----------+
| count(1) |
+----------+
| 34 |
+----------+
1 row in set (5.20 sec)
执行计划:
mysql> explain select count(1) from test where user_id = 0;
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | re
f | rows | Extra |
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NU
LL | 77009 | Using where |
+----+-------------+----------------+------+---------------+------+---------+---
---+-------+-------------+
1 row in set (0.00 sec)
select count(*)与select count(1)在增加条件的情况下使用了全表扫表
3、为where条件增加索引
mysql> create index user_id on test(user_id);
mysql> select count(*) from test where user_id = 0;
+----------+
| count(*) |
+----------+
| 34 |
+----------+
1 row in set (0.00 sec)
执行计划:
mysql> explain select count(*) from test where user_id = 0;
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len |
ref | rows | Extra |
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
| 1 | SIMPLE | test | ref | user_id | user_id | 9 |
const | 34 | Using where; Using index |
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
1 row in set (0.00 sec)
mysql> select count(1) from test where user_id = 0;
+----------+
| count(1) |
+----------+
| 34 |
+----------+
1 row in set (0.00 sec)
执行计划:
mysql> explain select count(1) from test where user_id = 0;
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len |
ref | rows | Extra |
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
| 1 | SIMPLE | test | ref | user_id | user_id | 9 |
const | 34 | Using where; Using index |
+----+-------------+----------------+------+---------------+---------+---------+
-------+------+--------------------------+
1 row in set (0.00 sec)
select count(*) 与select count(*)在为条件建立索引的情况加,使用索引,效率提高。
总结:在不使用where的情况下默认使用主键索引,如使用where,则需合理创建索引提高效率。同时测试了select count(col),效率相差不大
相关文章推荐
- MySQL的统计总数count(*)与count(id)或count(字段)的之间的各自效率性能对比
- VC+ADO+MySQL执行select count(*)效率问题
- Mysql 优化之 select count 效率
- mysql select count(*) 与 select count(id)效率比较
- mysql中select count()效率简析
- MySQL的统计总数count(*)与count(id)或count(字段)的之间的各自效率性能对比
- mysql中count(*),distinct的使用方法和效率研究
- mysql从innodb转到MyIsam的count查询效率极大提升
- mysql技巧之select count的区别分析
- Mysql中count(*),DISTINCT的使用方法和效率研究
- mysql select count(filed) 问题(where条件没有数据匹配的话也有数据返回)。
- MySQL select * 和把所有的字段都列出来,哪个效率更高?
- mysql SELECT FOUND_ROWS()与COUNT(*)用法区别
- mysql_num_rows VS COUNT 效率问题分析
- mysql大数据插入效率对比
- mysql_num_rows($result)和select count()的区别
- MongoDB批量操作及与MySQL效率对比
- MySQL优化之COUNT(*)效率
- [MySQL FAQ]系列 -- 为何innodb表select count(*)很慢
- Mysql中count(*),DISTINCT的使用方法和效率研究