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', 'web_sales']. Below is the join graph for these fact tables: ['catalog_returns(cr_warehouse_sk) references warehouse(w_warehouse_sk);', 'catalog_returns(cr_returning_hdemo_sk) references household_demographics(hd_demo_sk);', 'catalog_returns(cr_call_center_sk) references call_center(cc_call_center_sk);'] ['web_sales(ws_sold_time_sk) references time_dim(t_time_sk);', 'web_sales(ws_ship_addr_sk) references customer_address(ca_address_sk);', 'web_sales(ws_web_page_sk) references web_page(wp_web_page_sk);']
- The original query must employ 4 joins.
- The original query must employ the following operators in its WHERE clause: ['count'].
- 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 product_return_count as ( select cr_call_center_sk as prc_call_center_sk, cr_warehouse_sk as prc_warehouse_sk, count(*) as prc_return_count from catalog_returns join warehouse on cr_warehouse_sk = w_warehouse_sk where w_warehouse_name = 'West Warehouse' group by cr_call_center_sk, cr_warehouse_sk ), sales_shipment as ( select ws_web_page_sk as ss_web_page_sk, ws_ship_addr_sk as ss_ship_addr_sk, count(*) as ss_shipment_count from web_sales join customer_address on ws_ship_addr_sk = ca_address_sk where ca_state = 'KY' group by ws_web_page_sk, ws_ship_addr_sk ) select wp_web_page_id from product_return_count prc1, sales_shipment ss1, web_page, call_center where prc1.prc_return_count > 100 and prc1.prc_call_center_sk = cc_call_center_sk and ss1.ss_shipment_count > 50 and wp_web_page_sk = ss1.ss_web_page_sk order by wp_web_page_id limit 100;