mysql优化---in型子查询,exists子查询,from 型子查询

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
复制代码
in型子查询引出的陷阱:(扫更少的行,不要临时表,不要文件排序就快)

题: 在ecshop商城表中,查询6号栏目的商品, (注,6号是一个大栏目)
最直观的: mysql> select goods_id,cat_id,goods_name from  goods where cat_id in (select cat_id from category where parent_id=6);

误区: 给我们的感觉是, 先查到内层的6号栏目的子栏目,如7,8,9,11
然后外层, cat_id in (7,8,9,11)

事实: 如下图, goods表全扫描, 并逐行与category表对照,看parent_id=6是否成立
复制代码

复制代码
原因: mysql的查询优化器,针对In型做优化,被改成了exists的执行效果.
当goods表越大时, 查询速度越慢.

改进: 用连接查询来代替子查询
 explain select goods_id,g.cat_id,g.goods_name from  goods as g
 inner join (select cat_id from category where parent_id=6) as t
 using(cat_id) \G

内层 select cat_id from ecs_category where parent_id=6 ; 用到Parent_id索引, 返回4行
+--------+
| cat_id |
+--------+
|      7 |
|      8 |
|      9 |
|     11 |
+--------+    形成结果,设为t    



*************************** 3. row ***************************
           id: 2
  select_type: DERIVED
        table: ecs_category
         type: ref
possible_keys: parent_id
          key: parent_id
      key_len: 2
          ref:
         rows: 4
        Extra:
3 rows in set (0.00 sec)


第2次查询, 
t和 goods 通过 cat_id 相连, 
因为cat_id在 goods表中有索引, 所以相当于用7,8,911,快速匹配上 goods的行.
*************************** 2. row ***************************
           id: 1
  select_type: PRIMARY
        table: g
         type: ref
possible_keys: cat_id
          key: cat_id
      key_len: 2
          ref: t.cat_id
         rows: 6
        Extra:

第1次查询 :
是把上面2次的中间结果,直接取回.
*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: <derived2>
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 4
        Extra:
exists子查询:
题: 查询有商品的栏目.
按上面的理解,我们用join来操作,如下:
mysql> select c.cat_id,cat_name from ecs_category as c inner join  goods as g
 on c.cat_id=g.cat_id group by cat_name; (见36)

优化1:  在group时, 用带有索引的列来group, 速度会稍快一些,另外,
用int型 比 char型 分组,也要快一些.(见37)


优化2: 在group时, 我们假设只取了A表的内容,group by 的列,尽量用A表的列,
会比B表的列要快.(见38)

优化3: 从语义上去优化
select cat_id,cat_name from ecs_category where exists(select *from  goods where  goods.cat_id=ecs_category.cat_id) (见40)

|       36 | 0.00039075 | select c.cat_id,cat_name from ecs_category as c inner
join  goods as g on c.cat_id=g.cat_id group by cat_name
              |
|       37 | 0.00038675 | select c.cat_id,cat_name from ecs_category as c inner
join  goods as g on c.cat_id=g.cat_id group by cat_id
              |
|       38 | 0.00035650 | select c.cat_id,cat_name from ecs_category as c inner
join  goods as g on c.cat_id=g.cat_id group by c.cat_id
              |
|       40 | 0.00033500 | select cat_id,cat_name from ecs_category where exists
(select * from  goods where  goods.cat_id=ecs_category.cat_id)
              |

from 型子查询:
注意::内层from语句查到的临时表, 是没有索引的.因为是一个临时形成的结果。
所以: from的返回内容要尽量少.
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/8297192.html,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
10天前
|
关系型数据库 MySQL 索引
mysql 分析5语句的优化--索引添加删除
mysql 分析5语句的优化--索引添加删除
11 0
|
15天前
|
存储 关系型数据库 MySQL
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
|
15天前
|
存储 SQL 关系型数据库
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
|
15天前
|
存储 关系型数据库 MySQL
轻松入门MySQL:优化进销存管理,掌握MySQL索引,提升系统效率(11)
轻松入门MySQL:优化进销存管理,掌握MySQL索引,提升系统效率(11)
|
21天前
|
SQL 存储 关系型数据库
MySQL not exists 真的不走索引么
MySQL not exists 真的不走索引么
24 0
|
27天前
|
SQL 监控 关系型数据库
MySQL性能调优:监控和优化
MySQL性能调优:监控和优化
52 1
|
17天前
|
存储 SQL 关系型数据库
mysql优化一
mysql优化一
16 0
|
1月前
|
SQL 关系型数据库 MySQL
MYSQL分页limit速度太慢优化方法
MYSQL分页limit速度太慢优化方法
29 0
|
10天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
76 0
|
15天前
|
存储 关系型数据库 MySQL
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)
MySQL数据库性能大揭秘:表设计优化的高效策略(优化数据类型、增加冗余字段、拆分表以及使用非空约束)