MySQL之查询优化方式(笔记)
1.COUNT()
对COUNT的优化可以通过下面的SQL实现
mysql> select count(gnp<10000 or null) as '<<<<',count(gnp>=10000 or null) as '>>>>' from country;
+------+------+
| <<<< | >>>> |
+------+------+
| 152 | 87 |
+------+------+
1 row in set (0.00 sec)
count(*)与count(cloumn)返回的值可能不一样,因为如果存在空的情况,count(*)也会计算在内
2.SUM()
对SUM()的优化需要通过建立索引实现
mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | MUL | | |
| CountryCode | char(3) | NO | | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.03 sec) mysql> create index popu_index on city(population);
Query OK, 4079 rows affected (0.05 sec)
Records: 4079 Duplicates: 0 Warnings: 0 mysql> explain select SUM(population) as sum from city;
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | city | index | NULL | popu_index | 4 | NULL | 4079 | Using index |
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
1 row in set (0.00 sec) mysql> select SUM(population) as sum from city;
+------------+
| sum |
+------------+
| 1429559884 |
+------------+
1 row in set (0.00 sec)
3.子查询的优化
对于子查询的优化可以通过on实现
mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | MUL | | |
| CountryCode | char(3) | NO | | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | MUL | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.01 sec) mysql> desc country;;
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+--
| Field | Type | Null | Key | Default | E
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+--
| Code | char(3) | NO | PRI | |
| Name | char(52) | NO | | |
| Continent | enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') | NO | | Asia |
| Region | char(26) | NO | | |
| SurfaceArea | float(10,2) | NO | | 0.00 |
| IndepYear | smallint(6) | YES | | NULL |
| Population | int(11) | NO | | 0 |
| LifeExpectancy | float(3,1) | YES | | NULL |
| GNP | float(10,2) | YES | | NULL |
| GNPOld | float(10,2) | YES | | NULL |
| LocalName | char(45) | NO | | |
| GovernmentForm | char(45) | NO | | |
| HeadOfState | char(60) | YES | | NULL |
| Capital | int(11) | YES | | NULL |
| Code2 | char(2) | NO | | |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+--
15 rows in set (0.01 sec) ERROR:
No query specified mysql> explain select t.* from city t where t.countrycode in( select code from country);
+----+-------------+---------+--------+---------------+---------+---------+---------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+--------+---------------+---------+---------+---------------------+------+-------------+
| 1 | SIMPLE | t | ALL | NULL | NULL | NULL | NULL | 4079 | NULL |
| 1 | SIMPLE | country | eq_ref | PRIMARY | PRIMARY | 3 | world.t.CountryCode | 1 | Using index |
+----+-------------+---------+--------+---------------+---------+---------+---------------------+------+-------------+
2 rows in set (0.04 sec) mysql> explain select t.* from city t join country t1 on t.countrycode = t1.code;
+----+-------------+-------+--------+---------------+---------+---------+---------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------------+------+-------------+
| 1 | SIMPLE | t | ALL | NULL | NULL | NULL | NULL | 4079 | NULL |
| 1 | SIMPLE | t1 | eq_ref | PRIMARY | PRIMARY | 3 | world.t.CountryCode | 1 | Using index |
+----+-------------+-------+--------+---------------+---------+---------+---------------------+------+-------------+
2 rows in set (0.00 sec)
这样的优化可能返回重复的行,去重需要使用关键字distinct
4.Group by优化
正常执行情况下如下
mysql> explain select t.* from city t join country t1 on t.countrycode = t1.code group by t.id;
+----+-------------+-------+--------+---------------+---------+---------+---------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+---------------------+------+---------------------------------+
| 1 | SIMPLE | t | ALL | NULL | NULL | NULL | NULL | 4079 | Using temporary; Using filesort |
| 1 | SIMPLE | t1 | eq_ref | PRIMARY | PRIMARY | 3 | world.t.CountryCode | 1 | Using index |
+----+-------------+-------+--------+---------------+---------+---------+---------------------+------+---------------------------------+
2 rows in set (0.02 sec)
可以看到了使用临时表还有文件排序的方式来实现,那么我们应该怎么减少IO已经对资源的消耗呢,通过下面方式可以看出
mysql> explain select t.* from city t join (select code from country) t1 on t.countrycode = t1.code;
+----+-------------+------------+-------+---------------+-------------+---------+---------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+-------------+---------+---------------------+------+-------------+
| 1 | PRIMARY | t | ALL | NULL | NULL | NULL | NULL | 4079 | NULL |
| 1 | PRIMARY | <derived2> | ref | <auto_key0> | <auto_key0> | 3 | world.t.CountryCode | 10 | Using index |
| 2 | DERIVED | country | index | NULL | PRIMARY | 3 | NULL | 239 | Using index |
+----+-------------+------------+-------+---------------+-------------+---------+---------------------+------+-------------+
3 rows in set (0.00 sec)
这样就可以避免了文件排序还有使用该临时表的情况。
5.limit操作方式的优化
使用之前:
mysql> explain select * from city order by name limit 100,10;
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | city | ALL | NULL | NULL | NULL | NULL | 4079 | Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
可以看到使用了文件排序,这样会造成性能上的低下。
优化方式:可以通过主键或者是含有索引的列进行order by 操作;
mysql> create index name_index on city(name); mysql> explain select name from city order by name limit 100,10;
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
| 1 | SIMPLE | city | index | NULL | name_index | 35 | NULL | 110 | Using index |
+----+-------------+-------+-------+---------------+------------+---------+------+------+-------------+
1 row in set (0.00 sec)
可以发现只扫描了35行而且使用了索引但是效率增加了很多。
MySQL之查询优化方式(笔记)的更多相关文章
- WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)
WebAPI调用笔记 前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...
- 常见mysql的慢查询优化方式
一,第一步.开启mysql慢查询 方式一: 修改配置文件 在 my.ini 增加几行: 主要是慢查询的定义时间(超过2秒就是慢查询),以及慢查询log日志记录( slow_query_log) 方 ...
- MYSQL视图的学习笔记
MYSQL视图的学习笔记,学至Tarena金牌讲师,金色晨曦科技公司技术总监沙利穆 课程笔记的综合. 视图及图形化工具 1. 视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚 ...
- MySQL in查询优化
https://blog.csdn.net/gua___gua/article/details/47401621 MySQL in查询优化<一> 原创 2015年08月10日 17:57: ...
- 查询优化 | MySQL慢查询优化
Explain查询:rows,定位性能瓶颈. 只需要一行数据时,使用LIMIT1. 在搜索字段上建立索引. 使用ENUM而非VARCHAR. 选择区分度高的列作为索引. 采用扩展索引,而不是新建索引 ...
- MySQL 慢查询优化
为什么查询速度会慢 1.慢是指一个查询的响应时间长.一个查询的过程: 客户端发送一条查询给服务器 服务器端先检查查询缓存,如果命中了缓存,则立可返回存储在缓存中的结果.否则进入下一个阶段 服务器端进行 ...
- 《MySQL慢查询优化》之SQL语句及索引优化
1.慢查询优化方式 服务器硬件升级优化 Mysql服务器软件优化 数据库表结构优化 SQL语句及索引优化 本文重点关注于SQL语句及索引优化,关于其他优化方式以及索引原理等,请关注本人<MySQ ...
- MySQL 的查询优化
说起 MySQL 的查询优化,相信大家收藏了一堆奇技淫巧:不能使用 SELECT *.不使用 NULL 字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解它背 ...
- Linux 平台MySQL启动关闭方式总结
MySQL的启动方法有很多种,下面对比.总结这几种方法的一些差异和特性,下面实验的版本为MySQL 5.6.如有疏漏或不足,敬请指点一二. 1:使用mysqld启动.关闭MySQL服务 mysql ...
随机推荐
- js 性能优化整理之 缓存变量
简单的常见的操作:假设每个便签添加一个 属性 -webkit-animation-delay:0.1s 递增操作::通过for循环添加 <ul id="uls"> ...
- Spring 5 (0) - Introduction & Index
Spring Framework Reference Documentation I. Overview of Spring Framework . Getting Started with Spri ...
- HDU1061-Rightmost Digit(高速功率模)
pid=1061">主题链接 题意:求n^n的个位数的值. 思路:高速幂求值 代码: #include <iostream> #include <cstdio> ...
- 【Espruino】NO.06 关键是你的仆人(继续)
http://blog.csdn.net/qwert1213131/article/details/27834551 本文属于个人理解,能力有限,纰漏在所难免.还望指正. [小鱼有点电] 这几天一直在 ...
- WebGL 支持测试,并已支持的浏览器版本摘要
WebGL 支持情况检測与已支持浏览器版本号汇总 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公 ...
- PHP操作XML文件学习笔记
原文:PHP操作XML文件学习笔记 XML文件属于标签语言,可以通过自定义标签存储数据,其主要作用也是作为存储数据. 对于XML的操作包括遍历,生成,修改,删除等其他类似的操作.PHP对于XML的操作 ...
- jsp 行动标签
jsp行动标签 签.它影响JSP执行时的功能. 1.include动作标签 <jsp:include page="文件名称字"/> 或 <jsp:include ...
- 使用linux服务logrotate文件tomcat日志文件
使用notepad++编辑本地文件 tomcat: /usr/tomcat/logs/catalina.out { copytruncate daily dateext nocompress miss ...
- ASP.NET WebAPI从入门
在新出的MVC4中,增加了WebAPI,用于提供REST风格的WebService,新生成的WebAPI项目和典型的MVC项目一样,包含主要的Models.Views.Controllers等文件夹和 ...
- 从Ubuntu12.04升级到Ubuntu 14.04之后,系统将无法启动
进入Ubuntu启动界面.通常有几个选项,Ubuntu,Ubuntu先进... 输入e键,进入grub的设置界面.将里面的ro改动为rw就可以. 以上能够启动,临时性的设置 可是为了永久保存这个设置, ...