1

当我使用此查询时,一切正常。5 结果在 0.3587 秒内。

SELECT `prod_combined`.`sku`, `prod_combined`.`titel`, `prod_combined`.`preis_vk`, 
`prod_combined`.`link` 
FROM `prod_combined` 
WHERE 
(
   prod_combined.sku IN 
       (
       SELECT 
       prod_combined.sku FROM 
       (
           SELECT prod_combined.sku FROM prod_billiger 
           INNER JOIN
           prod_combined ON prod_combined.sku = prod_billiger.sku 
           WHERE rangGesamt = 1 
           AND prod_combined.aktiv = 1 
       ) q 
       JOIN prod_combined ON prod_combined.sku = q.sku)
) 
AND (aktiv = 1)
LIMIT 0,5

但是当我使用这个查询时,它会运行大约 3 分钟。

SELECT `prod_combined`.`sku`, `prod_combined`.`titel`, `prod_combined`.`preis_vk`, 
`prod_combined`.`link` 
FROM `prod_combined` 
WHERE 
(
   prod_combined.sku IN 
       (
       SELECT 
       prod_combined.sku FROM 
       (
           SELECT prod_combined.sku FROM prod_billiger 
           INNER JOIN
           prod_combined ON prod_combined.sku = prod_billiger.sku 
           WHERE rangGesamt = 1 
           AND prod_combined.aktiv = 1 
       ) q 
       JOIN prod_combined ON prod_combined.sku = q.sku)
) 
AND (aktiv = 1)

我想要做什么:获取所有排名第一的产品(prod_billiger.rangGesamt)。我在“prod_billiger”表中得到这些信息。其他产品信息来自 prod_combined。

有没有办法加快速度?我需要过度考虑整个查询吗?

问候, 查奇2305

4

2 回答 2

0

像这样的东西对你有用吗?

SELECT pc.sku, pc.titel, pc.preis_vk, pc.link
FROM prod_combined AS pc 
WHERE pc.sku IN (
    SELECT sku
    FROM prod_billiger
    WHERE rangGesamt = 1
)
AND pc.aktiv=1

我为 prod_combined ( prod_combined AS pc) 添加了一个同义词,只是为了使select语句更简洁。

如果您的表很小,或者如果您在 rangGesamt 和/或 aktiv 上有索引,这将很快运行。

于 2012-04-25T14:34:04.123 回答
0

重写您的查询让我:

SELECT pc.sku, pc.titel, pc.preis_vk, pc.link pc
FROM prod_combined pc   
WHERE 
(
   pc.sku IN
       (
       SELECT 
       pc2.sku FROM
       (
           SELECT pc2.sku FROM 
           prod_billiger  pb
           INNER JOIN prod_combined pc2 ON pc2.sku = pb.sku
           WHERE pb.rangGesamt = 1
           AND pc2.aktiv = 1 
       ) q 
       JOIN prod_combined pc3 ON pc3.sku = q.sku
        )
) 
AND pc.aktiv = 1
;

,这可能相当于:

SELECT pc.sku, pc.titel, pc.preis_vk, pc.link pc
FROM prod_combined  pc
WHERE pc.sku IN (
       SELECT q.sku 
       FROM (
           SELECT pc2.sku FROM 
           prod_billiger  pb
           INNER JOIN prod_combined pc2 ON pc2.sku = pb.sku
           WHERE pb.rangGesamt = 1
           AND pc2.aktiv = 1
           ) q
        )
AND pc.aktiv = 1
        ;

可以进一步简化为:

SELECT pc.sku, pc.titel, pc.preis_vk, pc.link pc
FROM prod_combined  pc
WHERE EXISTS (
       SELECT *
          FROM prod_billiger  pb
          INNER JOIN prod_combined pc2 ON pc2.sku = pb.sku 
          WHERE pb.rangGesamt = 1 AND pc2.aktiv = 1
          AND pc.sku = pb.sku;
        )
AND pc.aktiv = 1
        ;

如果“sku”是 prod_combined 的主键,子查询甚至可以进一步缩减,只包含“prod_billiger”。

SELECT pc.sku, pc.titel, pc.preis_vk, pc.link pc
FROM prod_combined  pc
WHERE EXISTS (
       SELECT *
          FROM prod_billiger  pb
          WHERE pc.sku = pb.sku;
          AND pb.rangGesamt = 1 AND pc2.aktiv = 1
        )
AND pc.aktiv = 1
        ;

这或多或少等同于:

SELECT pc.sku, pc.titel, pc.preis_vk, pc.link pc
FROM prod_combined  pc
WHERE pc.sku IN (
       SELECT sku
       FROM prod_billiger  pb
       WHERE pb.rangGesamt = 1 
       )
AND pc.aktiv = 1
        ;

如果 sku 也是 prod_billiger 的主要(或候选)键,则可以进一步简化为:

SELECT pc.sku, pc.titel, pc.preis_vk, pc.link pc
FROM prod_combined  pc
JOIN prod_billiger  pb ON pc.sku = pb.sku
WHERE pb.rangGesamt = 1
AND pc.aktiv = 1
        ;
于 2012-04-25T14:36:45.077 回答