Skip to content

Conversation

@layoutd
Copy link
Contributor

@layoutd layoutd commented Oct 24, 2025

Summary

Optimizes batch order generation by pre-fetching product, coupon, and customer IDs once per batch instead of querying the database for every single order.

Builds on: #182

Problem

When generating orders in batch, the generator was executing repeated queries for:

  • Customer selection: 2 queries per order (COUNT + random SELECT)
  • Product selection: 1-2 queries per order (COUNT + random query)
  • Coupon selection: 1 query per order when using --coupon-ratio

For a batch of 100 orders with coupons, this resulted in ~300 database queries.

Solution

Added batch-level caching that pre-fetches all IDs with just 3 queries at the start, then uses array_rand() for random selection throughout the batch.

Performance Impact

  • 1000 orders: 2.5 minutes → 1.1 minutes (56% faster)
  • 100 orders: ~300 queries → 3 queries
  • Scales better with larger batches

Compatibility

Maintains full backward compatibility. Single order generation continues to work as before without using the cache.

Pre-fetch product, coupon, and customer IDs once per batch to eliminate repeated database queries. This reduces query overhead significantly for large batch operations.

Performance improvements:
- Products: Single query vs N queries (one per order)
- Coupons: Single query vs N queries when using coupon-ratio
- Customers: Single query vs 2N queries (count + random select per order)

Results in 30-85% faster batch generation depending on batch size.
@layoutd layoutd self-assigned this Oct 24, 2025
@layoutd layoutd force-pushed the optimize-batch-order-generation branch from 0bb12f3 to 5332818 Compare October 24, 2025 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant