更新时间:2022-01-08 18:07:03
给大家讲解一下解决SQL问题,一定会帮助大家进一步了解MySQL!相信朋友们也应该很关心这个话题。现在,我们来谈谈为你的朋友解决SQL问题,这一定会帮助你进一步了解MySQL!边肖还收集了关于解决SQL问题的信息,一定会让你进一步了解MySQL!我希望你看到后会喜欢。
SQL教程专栏介绍了如何更有效地理解MySQL。
推荐(免费):SQL教程
属性表(product_props)具有以下结构
数据量超过800瓦
字段名类型描述了idintidpn _ idint属性类型pv_idint属性值product_id ID,其中product _ ID与PN _ ID和PV _ ID是一对多的关系。像这样的数据:
Product_idpn_idpv_id109705(型号)135(苹果9)1097011(内存)23 (512G)1097010(颜色)17(本地金)109708(网络)6 (5g) 1098051351098011
数据量超过40W
段名类型描述product _ IDT product _ idtype _ IDT product类型idbrand _ IDT pland id model _ IDT product类型idstatustinyint状态数据类似于以下内容:
产品_ ID类型_ ID品牌_ ID型号_ ID状态109701(手机)1(苹果)1(Iphone8)1(普通)109801(手机)1(苹果)1(Iphone8X)3(已售)109811(手机)1(苹果)1 (iPhone 88
找出苹果9的型号和512G内存的产品总数,本地金的颜色和正常状态。ps :的属性条件可能超过10组。
性能第一,没有聚合功能等。
问题的原始解决方案性能排名
从@Kamicloud中选择现有方案
sql _ no _ cache `product _ id
从
` zx_tests` AS a
在哪里
` pn_id`=101和` pv_id`=59
和存在(选择
sql_no_cache *
从
` zx _ tests
在哪里
a.product_id=product_id和
` pn _ id `=101 AND ` PV _ id `=171);
2组条件下,0.6573组0.6954组0.7595组0.743(单独查属性表)。来自@Elijah_Wang的子查询方案从` product '中选择` product _ ID `,其中` PN _ ID`=5,` PV _ ID`=135和`。product _ id ` IN(SELECT ` product _ id ` FROM ` product ` WHERE ` pn _ id `=11和` PV _ id `=23);
0.7293组0.754组0.7305组0.757(新题前)新题后成绩排名情况下第2组
从PDI产品连接中选择SQL _ no _ cachecount (1(
挑选
不同的产品id
从
“产品_道具”
在哪里
` pn_id`=5
AND `pv_id`=127
和“产品_id”输入(从“产品_道具”中选择“产品_id”,其中“pn _ id ”=11和“PV _ id ”=22)
和“产品id”输入(从“产品道具”中选择“产品id”,其中“pn _ id ”=10和“PV _ id ”=18)
和“产品_id”输入(从“产品_道具”中选择“产品_id”,其中“pn _ id ”=8和“PV _ id ”=6)
和“产品标识”输入(从“产品道具”中选择“产品标识”
n_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大多为DERIVEDDEPENDENT SUBQUERY。
相关免费学习推荐:mysql视频教程
以上就是解决 SQL 问题绝对能让你对 MySQL 的理解更进一步!的详细内容!
来源:php中文网