zoukankan      html  css  js  c++  java
  • Mysql优化——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; 

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

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

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

    |  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的返回内容要尽量少.

  • 相关阅读:
    Python冒泡排序(4)
    Python冒泡排序(3)
    Python3默认递归最大深度是998
    Python利用递归函数和列表推导式实现快速排序
    天池比赛的文章--欢迎大家交流
    caffe学习笔记1
    网络压缩系列1:低秩逼近
    yolov1
    Windows下用Caffe跑自己的数据(遥感影像)
    基于灰度共生矩阵的纹理提取
  • 原文地址:https://www.cnblogs.com/zgxblog/p/14120383.html
Copyright © 2011-2022 走看看