0

I used the Query Monitor plugin to find slow query on my WordPress site. The below-mentioned WP_Query->get_posts() takes about 0.3446 query time out of the total Database Query time of 0.3976.

SELECT wp_posts.ID
FROM wp_posts 
LEFT JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1 
AND wp_posts.ID NOT IN (203598)
AND ( wp_term_relationships.term_taxonomy_id IN (17) 
OR wp_term_relationships.term_taxonomy_id IN (11652,20693,21952,23971,24907,24908,25928) )
AND wp_posts.post_type = 'post'
AND ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 6

I'm guessing it has something to do with the 20,000+ Tags used on posts on my site. Is that so? If yes, how do you suggest fixing this slow query? Or how should I go about deleting Tags which aren't used in more than 5 posts of all the posts on the site?

Please help.

  • The culprit is likely AND wp_posts.ID NOT IN (203598). Is this query the result of code you've written, or something else? Query Monitor should tell you which. – Jacob Peattie Jun 23 '19 at 4:17
  • @JacobPeattie Not my code. But Query Monitor tells me that this is coming from "Theme". And it just hit me that it's the "Related Posts" function of the theme which is querying the database with AND wp_posts.ID NOT IN (...) to not show the post currently being viewed in the related post section. Thanks for pointing it out. I disabled the Related post feature and it has fixed the problem. However, I do want to use it. Isn't there any workaround? Can I create a database Index for it? – Shivy23 Jun 23 '19 at 5:55
  • The post ID is already indexed. You could try finding a related posts plugin that works differently to replace it? Or talk to the theme author about a fix. – Jacob Peattie Jun 23 '19 at 7:06
  • Could you post TEXT results from MySQL command prompt of EXPLAIN SELECT SQL_NO_CACHE (remainder of your query)? – Wilson Hauck Aug 9 '19 at 0:48
0

I have encountered the same issue but with product attributes, since I had more than 20000 products and much more product attributes, the queries were very slow.

What I did was, in order to make it faster, wrote an small plugin converting tags to custom fields and then displayed those on the page, but in your case if you need the tag archive pages and its functionalities, I suppose there is no other way around it.

To avoid max_execution_time try to configure your plugin to execute tag conversion instructions in small chunks,For instance, From posts 1 to 2000 and 2000 to 4000 and so on.

Database indexing might help too.

Finally try to use a cache plugin such as wp_fastest_cache it helps in an immense degree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.