Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow WP_Query to preload post data, and meta in wc_get_products() #12289

Merged
merged 1 commit into from Nov 9, 2016
Merged

Allow WP_Query to preload post data, and meta in wc_get_products() #12289

merged 1 commit into from Nov 9, 2016

Conversation

leewillis77
Copy link
Contributor

wc_get_products() runs a WP_Query to fetch the relevant post IDs only, and then (as long as the user hasn't specifically requested just the IDs) loads each post / product individually.

This results in a significant increase in the number of database queries executed. This change allows WP_Query to fetch the post & meta information as well, dramatically reducing the number of queries executed.

The 'fetch-IDs-only' optimisation to WP_Query is retained as long as the user has asked for only IDs to be returned from wc_get_products().

With the following test script on my dev box:

<?php

$args = array(
	'status'      => array( 'publish' ),
	'limit'       => 20,
	'offset'      => 0,
	'return'      => 'objects',
);
wc_get_products($args);

Before this change, the code above executes 158 database queries. After this PR that is reduced to 100 queries.

If you combine it with the changes in #12288 then that number comes down to 42 queries.

Allow WP_Query to bulk query for post data and meta if more than
just IDs are requested from wc_get_products(). Reduces query count
significantly.
@leewillis77
Copy link
Contributor Author

Some more context. I'm porting the Google Product Feed extensions to use wc_get_products() and these two PRs reduce the queries on my test setup from 1,478 down to 457.

There's still an issue somewhere though as the current version only runs 274 queries. I'll keep investigating.

@claudiosanches claudiosanches added this to the 2.7 milestone Nov 9, 2016
@claudiosanches claudiosanches added [Status] Needs Review focus: performance The issue/PR is related to performance. labels Nov 9, 2016
@justinshreve justinshreve merged commit 1fb8479 into woocommerce:product-crud Nov 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
focus: performance The issue/PR is related to performance.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants