查询的速度问题显示具有相同模型的多个产品

时间:2014-01-16 11:10:33

标签: mysql

通过以下查询,我得到的所有产品都列出了超过1个相同的products_model。 确切地说我只想要......查询的速度很糟糕。需要26秒。 avarage在屏幕上获取数据。 products_model已编入索引。 不知道如何加快这个查询。也许有人可以帮助我。 谢谢你提前。

SELECT products_model 
FROM products
WHERE manufacturers_id = 239
  AND products_model IN (
      SELECT products_model
      FROM 
         products_guede 
      GROUP BY
         products_model
      HAVING count(products_model) > 1
    )

3 个答案:

答案 0 :(得分:1)

你可以尝试一下吗?

SELECT p.products_model
FROM products p INNER JOIN products_guede pg
 ON p.products_model = pg.products_model
WHERE p.manufacturers_id = 239
GROUP BY p.products_model
HAVING COUNT(products_model) > 1

以下索引可能会对您有所帮助。

ALTER TABLE products ADD INDEX(manufacturers_id, products_model);
ALTER TABLE products_guede ADD INDEX( products_model);

答案 1 :(得分:0)

SQLFIDDLE

select
  p.products_model
from
  products p
  left join products_guede pg 
    on p.products_model = pg.products_model
-- where manufacturers_id in (239,...) 
group by p.products_model
having count(*) > 1;

答案 2 :(得分:0)

实际上,针对另一个表的所有列检查每个单个查询结果将花费很长时间来处理大型表(超过1,000个数据记录)

请改为尝试:

SELECT count(*) as model_occurence, products_model
FROM products_guede
INNER JOIN products on products.products_model = products_guede.products_model
WHERE manufacturers_id = 239
GROUP BY products_guede.products_model;