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:
- https://www.example.org/search/results/feed/rss
- https://www.example.org/search/results/feed/rss2
- https://www.example.org/category/tech-news-post/feed
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
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:
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.
Indexing Database Table fields:
used in query.
Follow Step: https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html