3

请帮帮我,如何加快这个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,而且速度更快!!!

4

4 回答 4

2

不幸的是,您的数据库设计迫使性能降低。

这将解决您的性能问题:您应该创建一个新表(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
于 2013-08-02T08:29:38.553 回答
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
于 2013-08-02T08:27:04.117 回答
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, '%')
)
于 2013-08-02T08:27:26.717 回答
0

这个怎么样?使用 convert 而不是 concat

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
于 2013-08-02T09:39:55.823 回答