【发布时间】:2012-01-28 06:18:24
【问题描述】:
所以,这里有一个问题可能会让 SQL 专家跳来跳去骂我懒惰,但我很难过。今天早上我们的在线商店崩溃并被烧毁,这里是可疑查询。我整天都在想这个,并没有想出任何天才的优化。我能得到一些帮助吗?有什么关键指标吗?有什么办法可以重组这个?我意识到这就像问墙的另一边有什么,然后给你一个指向另一个方向的望远镜,但我认为这值得一试:
SELECT DISTINCT (SELECT filename FROM (SELECT DISTINCT y.value AS label, x.value AS filename
FROM `catalog_product_super_link` AS z
INNER JOIN `catalog_product_entity_varchar` AS y
ON z.product_id = y.entity_id
INNER JOIN `catalog_product_entity_varchar` AS x
ON z.product_id = x.entity_id
WHERE parent_id = (SELECT entity_id
FROM `catalog_product_entity`
WHERE sku LIKE 'F11-ARC-7710%'
LIMIT 0, 1)
AND y.attribute_id = (SELECT attribute_id
FROM `eav_attribute`
WHERE attribute_code = 'image_label'
AND entity_type_id = (SELECT entity_type_id FROM `eav_entity_type` WHERE entity_type_code = 'catalog_product') LIMIT 0, 1)
AND x.attribute_id = (SELECT attribute_id
FROM `eav_attribute`
WHERE attribute_code = 'image'
AND entity_type_id = (SELECT entity_type_id FROM `eav_entity_type` WHERE entity_type_code = 'catalog_product'))) AS images WHERE c.value LIKE CONCAT(label,'%') LIMIT 0, 1) AS image,
pricing_value,
is_percent,
value_index,
c.value AS label,
d.sort_order AS sort_order
FROM `catalog_product_super_attribute_pricing` AS a
INNER JOIN `catalog_product_super_attribute_label` AS b
ON a.product_super_attribute_id = b.product_super_attribute_id
INNER JOIN `eav_attribute_option_value` AS c
ON value_index = c.option_id
INNER JOIN `eav_attribute_option` AS d
ON c.option_id = d.option_id
WHERE a.product_super_attribute_id = (SELECT product_super_attribute_id
FROM `catalog_product_super_attribute`
WHERE product_id = 5928
AND attribute_id = 143 LIMIT 0, 1)
UNION ALL
SELECT DISTINCT (SELECT filename FROM (SELECT DISTINCT y.value AS label, x.value AS filename
FROM `catalog_product_super_link` AS z
INNER JOIN `catalog_product_entity_varchar` AS y
ON z.product_id = y.entity_id
INNER JOIN `catalog_product_entity_varchar` AS x
ON z.product_id = x.entity_id
WHERE parent_id = (SELECT entity_id
FROM `catalog_product_entity`
WHERE sku LIKE 'F11-ARC-7710%'
LIMIT 0, 1)
AND y.attribute_id = (SELECT attribute_id
FROM `eav_attribute`
WHERE attribute_code = 'image_label'
AND entity_type_id = (SELECT entity_type_id FROM `eav_entity_type` WHERE entity_type_code = 'catalog_product') LIMIT 0, 1)
AND x.attribute_id = (SELECT attribute_id
FROM `eav_attribute`
WHERE attribute_code = 'image'
AND entity_type_id = (SELECT entity_type_id FROM `eav_entity_type` WHERE entity_type_code = 'catalog_product'))) AS images
WHERE label LIKE CONCAT((SELECT value FROM `eav_attribute_option_value` WHERE option_id = c.value LIMIT 0, 1),'%') LIMIT 0, 1) AS image,
0 AS pricing_value,
0 AS is_percent,
c.value AS value_index,
(SELECT value FROM `eav_attribute_option_value` WHERE option_id = c.value LIMIT 0, 1) AS label,
(SELECT sort_order FROM `eav_attribute_option` WHERE option_id = c.value LIMIT 0, 1) AS sort_order
FROM `catalog_product_entity` AS a
INNER JOIN `cataloginventory_stock_status` AS b
ON a.entity_id = b.product_id
INNER JOIN `catalog_product_entity_int` AS c
ON a.entity_id = c.entity_id
INNER JOIN `cataloginventory_stock_item` AS d
ON a.entity_id = d.product_id
WHERE c.attribute_id = (SELECT attribute_id
FROM `eav_attribute`
WHERE attribute_code = 'choose_size'
AND entity_type_id = (SELECT entity_type_id FROM `eav_entity_type` WHERE entity_type_code = 'catalog_product') LIMIT 0, 1)
AND a.entity_id IN (SELECT DISTINCT product_id
FROM `catalog_product_super_link`
WHERE parent_id = (SELECT entity_id FROM `catalog_product_entity` WHERE sku LIKE 'F11-ARC-7710%' LIMIT 0, 1))
AND (b.qty > 0 OR d.manage_stock = 0)
AND (SELECT value
FROM `eav_attribute_option_value`
WHERE option_id = c.value
LIMIT 0, 1) NOT IN (SELECT c.value
FROM `catalog_product_super_attribute_pricing` AS a
INNER JOIN `catalog_product_super_attribute_label` AS b ON a.product_super_attribute_id = b.product_super_attribute_id
INNER JOIN `eav_attribute_option_value` AS c ON value_index = c.option_id
WHERE a.product_super_attribute_id = (SELECT product_super_attribute_id FROM `catalog_product_super_attribute`
WHERE product_id = 5928
AND attribute_id = 143))
ORDER BY sort_order
提前致谢!
【问题讨论】:
-
此查询太长且太复杂,无法花时间理解,因此可以对其进行优化。你能给出一个(小!)输入数据、表结构和所需输出的样本吗?
标签: mysql sql database indexing