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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值