Slow DB Query
-
I have this issue come up.
This one took 4 seconds on our DB server.
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 ) WHERE 1=1 AND wp_posts.post_type = ‘product’ AND ((wp_posts.post_status = ‘publish’)) AND (
( wp_postmeta.meta_key = ‘_visibility’ AND CAST(wp_postmeta.meta_value AS CHAR) IN (‘catalog’,’visible’) )
AND
( mt1.meta_key = ‘_featured’ AND CAST(mt1.meta_value AS CHAR) = ‘yes’ )
) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 4;The main issue is the 2nd “INNER JOIN” of the wp_postmeta table as mt1 and any reference to the mt1 table. From my testing, changing it to this query: shaves off about 3 seconds.
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.post_type = ‘product’ AND (wp_posts.post_status = ‘publish’) AND (
( wp_postmeta.meta_key = ‘_visibility’ AND CAST(wp_postmeta.meta_value AS CHAR) IN (‘catalog’,’visible’) )
OR
( wp_postmeta.meta_key = ‘_featured’ AND CAST(wp_postmeta.meta_value AS CHAR) = ‘yes’ )
) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 4;Key changes are removal of the 2nd INNER JOIN and changing the mt1 references to the wp_postmeta and changing the where to use OR instead of AND.
This is from MySQL query logging to see the delay on a WooCommerce store.
Cheers
Luke
- The topic ‘Slow DB Query’ is closed to new replies.