两个相似的WP查询中的一个非常慢

时间:2019-09-04 作者:cloudinstone

我有两个WP查询都很相似。一个很快,另一个很慢。我已经通过mysql直接测试了两个查询。

最快的一个:SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_posts.post_date >= \'2019-09-04 21:45:00\' ) AND ( ( ( wp_postmeta.meta_key = \'wpcm_home_club\' AND wp_postmeta.meta_value = \'37358\' ) OR ( wp_postmeta.meta_key = \'wpcm_away_club\' AND wp_postmeta.meta_value = \'37358\' ) ) ) AND wp_posts.post_type = \'wpcm_match\' AND ((wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'future\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC LIMIT 0, 500

下面的慢一点:

SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND ( wp_posts.post_date < \'2019-09-04 21:45:00\' ) AND ( ( ( wp_postmeta.meta_key = \'wpcm_home_club\' AND wp_postmeta.meta_value = \'37358\' ) OR ( wp_postmeta.meta_key = \'wpcm_away_club\' AND wp_postmeta.meta_value = \'37358\' ) ) ) AND wp_posts.post_type = \'wpcm_match\' AND ((wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'future\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 1

我很好奇为什么另一个这么慢?

1 个回复
SO网友:Wilson Hauck

可能只有很少的行(wp\\u posts.post\\u date>=“2019-09-04 21:45:00”)可能有数千行(wp\\u posts.post\\u date<;“2019-09-04 21:45:00”)需要进行表格扫描。解释为每个查询选择SQL\\u NO\\u缓存剩余部分,并仔细检查结果。发布每个EXPLAIN的文本结果,选择SQL\\u NO\\u缓存查询的其余部分,以帮助我们了解发生了什么。