搜索
查看: 1290|回复: 0

【MySql】Mysql-SQL优化-统计某种类型的个数

[复制链接]

183

主题

8

回帖

820

积分

高级会员

积分
820
发表于 2014-11-10 17:27:20 | 显示全部楼层 |阅读模式
有时我们想统计某种类型有多少个,会用这个SQL。全表扫描之余,还要filesort,耗时1.34秒。
  1. mysql>  select country,count(*) from t1 group by country;
  2. +---------+----------+
  3. | country | count(*) |
  4. +---------+----------+
  5. | NULL    |       32 |
  6. | africa  |   524288 |
  7. | america |   524288 |
  8. | china   |   524288 |
  9. +---------+----------+
  10. 4 rows in set (1.34 sec)

  11. mysql> desc select country,count(*) from t1 group by country;
  12. +----+-------------+-------+------+---------------+------+---------+------+---------+---------------------------------+
  13. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra                           |
  14. +----+-------------+-------+------+---------------+------+---------+------+---------+---------------------------------+
  15. |  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL | 1573382 | Using temporary; Using filesort |
  16. +----+-------------+-------+------+---------------+------+---------+------+---------+---------------------------------+
  17. 1 row in set (0.00 sec)
复制代码
  1. mysql> select count(country='africa' or null) as africa,count(country='america' or null) as america, count(country='china' or null) as china from t1;
  2. +--------+---------+--------+
  3. | africa | america | china  |
  4. +--------+---------+--------+
  5. | 524288 |  524288 | 524288 |
  6. +--------+---------+--------+
  7. 1 row in set (0.78 sec)

  8. mysql> desc select count(country='africa' or null) as africa,count(country='america' or null) as america, count(country='china' or null) as china from t1;
  9. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+
  10. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra |
  11. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+
  12. |  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL | 1573382 |       |
  13. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+
  14. 1 row in set (0.00 sec)



  15. mysql> select sum(country='africa') as africa ,sum(country='america') as america,sum(country='china') from t1;
  16. +--------+---------+----------------------+
  17. | africa | america | sum(country='china') |
  18. +--------+---------+----------------------+
  19. | 524288 |  524288 |               524288 |
  20. +--------+---------+----------------------+
  21. 1 row in set (0.86 sec)

  22. mysql> desc select sum(country='africa') as africa ,sum(country='america') as america,sum(country='china') from t1;
  23. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+
  24. | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows    | Extra |
  25. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+
  26. |  1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL | 1573382 |       |
  27. +----+-------------+-------+------+---------------+------+---------+------+---------+-------+
  28. 1 row in set (0.00 sec)
复制代码
  1. 版本mysql5.5.30
复制代码



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

 
 
大数据行业交流
大数据行业交流
大数据求职招聘
大数据求职招聘
站长电话:
15010106923
微信联系:
hb-0310
站长邮箱:
ab12-120@163.com
大数据中国微信

QQ   

版权所有: Discuz! © 2001-2013 大数据.

GMT+8, 2024-5-2 10:33 , Processed in 0.087991 second(s), 24 queries .

快速回复 返回顶部 返回列表