Skip to content

Reduce queries while generating orders #158

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

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 40 additions & 33 deletions includes/Generator/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace WC\SmoothGenerator\Generator;

use WC\SmoothGenerator\Util\RandomRuntimeCache;

/**
* Order data generator.
*/
Expand All @@ -29,9 +31,6 @@ public static function generate( $save = true, $assoc_args = array() ) {

$order = new \WC_Order();
$customer = self::get_customer();
if ( ! $customer instanceof \WC_Customer ) {
return false;
}
$products = self::get_random_products( 1, 10 );

foreach ( $products as $product ) {
Expand Down Expand Up @@ -154,6 +153,10 @@ public static function batch( $amount, array $args = array() ) {
$order_ids[] = $order->get_id();
}

// In case multiple batches are being run in one request, refresh the cache data.
RandomRuntimeCache::clear( 'customers' );
RandomRuntimeCache::clear( 'products' );

return $order_ids;
}

Expand All @@ -163,20 +166,30 @@ public static function batch( $amount, array $args = array() ) {
* @return \WC_Customer Customer object with data populated.
*/
public static function get_customer() {
global $wpdb;
$customer = null;

$guest = (bool) wp_rand( 0, 1 );
$existing = (bool) wp_rand( 0, 1 );
if ( ! RandomRuntimeCache::exists( 'customers' ) ) {
global $wpdb;
$user_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->users} ORDER BY rand() LIMIT 100" );
RandomRuntimeCache::set( 'customers', $user_ids );
}

$guest_chances = 1;
if ( RandomRuntimeCache::count( 'customers' ) < 10 ) {
// Chance that customer is guest increases with fewer user accounts.
$guest_chances = 10 - RandomRuntimeCache::count( 'customers' );
}
$guest = (bool) wp_rand( 0, $guest_chances );

if ( $existing ) {
$total_users = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->users}" );
$offset = wp_rand( 0, $total_users );
$user_id = (int) $wpdb->get_var( "SELECT ID FROM {$wpdb->users} ORDER BY rand() LIMIT $offset, 1" ); // phpcs:ignore
return new \WC_Customer( $user_id );
if ( ! $guest ) {
RandomRuntimeCache::shuffle( 'customers' );
$customer_id = RandomRuntimeCache::get( 'customers', 1 )[0];
$customer = new \WC_Customer( $customer_id );
}

Customer::disable_emails();
$customer = Customer::generate( ! $guest );
if ( is_null( $customer ) ) {
$customer = Customer::generate( false );
}

return $customer;
}
Expand Down Expand Up @@ -272,31 +285,25 @@ private static function get_status( $assoc_args ) {
* @return array Random list of products.
*/
protected static function get_random_products( int $min_amount = 1, int $max_amount = 4 ) {
global $wpdb;

$products = array();

$num_existing_products = (int) $wpdb->get_var(
"SELECT COUNT( DISTINCT ID )
FROM {$wpdb->posts}
WHERE 1=1
AND post_type='product'
AND post_status='publish'"
);
if ( ! RandomRuntimeCache::exists( 'products' ) ) {
$query = new \WC_Product_Query( array(
'limit' => 100,
'return' => 'ids',
'orderby' => 'rand',
) );

$num_products_to_get = wp_rand( $min_amount, $max_amount );
$product_ids = $query->get_products();

if ( $num_products_to_get > $num_existing_products ) {
$num_products_to_get = $num_existing_products;
RandomRuntimeCache::set( 'products', $product_ids );
}

$query = new \WC_Product_Query( array(
'limit' => $num_products_to_get,
'return' => 'ids',
'orderby' => 'rand',
) );
RandomRuntimeCache::shuffle( 'products' );

$amount = wp_rand( $min_amount, $max_amount );
$product_ids = RandomRuntimeCache::get( 'products', $amount );
$products = array();

foreach ( $query->get_products() as $product_id ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );

if ( $product->is_type( 'variable' ) ) {
Expand Down
3 changes: 2 additions & 1 deletion includes/Generator/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public static function batch( $amount, array $args = array() ) {
}

// In case multiple batches are being run in one request, refresh the cache data.
RandomRuntimeCache::reset();
RandomRuntimeCache::clear( 'product_cat' );
RandomRuntimeCache::clear( 'product_tag' );

return $product_ids;
}
Expand Down