mysql sql优化实例

优化前:

pt-query-degist分析结果:

  1. # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B5 at byte 628331
  2. # This item is included in the report because it matches --limit.
  3. # Scores: V/M = 0.19
  4. # Time range: 2016-09-24T15:14:24 to 2016-10-08T07:46:24
  5. # Attribute pct total min max avg 95% stddev median
  6. # ============ === ======= ======= ======= ======= ======= ======= =======
  7. # Count 12 50
  8. # Exec time 6 623s 10s 16s 12s 15s 2s 11s
  9. # Lock time 0 28ms 176us 12ms 553us 568us 2ms 287us
  10. # Rows sent 0 162 3 5 3.24 4.96 0.67 2.90
  11. # Rows examine 11 776.54k 13.80k 16.19k 15.53k 15.96k 761.60 15.96k
  12. # Query size 7 12.74k 261 261 261 261 0 261
  13. # String:
  14. # Databases wechat_prod
  15. # Hosts localhost
  16. # Users test
  17. # Query_time distribution
  18. # 1us
  19. # 10us
  20. # 100us
  21. # 1ms
  22. # 10ms
  23. # 100ms
  24. # 1s
  25. # 10s+ ################################################################
  26. # Tables
  27. # SHOW TABLE STATUS FROM `wechat_prod` LIKE 'product'\G
  28. # SHOW CREATE TABLE `wechat_prod`.`product`\G
  29. # SHOW TABLE STATUS FROM `wechat_prod` LIKE 'sys_members'\G
  30. # SHOW CREATE TABLE `wechat_prod`.`sys_members`\G
  31. # SHOW TABLE STATUS FROM `wechat_prod` LIKE 'product_sku'\G
  32. # SHOW CREATE TABLE `wechat_prod`.`product_sku`\G
  33. # EXPLAIN /*!50100 PARTITIONS*/
  34. SELECT `p`.`id`, `p`.`title`, `p`.`fare`, `p`.`sales`, `p`.`user_openid`, `u`.`nickname`, `s`.`price` FROM `product` `p` LEFT JOIN `sys_members` `u` ON p.user_openid = u.openid
  35. LEFT JOIN `product_sku` `s` ON s.product_id = p.id ORDER BY `wd_sort` LIMIT 3\G

sql 分析

  1. mysql> EXPLAIN /*!50100 PARTITIONS*/
  2. -> SELECT `p`.`id`, `p`.`title`, `p`.`fare`, `p`.`sales`, `p`.`user_openid`, `u`.`nickname`, `s`.`price` FROM `product` `p` LEFT JOIN `sys_members` `u` ON p.user_openid = u.openid
  3. -> LEFT JOIN `product_sku` `s` ON s.product_id = p.id ORDER BY `wd_sort` LIMIT 3\G
  4. *************************** 1. row ***************************
  5. id: 1
  6. select_type: SIMPLE
  7. table: p
  8. partitions: NULL
  9. type: ALL
  10. possible_keys: NULL
  11. key: NULL
  12. key_len: NULL
  13. ref: NULL
  14. rows: 2413
  15. filtered: 100.00
  16. Extra: Using temporary; Using filesort
  17. *************************** 2. row ***************************
  18. id: 1
  19. select_type: SIMPLE
  20. table: u
  21. partitions: NULL
  22. type: eq_ref
  23. possible_keys: openid
  24. key: openid
  25. key_len: 152
  26. ref: wechat_prod.p.user_openid
  27. rows: 1
  28. filtered: 100.00
  29. Extra: Using where
  30. *************************** 3. row ***************************
  31. id: 1
  32. select_type: SIMPLE
  33. table: s
  34. partitions: NULL
  35. type: ALL
  36. possible_keys: NULL
  37. key: NULL
  38. key_len: NULL
  39. ref: NULL
  40. rows: 518
  41. filtered: 100.00
  42. Extra: Using where; Using join buffer (Block Nested Loop)
  43. 3 rows in set, 2 warnings (0.00 sec)

productproduct_sku表都没有使用索引。

其中product表的分析结果为Extra: Using temporary; Using filesort,此结果表示使用了临时文件排序,product_sku表的分析结果为Extra: Using where; Using join buffer (Block Nested Loop),而此结果表示使用了循环查找,扫描了518行。

product表表结构:

  1. CREATE TABLE `product` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `title` varchar(64) DEFAULT NULL ,
  4. `description` varchar(1200) DEFAULT '' ,
  5. `cat_id` smallint(6) DEFAULT '1' ,
  6. `on_sell` tinyint(4) DEFAULT NULL,
  7. `sort` int(8) DEFAULT NULL ,
  8. `nice` tinyint(4) DEFAULT NULL ,
  9. `user_openid` varchar(32) DEFAULT NULL ,
  10. `is_return` tinyint(2) DEFAULT NULL ,
  11. `fare` tinyint(4) DEFAULT NULL ,
  12. `content` text COMMENT ,
  13. `add_time` int(11) DEFAULT NULL ,
  14. `sales` int(11) DEFAULT '0' ,
  15. `if_audit` tinyint(1) DEFAULT '1,
  16. PRIMARY KEY (`id`)
  17. ) ENGINE=InnoDB AUTO_INCREMENT=3321 DEFAULT CHARSET=utf8

product_sku表表结构:

  1. CREATE TABLE `product_sku` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  3. `product_id` bigint(20) DEFAULT NULL,
  4. `name` varchar(64) DEFAULT NULL ,
  5. `count` int(8) DEFAULT NULL ,
  6. `price` decimal(10,2) DEFAULT NULL ,
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB AUTO_INCREMENT=3367 DEFAULT CHARSET=utf8

添加索引

  1. alter table product add index user_openid(user_openid);
  2. alter table product_sku add index product_id(product_id);

分析添加索引后的查询情况

  1. mysql> explain SELECT `p`.`id`, `p`.`title`, `p`.`fare`, `p`.`sales`, `p`.`user_openid`, `u`.`nickname`, `s`.`price` FROM `product` `p` LEFT JOIN `sys_members` `u` ON p.user_openid = u.openid LEFT JOIN `product_sku` `s` ON s.product_id = p.id LIMIT 3;
  2. +----+-------------+-------+------------+--------+---------------+---------------+---------+--------------------------+------+----------+-------------+
  3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  4. +----+-------------+-------+------------+--------+---------------+---------------+---------+--------------------------+------+----------+-------------+
  5. | 1 | SIMPLE | p | NULL | ALL | NULL | NULL | NULL | NULL | 2413 | 100.00 | NULL |
  6. | 1 | SIMPLE | u | NULL | eq_ref | openid | openid | 152 | wechat_prod.p.user_openid | 1 | 100.00 | Using where |
  7. | 1 | SIMPLE | s | NULL | ref | product_id | product_id | 9 | wechat_prod.p.id | 1 | 100.00 | NULL |
  8. +----+-------------+-------+------------+--------+---------------+---------------+---------+--------------------------+------+----------+-------------+
  9. 3 rows in set, 1 warning (0.00 sec)

使用索引后,product_sku表只扫描了1行。

由平均的12s降为0.0几秒,几乎可以忽略不计。

mysql sql优化实例的更多相关文章

  1. mysql sql优化实例1(force index使用)

    今天和运维同学一块查找mysql慢查询日志,发现了如下一条sql: SELECT sum(`android` + ios) total,pictureid,title,add_time FROM `j ...

  2. Mysql SQL优化&执行计划

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

  3. 18.Mysql SQL优化

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

  4. mysql sql优化及注意事项

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

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

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

  6. MySQL SQL优化

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

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

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

  8. MySql Sql 优化技巧分享

    有天发现一个带inner join的sql 执行速度虽然不是很慢(0.1-0.2),但是没有达到理想速度.两个表关联,且关联的字段都是主键,查询的字段是唯一索引. sql如下: SELECT p_it ...

  9. MySQL索引优化实例说明

    下面分别创建三张表,并分别插入1W条简单的数据用来测试,详情如下: [1] test_a 有主键但无索引   CREATE TABLE `test_a` (   `id` int(10) unsign ...

随机推荐

  1. jquery修改css样式,样式带!important

    由于需求的需要,今天在用jquery修改一个弹出框的样式的时候,由于有一个按钮有padding-left:12px;导致内间距空出来的这一块颜色用普通的方式无法改变. 普通的jquery修改css的方 ...

  2. [转]java selenium webdriver实战 应用小结

    原文链接:http://www.cnblogs.com/itliucheng/p/5578788.html 部分api 1.访问网站 driver.get("http://www.baidu ...

  3. 什么是Jedis?

    Jedis 是 Redis 官方首选的 Java 客户端开发包. 实例方法: 1 import redis.clients.jedis.* 1 2 3 Jedis jedis = new Jedis( ...

  4. Ubuntu学习总结-07 Nodejs和npm的安装

    一 安装NodeJS 1 下载nodejs源码 从以下网址下载最新的Nodejs源码 https://nodejs.org/en/download/ 2 安装依赖的 python,gcc,g++ 函数 ...

  5. request 获取服务根目录地址

    这是常用的request获取服务地址的常用方式. 源请求服务地址:http://localhost/api-server/1/forum/thread/hot_topic?sex=1 String p ...

  6. animate对颜色设置不起作用

    今天了解了一下stop的使用方法,但是实例中加入color:red的时候,动画效果没有实现,具体实例如下: http://jsbin.com/fezaroyene/edit?html,js,outpu ...

  7. Java框架--jQueryEasyUI

    111------------------------------------------------------------------------------------------------- ...

  8. 9月11日上午HTML有序列表、无序列表、网页的格式和布局

    样式表 六.列表方块 1.有序列表变无序列表 <ol> <li>张店</li> <li>桓台</li> <li>淄川</l ...

  9. Java数据结构——图

    点 //类名:Vertex //属性: //方法: class Vertex{ public char label; //点的名称,如A public boolean wasVisited; publ ...

  10. 直接拿来用,最火的.NET开源项目

    综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...