mysql 中select count1_MySQL select count(*)、select count(1)效率对比

在开发过程中,遇到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),效率相差不大

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值