[英]Optimizing a MySQL Join Query?
我有以下查詢:
select *
from products as p
left join files as f on f.contentid = p.productid
where
p.state = 'active' AND
p.price > 0 order by p.created DESC
LIMIT 2200
當前,此查詢大約需要1.7秒才能處理2200行。 我不明白的是,當我將限制從2200更改為10時,為什么這個時間不會減少,更重要的是,我可以做些什么來加快速度呢?
通過在files [contentid]
和products [productid]
上添加INDEX ,可以加快查詢速度。
嘗試:
EXPLAIN select *
from products p
left join files f on f.contentid = p.productid
where
p.state = 'active' AND
p.price > 0 order by p.created DESC
LIMIT 2200
看看你需要什么索引
通常,我只是玩查詢,以了解什么可以最大程度地減少運行時間。 如有必要,請使用關系代數解決問題,然后從那里尋找最優化的地方。 這是我的建議:
SELECT *
FROM products as p, files f
WHERE f.contentid = p.productid
AND p.state = 'active'
AND p.price > 0
ORDER BY p.created DESC
LIMIT 2200
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.