请帮帮我,如何加快这个SQL查询?
SELECT pa.* FROM ParametrickeVyhladavanie pa, (SELECT p.* FROM produkty p WHERE p.KATEGORIA IN ('$categoryArray')) produkt WHERE produkt.ATTRIBUTE_CODE LIKE CONCAT('%', pa.code, '%') AND produkt.ATTRIBUTE_VALUE LIKE CONCAT('%', pa.ValueCode, '%') GROUP BY pa.code
索引:pa.code,pa.ValueCode,p.ATTRIBUTE_CODE,p.ATTRIBUTE_VALUE
显示第0-25行(共26行,查询耗时 20.4995秒 )
编辑 实际代码:
SELECT pa.* FROM ParametrickeVyhladavanie pa WHERE EXISTS ( SELECT 1 FROM produkty p JOIN PRODUCT_INFO AS pi ON p.ProId = pi.ProduktID AND p.KATEGORIA IN ('Mobily')) AND pi.ATTRIBUTE_CODE = pa.AttributeCode AND pi.ATTRIBUTE_VALUE = pa.ValueCode GROUP BY pa.code
这段代码显示错误#1054 - Unknown column 'pi.ATTRIBUTE_CODE' in 'where clause' 该pi.表仅在(和之间工作)
#1054 - Unknown column 'pi.ATTRIBUTE_CODE' in 'where clause'
pi.
(
)
编辑-这是答案
我将MySQL 5.1更改为MariaDB 5.5及其更快的版本!!!
不幸的是,您的数据库设计迫使性能降低。
这将解决您的性能问题:您应该创建一个新表(PRODUCT_INFO),并使外键指向产品主键。使用来自ATTRIBUTE_CODE和ATTRIBUTE_VALUE的各个值填充该表。
SELECT pa.code FROM ParametrickeVyhladavanie pa WHERE EXISTS ( SELECT 1 FROM produkty p JOIN PRODUCT_INFO AS pi ON p.ProId = pi.ProduktID WHERE pi.ATTRIBUTE_CODE = pa.Code AND pi.ATTRIBUTE_VALUE = pa.ValueCode AND p.KATEGORIA IN ('Mobily')) GROUP BY pa.code