题: 在ecshop商城表中,查询6号栏目的商品, (注,6号是一个大栏目)
最直观的: mysql> select goods_id,cat_id,goods_name from goods where cat_id in (select
cat_id from ecs_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 ecs_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:

题: 在ecshop商城表中,查询6号栏目的商品, (注,6号是一个大栏目)

最直观的:
mysql> select goods_id,cat_id,goods_name from  goods where cat_id in (select

cat_id from ecs_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 ecs_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:

in 型子查询引出的陷阱的更多相关文章

  1. in型子查询陷阱,exists子查询

    in 型子查询引出的陷阱 select goods_id from goods where cat_id in (1,2,3) 直接用id,不包含子查询,不会中陷阱 题: 在ecshop商城表中,查询 ...

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

    in型子查询引出的陷阱:(扫更少的行,不要临时表,不要文件排序就快) 题: 在ecshop商城表中,查询6号栏目的商品, (注,6号是一个大栏目) 最直观的: mysql); 误区: 给我们的感觉是, ...

  3. MySql cmd下的学习笔记 —— 有关子查询的操作(where型,from型,exists型子查询)

    先找到goods表 查询goods_id最大的商品 where型的子查询 查询goods_id最大的商品(不能用排序) 把两步写成一步,就是子查询 from型子查询 查找出每种cat_id下goods ...

  4. MySQL in型子查询陷阱

    现在有两个表,table1和table2,table1有1千万数据(id 主键索引),table2有三条数据(uid字段 3,5,7): select * from table1 where id i ...

  5. mysql的查询、子查询及连接查询

    >>>>>>>>>> 一.mysql查询的五种子句         where(条件查询).having(筛选).group by(分组). ...

  6. MySql学习(三) —— 子查询(where、from、exists) 及 连接查询(left join、right join、inner join、union join)

    注:该MySql系列博客仅为个人学习笔记. 同样的,使用goods表来练习子查询,表结构如下: 所有数据(cat_id与category.cat_id关联): 类别表: mingoods(连接查询时作 ...

  7. Mysql子查询、关联查询

    mysql中update.delete.install尽量不要使用子查询 一.mysql查询的五种子句         where(条件查询).having(筛选).group by(分组).orde ...

  8. oracle 子查询的几个种类

    1.where型子查询: select cat_id,good_id,good_name from goods where good_id in (selct max(good_id) from go ...

  9. 从项目上一个子查询扩展学习开来:mysql的查询、子查询及连接查询

    上面这样的数据,想要的结果是:如果matchResult为2的话,代表是黑名单.同一个softId,version,pcInfoId的代表是同一个软件,需要去重:同时,如果相同软件里面只要有一个mat ...

随机推荐

  1. CentOS 安装 Hadoop

    原文地址:http://www.cnblogs.com/caca/p/centos_hadoop_install.html 下载和安装   download hadoop from http://ha ...

  2. 【Linux】撷取命令grep

    什么是撷取命令啊?说穿了,就是将一段数据经过分析后,取出我们所想要的.或者是经由分析关键词,取得我们所想要的那一行! 不过,要注意的是,一般来说,撷取信息通常是针对『一行一行』来分析的, 并不是整篇信 ...

  3. android语音识别技术

      今天从网上找了个例子实现了语音识别,个人感觉挺好玩的,就把代码贴出来与大家分享下: Android中主要通过RecognizerIntent来实现语音识别,其实代码比较简单,但是如果找不到设置,就 ...

  4. 图解最小生成树 - 普里姆(Prim)算法

    我们在图的定义中说过,带有权值的图就是网结构.一个连通图的生成树是一个极小的连通子图,它含有图中全部的顶点,但只有足以构成一棵树的n-1条边.所谓的最小成本,就是n个顶点,用n-1条边把一个连通图连接 ...

  5. gulp#4.0 Did you forget to signal async completion?

    异常截图 解决方案: https://stackoverflow.com/questions/36897877/gulp-error-the-following-tasks-did-not-compl ...

  6. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  7. django1.8forms读书笔记

    一.HttpRequest对象的一些属性或方法 request.path,The full path, not including the domain but including the leadi ...

  8. Oracle Study之-AIX6.1构建Oracle 10gR2 RAC(3)

    Oracle Study之-AIX6.1构建Oracle 10gR2 RAC(3) 一.配置共享存储 [oracle@aix203 ~]$lsdev -c disk hdisk0 Available ...

  9. C程序的内存布局

     1.代码段(code或text): 通常是指用来存放程序执行代码的一块内存区域.这部分区域的大小在程序运行前就已经确定,并且内存区域通常属于只读. 某些架构也允许代码段为可写,即允许修改程序.在代码 ...

  10. CSocket类网络编程 MFC

    Visual C++的MFC提供了CSocket类用来实现网络通信. 下面介绍VC++在Windows 95中实现Socket的 CSocket 类相关成员函数(这些成员函数实际上是从CAsyncSo ...