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', 'inventory', 'catalog_returns']. Below is the join graph for these fact tables: ['catalog_sales(cs_bill_hdemo_sk) references household_demographics(hd_demo_sk);', 'catalog_sales(cs_ship_cdemo_sk) references customer_demographics(cd_demo_sk);', 'catalog_sales(cs_bill_cdemo_sk) references customer_demographics(cd_demo_sk);'] ['inventory(inv_item_sk) references item(i_item_sk);', 'inventory(inv_date_sk) references date_dim(d_date_sk);', 'inventory(inv_warehouse_sk) references warehouse(w_warehouse_sk);'] ['catalog_returns(cr_item_sk) references item(i_item_sk);', 'catalog_returns(cr_returning_cdemo_sk) references customer_demographics(cd_demo_sk);', 'catalog_returns(cr_refunded_addr_sk) references customer_address(ca_address_sk);']
- The original query must employ 4 joins.
- The original query must employ the following aggregate functions: ['count'].
- The original query must employ the following operators in its WHERE clause: ['max', 'avg', 'sum'].
- Assistant
Here's a new pair of original and rewritten queries based on the given guidelines:
[ORIGINAL QUERY]
select i_item_id from catalog_sales, catalog_returns, inventory, item where cs_item_sk = i_item_sk and cr_item_sk = i_item_sk and inv_item_sk = i_item_sk and inv_date_sk = (select max(d_date_sk) from date_dim where d_year = 2021) and cs_warehouse_sk = inv_warehouse_sk and cs_sold_date_sk = inv_date_sk group by i_item_id having count(cs_item_sk) > (select avg(cs_quantity) * 1.5 from catalog_sales) order by i_item_id limit 50;