Features

Optimise Product Attributes Lookup

Published · Last updated

WooCommerce keeps a product attribute lookup table to make filtering and faceted searches fast. That table needs regenerating from time to time, and the regeneration runs in the background through cron and Action Scheduler. The trouble is in the query WooCommerce builds while it does this. It can end up checking both product_id and product_or_parent_id for the same value joined with an OR. An OR across two columns like that stops MySQL using its index efficiently, so instead of a quick indexed lookup you get a much heavier scan, and on a large catalogue that makes the regeneration far slower than it needs to be.

Why this matters

An OR across two different columns forces MySQL to consider rows matching either condition, and it cannot satisfy that from a single index range. It falls back to a much heavier scan. Reducing the condition to one column is what lets the index do its job.

The Optimize Product Attributes Lookup option spots the case where both product_id and product_or_parent_id refer to the same id. When they do, the product_id clause is redundant – it’s asking the same question twice – so the option removes it. With the redundant clause gone the query is a straightforward single-column lookup, and MySQL can use the index properly again.

This runs where the work actually happens: during the background attribute lookup regeneration driven by cron and Action Scheduler. You don’t need to trigger anything by hand, the optimisation applies to the regeneration as it runs.

You’ll find this in the Imports tab under “Optimize Product Attributes Lookup”.

In short: WooCommerce’s attribute lookup regeneration can build an OR across product_id and product_or_parent_id that stops MySQL using the index. When both point at the same id this option drops the redundant product_id clause so the query uses the index again, speeding up the background regeneration on large catalogues.

Further reading

keep the job tables that drive this regeneration lean
another WooCommerce query rewritten to use an index instead of a full scan
add your own indexes where MySQL still has to scan

Comments

▲
▼
1/1