有时我们想统计某种类型有多少个,会用这个SQL。

全表扫描之余,还要filesort。耗时1.34秒。
 
mysql>  select country,count(*) from t1 group by country;
+---------+----------+
| country | count(*) |
+---------+----------+
| NULL | 32 |
| africa | 524288 |
| america | 524288 |
| china | 524288 |
+---------+----------+
4 rows in set (1.34 sec) mysql> desc select country,count(*) from t1 group by country;
+----+-------------+-------+------+---------------+------+---------+------+---------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+---------------------------------+
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 1573382 | Using temporary; Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+---------+---------------------------------+
1 row in set (0.00 sec)
下面是两种优化方法,都是全表扫描,但预计count()要比sum()耗的CPU少点,方法更佳。
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;
+--------+---------+--------+
| africa | america | china |
+--------+---------+--------+
| 524288 | 524288 | 524288 |
+--------+---------+--------+
1 row in set (0.78 sec) 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;
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 1573382 | |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
1 row in set (0.00 sec) mysql> select sum(country='africa') as africa ,sum(country='america') as america,sum(country='china') from t1;
+--------+---------+----------------------+
| africa | america | sum(country='china') |
+--------+---------+----------------------+
| 524288 | 524288 | 524288 |
+--------+---------+----------------------+
1 row in set (0.86 sec) mysql> desc select sum(country='africa') as africa ,sum(country='america') as america,sum(country='china') from t1;
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 1573382 | |
+----+-------------+-------+------+---------------+------+---------+------+---------+-------+
1 row in set (0.00 sec)
版本号mysql5.5.30

Mysql-SQL优化-统计某种类型的个数的更多相关文章

  1. mysql sql优化实例

    mysql sql优化实例 优化前: pt-query-degist分析结果: # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B ...

  2. 18.Mysql SQL优化

    18.SQL优化18.1 优化SQL语句的一般步骤 18.1.1 通过show status命令了解各种SQL的执行频率show [session|global] status; -- 查看服务器状态 ...

  3. Mysql SQL优化&执行计划

    SQL优化准则 禁用select * 使用select count(*) 统计行数 尽量少运算 尽量避免全表扫描,如果可以,在过滤列建立索引 尽量避免在where子句对字段进行null判断 尽量避免在 ...

  4. MySQL sql优化(摘抄自文档)

    前言 有人反馈之前几篇文章过于理论缺少实际操作细节,这篇文章就多一些可操作性的内容吧. 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需要有一些索引知识为基础. 优化目标 ...

  5. MySQL SQL优化

    一.优化数据库的一般步骤: (A) 通过 show status 命令了解各种SQL的执行频率. (B) 定位执行效率较低的SQL语句,方法两种: 事后查询定位:慢查询日志:--log-slow-qu ...

  6. mysql sql优化及注意事项

    sql优化分析 通过slow_log等方式可以捕获慢查询sql,然后就是减少其对io和cpu的使用(不合理的索引.不必要的数据访问和排序)当我们面对具体的sql时,首先查看其执行计划A.看其是否使用索 ...

  7. 《Mysql - SQL优化》

    一:在查询语句时,应该注意的优化问题 - SELECT语句务必指明字段名称 - SELECT * 会增加很多不必要的消耗(CPU.IO.内存.网络带宽) - 同时会让 Mysql 优化器无法优化 -  ...

  8. MySQL SQL优化之in与range查询【转】

    本文来自:http://myrock.github.io/ 首先我们来说下in()这种方式的查询.在<高性能MySQL>里面提及用in这种方式可以有效的替代一定的range查询,提升查询效 ...

  9. Mysql SQL 优化

    1. 查询缓存 多数MySQL服务器都开启了查询缓存,相同的查询被执行多次,查询结果会被放到一个缓存中,这样,后续的相同的查询就不用操作表而直接访问缓存结果了. // 查询缓存不开启 $r = mys ...

随机推荐

  1. 关于TreeView控件的TreeNodeCheckChanged事件无法回发处理

    1.在后台设置属性 TreeView1.Attributes.Add("onclick", "postBackByObject()"); 2.在前台页面中间添加 ...

  2. Vue打包之后部署到 express 服务器上

    Part.1 安装 express npm install express body-parer --save Part.2 在项目根目录下创建 app.js 文件作为启动 express 服务器代码 ...

  3. Jenkins总结(ant+jmeter+java)

    1.jdk与ant都需要在Jenkins-->系统管理-->全局工具配置里面配置各自的安装目录 2.修改Jenkins配置文件后,通过命令行重启: source /etc/profile ...

  4. sql备份

    SELECT id,Name FROM TeachSite GROUP BY id select * from #temp as [type], SchoolRollID,SUM(Chargeable ...

  5. python note of decorator

    def decorate_log(decorate_arg,*args,**kwargs): # 存放装饰器参数 def decorate_wrapper(func,*args,**kwargs): ...

  6. [Luogu] P4910 帕秋莉的手环

    题目背景 帕秋莉是蕾米莉亚很早结识的朋友,现在住在红魔馆地下的大图书馆里.不仅擅长许多魔法,还每天都会开发出新的魔法.只是身体比较弱,因为哮喘,会在咏唱符卡时遇到麻烦. 她所用的属性魔法,主要是生命和 ...

  7. Python中的列表(5)

    1.使用函数 range() 创建一个数字列表 for value in range(1,5): print(value) console: 我们发现,它并不会打印数字5,因为 range() 函数, ...

  8. LeetCode(30) Substring with Concatenation of All Words

    题目 You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  9. 杭电 4883 TIANKENG’s restaurant (求饭店最少需要座椅)

    Description TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of custo ...

  10. 集训第六周 数学概念与方法 UVA 11722 几何概型

    ---恢复内容开始--- http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=31471 题意,两辆火车,分别会在[t1,t2],[ ...