繁体   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