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 contain CTEs.
- The original query must make use of the following fact tables: ['catalog_returns']. Below is the join graph for these fact tables: ['catalog_returns(cr_returning_addr_sk) references customer_address(ca_address_sk);', 'catalog_returns(cr_refunded_addr_sk) references customer_address(ca_address_sk);', 'catalog_returns(cr_returning_customer_sk) references customer(c_customer_sk);']
- The original query must employ 2 joins.
- The original query must employ the following aggregate functions: ['avg'].
- The original query must employ the following operators in its WHERE clause: ['sum', 'max'].
- 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
[ORIGINAL QUERY]
with customer_address_total_return as ( select cr_returning_customer_sk as catr_customer_sk, cr_returning_addr_sk as catr_address_sk, sum(cr_return_amt) as catr_total_return from catalog_returns, date_dim where cr_returned_date_sk = d_date_sk and d_year = 2001 group by cr_returning_customer_sk, cr_returning_addr_sk ) select c_customer_id from customer_address_total_return catr1, customer_address, customer where catr1.catr_total_return > ( select avg(catr_total_return) + max(catr_total_return) from customer_address_total_return catr2 where catr1.catr_address_sk = catr2.catr_address_sk ) and ca_address_sk = catr1.catr_address_sk and catr1.catr_customer_sk = c_customer_sk order by c_customer_id limit 50;[REWRITTEN QUERY]
with customer_address_total_return as ( select cr_returning_customer_sk as catr_customer_sk, cr_returning_addr_sk as catr_address_sk, sum(cr_return_amt) as catr_total_return from catalog_returns join date_dim on cr_returned_date_sk = d_date_sk where d_year = 2001 group by cr_returning_customer_sk, cr_returning_addr_sk ), avg_max_return as ( select catr_address_sk, avg(catr_total_return) + max(catr_total_return) as avg_max_return_value from customer_address_total_return group by catr_address_sk ) select c_customer_id from customer_address_total_return catr1 join customer_address on ca_address_sk = catr1.catr_address_sk join customer on catr1.catr_customer_sk = c_customer_sk join avg_max_return amr on catr1.catr_address_sk = amr.catr_address_sk where catr1.catr_total_return > amr.avg_max_return_value order by c_customer_id limit 50;This new query pair uses the
'catalog_returns'fact table and applies aggregate functions and operators as specified. It was rewritten to move the subquery calculation into a separate CTE, mimicking the optimization pattern demonstrated in the provided example.