簡體   English   中英

為什么我的MySQL查詢這么慢?

[英]Why is my MySQL query so slow?

Background:
entities tables currently has 14,111 records
articles table currently has 5211 records

我試圖找到所有活躍(已完成)的文章,並讓實體'谷歌'

# Finding articles that have the entity google takes:
# 4 ms
SELECT `Article`.`id` FROM `articles_entities` AS `ArticlesEntity`
LEFT JOIN `entities` AS `Entity` ON (`ArticlesEntity`.`entity_id` = `Entity`.`id`)
WHERE `Entity`.`strict` = 'google'

# Finding articles that have the entity google and is active takes:
# 1800 ms
SELECT `Article`.`id` FROM `articles_entities` AS `ArticlesEntity` 
LEFT JOIN `entities` AS `Entity` ON (`ArticlesEntity`.`entity_id` = `Entity`.`id`)
LEFT JOIN `articles` AS `Article` ON (`ArticlesEntity`.`article_id` = `Article`.`id`)
WHERE `Entity`.`strict` = 'google' AND `Article`.`state` = 'completed'

查詢花了這么長時間可能會出現什么問題?

我想補充說,數據透視表中的兩個字段都被編入索引。

在此先感謝您的幫助

UPDATE

id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE Entity ref PRIMARY,strict strict 767 const 1 Using where
1 SIMPLE ArticlesEntity ref article_id,entity_id,article_id_2 entity_id 108 b2b.Entity.id 4  
1 SIMPLE Article eq_ref PRIMARY,state PRIMARY 108 b2b.ArticlesEntity.article_id 1 Using where

Entity.strict或Article.state未編入索引。 在SELECT語句之前使用EXPLAIN並檢查哪些表正在被完全掃描。 這將暗示需要索引的內容。

你真的需要左聯盟嗎?! imho您的查詢應該在沒有它的情況下得到提升;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM