Slow wp_posts and wp_postmeta query using Advance Custom Fields

I have a particularly slow query (around 2.2 seconds) getting custom posts (Previews) which are for an event after a certain date and are set as featured (both custom fields stored in wp_postmeta). The query is as below:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.ID = wp_postmeta.post_id )
INNER JOIN wp_postmeta AS mt1
ON ( wp_posts.ID = mt1.post_id )
INNER JOIN wp_postmeta AS mt2
ON ( wp_posts.ID = mt2.post_id )
WHERE 1=1
AND ( wp_postmeta.meta_key = ‘race_datetime’
AND ( ( mt1.meta_key = ‘race_datetime’
AND CAST(mt1.meta_value AS DATETIME) >= ‘2018-04-17 17:06’ )
AND ( mt2.meta_key = ‘featured’
AND mt2.meta_value = ‘1’ ) ) )
AND wp_posts.post_type = ‘previews’
AND ((wp_posts.post_status = ‘publish’))
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value ASC
LIMIT 0, 7

I’ve tried looking into the query but I’m no SQL expert and can’t seem to work out a way of making the query quicker.

Does anyone have any suggestions on why its so slow and what I could do to improve it?

Read more here:: Slow wp_posts and wp_postmeta query using Advance Custom Fields

Leave a Reply

Your email address will not be published. Required fields are marked *