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: ['web_sales']. Below is the join graph for these fact tables: ['web_sales(ws_sold_date_sk) references date_dim(d_date_sk);', 'web_sales(ws_bill_cdemo_sk) references customer_demographics(cd_demo_sk);', 'web_sales(ws_ship_hdemo_sk) references household_demographics(hd_demo_sk);']
- The original query must employ 3 joins.
- The original query must employ the following aggregate functions: ['avg', '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
Certainly! Here's a pair of original and rewritten queries, using the TPC-DS schema with the specified requirements:
[ORIGINAL QUERY]
select ws1.ws_order_number from web_sales ws1, date_dim, customer_demographics, household_demographics where ws1.ws_sold_date_sk = d_date_sk and ws_bill_cdemo_sk = cd_demo_sk and ws_ship_hdemo_sk = hd_demo_sk and d_year = 2002 and cd_marital_status = 'M' and hd_income_band_sk = 5 and ws1.ws_wholesale_cost < (select avg(ws2.ws_wholesale_cost) from web_sales ws2 where ws1.ws_sold_date_sk = ws2.ws_sold_date_sk) and ws1.ws_quantity = (select min(ws3.ws_quantity) from web_sales ws3 where ws1.ws_sold_date_sk = ws3.ws_sold_date_sk) order by ws1.ws_order_number limit 50;[REWRITTEN QUERY]
with avg_wholesale_cost_per_date as ( select ws_sold_date_sk, avg(ws_wholesale_cost) as avg_ws_wholesale_cost from web_sales group by ws_sold_date_sk ), min_quantity_per_date as ( select ws_sold_date_sk, min(ws_quantity) as min_ws_quantity from web_sales group by ws_sold_date_sk ) select ws1.ws_order_number from web_sales ws1 join date_dim on ws1.ws_sold_date_sk = d_date_sk join customer_demographics on ws_bill_cdemo_sk = cd_demo_sk join household_demographics on ws_ship_hdemo_sk = hd_demo_sk join avg_wholesale_cost_per_date a on ws1.ws_sold_date_sk = a.ws_sold_date_sk join min_quantity_per_date m on ws1.ws_sold_date_sk = m.ws_sold_date_sk where d_year = 2002 and cd_marital_status = 'M' and hd_income_band_sk = 5 and ws1.ws_wholesale_cost < a.avg_ws_wholesale_cost and ws1.ws_quantity = m.min_ws_quantity order by ws1.ws_order_number limit 50;Explanation:
- Original Query: Joins the
web_salestable withdate_dim,customer_demographics, andhousehold_demographics. It uses subqueries to calculate the average wholesale cost and minimum quantity for each sold date to filter results. - Rewritten Query: Uses Common Table Expressions (CTEs) to precompute the average wholesale cost and minimum quantity for each sold date. The main query utilizes joins to these CTEs, allowing for a cleaner and potentially more efficient query execution similar to the approach in the provided example.
- Original Query: Joins the