如何在Wordpress中找到慢查询的来源?


7

我有一个WordPress网站,其中有8000多个帖子,每当我添加一个新的WordPress站点时,该站点就会变得无响应。我检查了MySQL慢查询日志,发现它正在执行一次选择,该选择返回posts表中的大多数行,并且花费大量时间来执行。

这是一个例子:

Query_time: 149.702704  
Lock_time: 0.000078  
Rows_sent: 4699  
Rows_examined: 9398  
Rows_affected: 0  
Rows_read: 4699
use 488726_wp;

SELECT `ID`, `post_author`, `post_date`, `post_date_gmt`, `post_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `post_type`
    FROM `wp_posts` 
        WHERE ( (post_status = 'publish' AND (post_type = 'post' OR post_type = ''))  
            OR  (post_status = 'publish' AND post_type = 'page') )  
        AND post_password='' 
        ORDER BY post_modified DESC;

如何找到这些查询的来源?

Answers:



2

您可能想要做的是对查询执行EXPLAIN,如下所示:

EXPLAIN SELECT ID, post_author, post_date,
post_date_gmt, post_status, post_name,
post_modified, post_modified_gmt, post_parent,
post_type FROM wp_posts
WHERE ( (post_status = 'publish' AND (post_type = 'post' OR post_type = ''))
OR (post_status = 'publish' AND post_type = 'page') )
AND post_password='' ORDER BY post_modified DESC;

这将显示MySQL采取的访问模式正在收集您需要的数据。

但是,仅盯着WHERE和ORDER BY子句,我想提出以下建议:创建可以帮助加快查询速度的索引。由于post_status和post_type在查询中具有静态值,并且post_modified提供了这两列的排序顺序,请尝试使用此索引:

ALTER TABLE wp_posts ADD INDEX (post_status,post_type,post_modified);

试试看 !!!



0

尝试使用以下插件Query Interface,它将使您显示查询并为您提供一个接口,您还可以在其中通过检查查询的索引或说明来检查查询的内容。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.