请帮帮我,如何加快这个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.
表仅在(
和)
编辑 - 这是答案
我将MySQL 5.1更改为MariaDB 5.5,速度更快!!!
答案 0 :(得分:2)
不幸的是,您的数据库设计迫使性能降低。
这将解决您的性能问题: 您应该创建一个新表(PRODUCT_INFO)并使外键指向produkty的主键。 使用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
答案 1 :(得分:1)
您可以尝试直接加入produkty
:
SELECT pa.*
FROM ParametrickeVyhladavanie pa
JOIN produkty p
ON p.ATTRIBUTE_CODE LIKE CONCAT('%', pa.code, '%')
AND p.ATTRIBUTE_VALUE LIKE CONCAT('%', pa.ValueCode, '%')
AND p.KATEGORIA IN ('$categoryArray')
GROUP BY pa.code
答案 2 :(得分:1)
试试这个 -
SELECT *
FROM (
SELECT DISTINCT Code, ValueCode
FROM ParametrickeVyhladavanie
) AS t
WHERE EXISTS(
SELECT 1
FROM produkty
WHERE KATEGORIA IN ('$categoryArray')
AND ATTRIBUTE_CODE LIKE CONCAT('%', t.code, '%')
AND ATTRIBUTE_VALUE LIKE CONCAT('%', t.ValueCode, '%')
)
答案 3 :(得分:0)
SELECT pa.*
FROM ParametrickeVyhladavanie pa
JOIN produkty p
ON p.ATTRIBUTE_CODE LIKE CONVERT(NVARCHAR,'%')+ pa.code + CONVERT(NVARCHAR,'%')
AND p.ATTRIBUTE_VALUE LIKE CONVERT(NVARCHAR,'%')+ pa.ValueCode+CONVERT(NVARCHAR,'%')
AND p.KATEGORIA IN ('$categoryArray')
GROUP BY pa.code