You are a database expert and SQL optimizer. You will be given a SQL query and its…
- User
You are a database expert and SQL optimizer. You will be given a SQL query and its rewritten variant. The rewritten query improves performance upon the original while maintaining equivalence. Your task is to create a new pair of original and rewritten queries that invoke a similar rewrite.
[ORIGINAL QUERY]
with customer_total_return as (select sr_customer_sk as ctr_customer_sk,sr_store_sk as ctr_store_sk,sum(SR_FEE) as ctr_total_return from store_returns,date_dim where sr_returned_date_sk = d_date_sk and d_year =2000 group by sr_customer_sk,sr_store_sk) select c_customer_id from customer_total_return ctr1,store,customer where ctr1.ctr_total_return > (select avg(ctr_total_return)*1.2 from customer_total_return ctr2 where ctr1.ctr_store_sk = ctr2.ctr_store_sk) and s_store_sk = ctr1.ctr_store_sk and s_state = 'TN' and ctr1.ctr_customer_sk = c_customer_sk order by c_customer_id limit 100;[REWRITTEN QUERY]
with customer_total_return as ( select sr_customer_sk as ctr_customer_sk, sr_store_sk as ctr_store_sk, sum(SR_FEE) as ctr_total_return from store_returns join date_dim on sr_returned_date_sk = d_date_sk where d_year = 2000 group by sr_customer_sk , sr_store_sk ), avg_customer_return as ( select ctr_store_sk, avg(ctr_total_return) as avg_return from customer_total_return group by ctr_store_sk ) select c_customer_id from customer_total_return ctr1 join store on s_store_sk = ctr1.ctr_store_sk join customer on ctr1.ctr_customer_sk = c_customer_sk join avg_customer_return acr on ctr1.ctr_store_sk = acr.ctr_store_sk where ctr1.ctr_total_return > acr.avg_return * 1.2 and s_state = 'TN' order by c_customer_id limit 100;Now, consider the TPC-DS schema and create a new original and equivalent rewritten query pair such that the rewrite operation is similar to the provided demonstration.Keep the following points in mind while creating the new query pair:
- The original query must not have any CTEs.
- The original query must make use of the following fact tables: ['catalog_sales', 'web_sales']. Below is the join graph for these fact tables: ['catalog_sales(cs_promo_sk) references promotion(p_promo_sk);', 'catalog_sales(cs_catalog_page_sk) references catalog_page(cp_catalog_page_sk);', 'catalog_sales(cs_call_center_sk) references call_center(cc_call_center_sk);'] ['web_sales(ws_web_site_sk) references web_site(web_site_sk);', 'web_sales(ws_ship_hdemo_sk) references household_demographics(hd_demo_sk);', 'web_sales(ws_ship_customer_sk) references customer(c_customer_sk);']
- The original query must employ 1 joins.
- The original query must employ the following aggregate functions: ['count', 'min'].
- The original query must employ the following operators in its WHERE clause: [].
- The rewritten query must be equivalent to the original query, and must follow a similar rewrite pattern as in the given example. Enclose the original and rewritten queries within
and.
- Assistant
Here is the new pair of original and rewritten queries following the specified instructions and similar optimization pattern:
[ORIGINAL QUERY]
select ws_ship_customer_sk, count(ws_order_number) as num_orders from web_sales group by ws_ship_customer_sk having count(ws_order_number) > ( select min(min_orders) from ( select count(ws_order_number) as min_orders from web_sales group by ws_ship_customer_sk ) as subquery )[REWRITTEN QUERY]
with customer_order_count as ( select ws_ship_customer_sk, count(ws_order_number) as num_orders from web_sales group by ws_ship_customer_sk ), min_customer_order as ( select min(num_orders) as min_orders from customer_order_count ) select ws_ship_customer_sk, num_orders from customer_order_count join min_customer_order on customer_order_count.num_orders > min_customer_order.min_ordersExplanation:
-
Original Query:
- It aggregates
web_salesbyws_ship_customer_sk, counting the number of orders. - It retrieves customer keys only if their order count exceeds the minimum of the order counts of all customers, calculated via a nested subquery.
- It aggregates
-
Rewritten Query:
- It introduces two CTEs:
customer_order_count: Computes the count of orders per customer.min_customer_order: Computes the minimum order count across customers using a separate aggregation.
- The final selection joins these CTEs to filter customers with order count above the minimum order count, similar to the original logic.
- It introduces two CTEs:
This revised pair adopts the CTE method to separate concerns, improving query clarity and potentially performance, similar to the transformation shown in the provided example.
-