Php 为什么我的MySQL查询这么慢?

Php 为什么我的MySQL查询这么慢?,php,mysql,query-optimization,Php,Mysql,Query Optimization,我正在尝试查找所有活动(已完成)且实体为“谷歌”的文章。 Background: entities tables currently has 14,111 records articles table currently has 5211 records 查询花费这么长时间可能有什么问题 我想补充一点,pivot表中的两个字段都被索引 提前谢谢你的帮助 更新 Entity.strict或Article.state未编制索引。 在SELECT语句之前使用EXPLAIN并检查哪些表正在被完全扫描。

我正在尝试查找所有活动(已完成)且实体为“谷歌”的文章。

Background:
entities tables currently has 14,111 records
articles table currently has 5211 records
查询花费这么长时间可能有什么问题

我想补充一点,pivot表中的两个字段都被索引

提前谢谢你的帮助

更新


Entity.strict或Article.state未编制索引。
在SELECT语句之前使用EXPLAIN并检查哪些表正在被完全扫描。这将提示什么需要索引。

您真的需要左连接吗?!我知道没有它,您的查询应该得到提升;)

每个表上都有哪些索引,EXPLAIN告诉了您什么?查询返回多少行?
# 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'
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