skip to Main Content

Our main website is running WordPress 6.0.3 on PHP 8.0 with a MariaDB 10.x database.

We are using New Relic (NR) for system and application metrics and are noticing a particular SQL query consistently showing in the NR "Slow SQL traces" table. Upon narrowing it down further, these slow queries are coming from the WP RSS Feeds.

Some examples:

We can pretty much hit any RSS feed URL and within about 10 to 30 seconds it’ll show up in the NR "Slow SQL traces" table.

The Query looks like this behemoth:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE ?=? AND 
(((wp_posts.post_title LIKE ?) OR (wp_posts.post_excerpt LIKE ?) OR 
(wp_posts.post_content LIKE ?))) AND (wp_posts.post_password = ?) AND 
((wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?)) OR 
(wp_posts.post_type = ? AND (wp_posts.post_status = ? OR wp_posts.post_status = ?))) 
ORDER BY wp_posts.post_title LIKE ? DESC, wp_posts.post_date DESC LIMIT ?, ?

Any idea how to optimize this RSS query, or perhaps cache it?

Thanks

Jon

2

Answers


  1. Chosen as BEST ANSWER

    Update on this item:

    We found that many of the "wp_posts.post_type = ?" statements in the query I mentioned are related to our WP Custom Post Types. (Six of those statements were in regard to our custom types.)

    Since they shouldn't be queried in our case, I have removed them from the get_posts() flow via the fields:

        'exclude_from_search'   => true,
        'publicly_queryable'    => false,
    

    in the register_post_type() config. (No core WP logic was altered, just our custom post registration settings.)

    After this, the RSS query stopped showing in our NR "Slow SQL traces" table.


  2. Indexing Database Table fields:

    wp_posts:
    post_title, post_excerpt, post_content, post_password, post_type and post_date etc.
    

    used in query.
    Follow Step: https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search