Skip to content

Rewrite the very slow query for the big store #3146

@kevin25

Description

@kevin25

My store has 50k variable products and we've received thousands of slow queries. They also hit the CPU upto 400+ %.

Can you please rewrite it to make it less painful?

`SELECT wp_posts.ID
FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) LEFT JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '_wc_gla_visibility' ) LEFT JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) LEFT JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id AND mt2.meta_key = '_wc_gla_errors' ) LEFT JOIN wp_postmeta AS mt3 ON ( wp_posts.ID = mt3.post_id ) LEFT JOIN wp_postmeta AS mt4 ON ( wp_posts.ID = mt4.post_id )
WHERE 1=1 AND (
(
wp_term_relationships.term_taxonomy_id IN (2,4)
OR
NOT EXISTS (
SELECT 1
FROM wp_term_relationships
INNER JOIN wp_term_taxonomy
ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
WHERE wp_term_taxonomy.taxonomy = 'product_type'
AND wp_term_relationships.object_id = wp_posts.ID
)
)
) AND (
(
wp_postmeta.post_id IS NULL
OR
( mt1.meta_key = '_wc_gla_visibility' AND mt1.meta_value != 'dont-sync-and-show' )
)
AND
(
mt2.post_id IS NULL
OR
( mt3.meta_key = '_wc_gla_errors' AND mt3.meta_value = '' )
)
AND
(
( mt4.meta_key = '_wc_gla_synced_at' AND mt4.meta_value < '1760156945' )
)
) AND wp_posts.post_type IN ('product', 'product_variation') AND ((wp_posts.post_status = 'publish' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private'))
GROUP BY wp_posts.ID

				 LIMIT 25000, 100;

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions