sql教程栏目介绍如何更有效理解mysql
推荐(免费):sql教程
属性表(product_props)结构如下
数据量800w以上
字段名类型说明
id int id
pn_id int 属性类型
pv_id int 属性值
product_id int 产品id
其中product_id与pn_id,pv_id是一对多的关系。
数据类似这样:
product_idpn_idpv_id
10970 5 (型号) 135 (苹果9)
10970 11 (内存) 23 (512g)
10970 10 (颜色) 17 (土豪金)
10970 8 (网络) 6(5g)
10980 5 135
10980 11 24 (1024g)
10980 10 16 (极光蓝)
产品表(product)结构如下
数据量40w以上
字段名类型说明
product_id int product_id
type_id int 类型id
brand_id int 品牌id
model_id int 型号id
status tinyint 状态
数据类似以下:
product_idtype_idbrand_idmodel_idstatus
10970 1(手机) 1(苹果) 1(iphone8) 1(正常)
10980 1(手机) 1(苹果) 1(iphone8x) 3(已售)
10981 1(手机) 1(苹果) 1(iphone8xp) 1(正常)
问题
找出型号为苹果9同时内存为512g,颜色为土豪金,状态为正常的产品总数,
ps : 属性条件可能会有超过10组。
要求性能第一,杜绝聚合函数等
原问题的解决方案性能排行
来自 @kamicloud的 exist方案select sql_no_cache `product_id`from `zx_tests` as awhere `pn_id` = 101 and `pv_id` = 59 and exists( select sql_no_cache * from `zx_tests` where a.product_id = product_id and `pn_id` = 101 and `pv_id` = 171); 2 组条件下 0.657,3 组 0.695,4 组 0.759,5 组 0.743 (单独查属性表)
来自 @elijah_wang的子查询方案select `product_id` from `product` where `pn_id` = 5 and `pv_id` = 135 and `product_id` in (select `product_id` from `product` where `pn_id` = 11 and `pv_id` = 23); 2 组条件下 0.729,3 组 0.75,4 组 0.730,5 组 0.757 (新问题之前)
新问题之后的性能排行
来自 @elijah_wang的子查询方案 select sql_no_cache count(1) from pdi_product a join ( select distinct product_id from `product_props` where `pn_id` = 5 and `pv_id` = 127 and `product_id` in ( select `product_id` from `product_props` where `pn_id` = 11 and `pv_id` = 22 ) and `product_id` in ( select `product_id` from `product_props` where `pn_id` = 10 and `pv_id` = 18 ) and `product_id` in ( select `product_id` from `product_props` where `pn_id` = 8 and `pv_id` = 6 ) and `product_id` in ( select `product_id` from `product_props` where `pn_id` = 9 and `pv_id` = 1 ) ) b on a.product_id = b.product_id where a.status = 1;
耗时1.5-1.56 (执行10次的范围)
expain分析:
select sql_no_cache count(1) from pdi_product a where a.status = 1 and a.product_id in (select distinct product_id from `product_props` where `pn_id` = 5 and `pv_id` = 127 and `product_id` in ( select `product_id` from `product_props` where `pn_id` = 11 and `pv_id` = 22 ) and `product_id` in ( select `product_id` from `product_props` where `pn_id` = 10 and `pv_id` = 18 ) and `product_id` in ( select `product_id` from `product_props` where `pn_id` = 8 and `pv_id` = 6 ) and `product_id` in ( select `product_id` from `product_props` where `pn_id` = 9 and `pv_id` = 1 ))
耗时0.69-0.72(执行10次的范围)
explain分析:
来自 @kamicloud的 exist方案 select sql_no_cache count(1) from product a where a.status = 1 and a.product_id in ( select distinct `product_id` from `product_props` as a where a.`pn_id` = 5 and a.`pv_id` = 127 and exists ( select product_id from `product_props` where a.product_id = product_id and `pn_id` = 11 and `pv_id` = 22 ) and exists ( select product_id from `product_props` where a.product_id = product_id and `pn_id` = 10 and `pv_id` = 18 ) and exists ( select product_id from `product_props` where a.product_id = product_id and `pn_id` = 9 and `pv_id` = 1 ) and exists ( select product_id from `product_props` where a.product_id = product_id and `pn_id` = 8 and `pv_id` = 6 ) );
耗时5.7-5.85 (执行10次的范围)
explain分析:
select sql_no_cache count(1) from pdi_product a join (select distinct `product_id` from `product_props` as a where a.`pn_id` = 5 and a.`pv_id` = 127 and exists ( select product_id from `product_props` where a.product_id = product_id and `pn_id` = 11 and `pv_id` = 22 ) and exists ( select product_id from `product_props` where a.product_id = product_id and `pn_id` = 10 and `pv_id` = 18 ) and exists ( select product_id from `product_props` where a.product_id = product_id and `pn_id` = 9 and `pv_id` = 1 ) and exists ( select product_id from `product_props` where a.product_id = product_id and `pn_id` = 8 and `pv_id` = 6 ) ) b on a.product_id = b.product_id where a.status = 1
耗时5.7-6.0(执行10次的范围)
explain分析:
可以看到如果单纯查属性表,第一位的速度是最快的,可要查产品状态后,速度反而不如子查询。
经explain分析,第一个子查询速度之所以快是因为它的sql简单,select_type皆为simple。
而不管是join还是exists的方式,select_type大多为derived,dependent subquery。
相关免费学习推荐:mysql视频教程
以上就是解决 sql 问题绝对能让你对 mysql 的理解更进一步!的详细内容。
