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之查询优化方式(笔记)的更多相关文章

  1. WebAPI调用笔记 ASP.NET CORE 学习之自定义异常处理 MySQL数据库查询优化建议 .NET操作XML文件之泛型集合的序列化与反序列化 Asp.Net Core 轻松学-多线程之Task快速上手 Asp.Net Core 轻松学-多线程之Task(补充)

    WebAPI调用笔记   前言 即时通信项目中初次调用OA接口遇到了一些问题,因为本人从业后几乎一直做CS端项目,一个简单的WebAPI调用居然浪费了不少时间,特此记录. 接口描述 首先说明一下,基于 ...

  2. 常见mysql的慢查询优化方式

    一,第一步.开启mysql慢查询 方式一: 修改配置文件  在 my.ini 增加几行:  主要是慢查询的定义时间(超过2秒就是慢查询),以及慢查询log日志记录( slow_query_log) 方 ...

  3. MYSQL视图的学习笔记

    MYSQL视图的学习笔记,学至Tarena金牌讲师,金色晨曦科技公司技术总监沙利穆 课程笔记的综合. 视图及图形化工具   1.       视图的定义 视图就是从一个或多个表中,导出来的表,是一个虚 ...

  4. MySQL in查询优化

    https://blog.csdn.net/gua___gua/article/details/47401621 MySQL in查询优化<一> 原创 2015年08月10日 17:57: ...

  5. 查询优化 | MySQL慢查询优化

    ​Explain查询:rows,定位性能瓶颈. 只需要一行数据时,使用LIMIT1. 在搜索字段上建立索引. 使用ENUM而非VARCHAR. 选择区分度高的列作为索引. 采用扩展索引,而不是新建索引 ...

  6. MySQL 慢查询优化

    为什么查询速度会慢 1.慢是指一个查询的响应时间长.一个查询的过程: 客户端发送一条查询给服务器 服务器端先检查查询缓存,如果命中了缓存,则立可返回存储在缓存中的结果.否则进入下一个阶段 服务器端进行 ...

  7. 《MySQL慢查询优化》之SQL语句及索引优化

    1.慢查询优化方式 服务器硬件升级优化 Mysql服务器软件优化 数据库表结构优化 SQL语句及索引优化 本文重点关注于SQL语句及索引优化,关于其他优化方式以及索引原理等,请关注本人<MySQ ...

  8. MySQL 的查询优化

    说起 MySQL 的查询优化,相信大家收藏了一堆奇技淫巧:不能使用 SELECT *.不使用 NULL 字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解它背 ...

  9. Linux 平台MySQL启动关闭方式总结

    MySQL的启动方法有很多种,下面对比.总结这几种方法的一些差异和特性,下面实验的版本为MySQL 5.6.如有疏漏或不足,敬请指点一二.   1:使用mysqld启动.关闭MySQL服务 mysql ...

随机推荐

  1. 如何关闭CBox(2.4版本号)强制升级的形式

    从今天开始2.4.0.9版本号CBox,提示检测到新的版本号,能够使用后必须更新为新版本号,提示表见下面的例子. 此次升级是强制升级.假如你选择不升级(单击窗体上的升级提示右下角"辍学but ...

  2. jQuery.reveal弹出层使用

    最近用到弹出层,还得自定义UI,原本用的artDialog太庞大,不合适了,于是就找到了这个东西,又小又好用,基础的弹出遮罩都有了,想要什么还不是Coder自己说了算. 这个插件是基于Jquery实现 ...

  3. mongodb迁移

    A机器上有mongodb服务,A机器要废,于是迁至B. 简单起见,依旧是在A上ps auxwww|grep mongo找到正在执行的进程: /home/admin/mongodb/mongodb-li ...

  4. 【Linux】CentOS系统

    版本号:CentOS release 5.7 1)查看系统版本号 cat  /etc/readhat-release 2)安装软件 wget  资源链接 make make install 在线安装: ...

  5. Prototype and Constructor in JavaScript

    The concept of prototype in JavaScript is very confusing, especially to those who come with a C++/JA ...

  6. UBUNTU如何改变mysql默认文件夹数据文件夹

    停止mysql维修 * sudo /etc/init.d/mysql stop 原始文件夹的副本 * cp -r /var/lib/mysql /home/yourname/somewhere 改动权 ...

  7. 大数据系列修炼-Scala课程02

    Scala数组操作实战详解 接着昨天的课程,下面我们继续学习关于Scala数组操作详解.Scala数组的定义 //数组定义 //定长数组格式 /** * val arrayName = new Arr ...

  8. jQuery自定义右键菜单

    首先看下效果,效果在最下面: 代码: body { font-size: 12px; margin: 0px; padding: 0px; } form,div,ul,li { margin: 0px ...

  9. BZOJ 1025 SCOI2009 游戏 动态规划

    标题效果:特定n.行定义一个替代品1~n这种更换周期发生后,T次要(T>0)返回到原来的顺序 找到行的所有可能的数 循环置换分解成若干个,然后行位移数是这些周期的长度的最小公倍数 因此,对于一些 ...

  10. opengl微开发之1-从零開始

    对OpenGL有一点了解之后,如今開始真正编写代码. 今天的内容: 使用FreeGLUT创建OpenGL的上下文环境 初始化GLEW 创建一个OpenGL的的模板范例 第一步: 一个OpenGL的上下 ...