How can I find the source of slow queries in WordPress?

I have a WordPress site with more than 8000 posts and everytime I add a new one the site becomes unresponsive. I checked the MySQL slow queries log and found out that it is performing a select that returns most of the rows in the posts table and is taking a lot of time to execute.

This is an example:

Read More
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;

How can I find the source of these queries?

Related posts

Leave a Reply

4 comments

  1. What you may want to do is perform an EXPLAIN on the query like this:

    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;
    

    This will reveal the access pattern taken by MySQL is gathering the data you need.

    However, just staring at the WHERE and ORDER BY clauses, I would like to make thie following suggestion: create an index that can help the query speed up. Since post_status and post_type have static values in the query and post_modified present a sort order for those two columns, try this index please:

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

    Give it a Try !!!