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]
SELECT c.* FROM customer c WHERE c.address_id IN (SELECT a.address_id FROM address) and c.address_id in (SELECT a.address_id FROM address WHERE a.pin_code = '560012');[REWRITTEN QUERY]
SELECT c.* FROM customer c WHERE c.address_id IN (SELECT a.address_id FROM address WHERE a.pin_code = '560012');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: ['web_sales', 'catalog_returns']. Below is the join graph for these fact tables: ['web_sales(ws_bill_customer_sk) references customer(c_customer_sk);', 'web_sales(ws_sold_date_sk) references date_dim(d_date_sk);', 'web_sales(ws_ship_hdemo_sk) references household_demographics(hd_demo_sk);'] ['catalog_returns(cr_refunded_hdemo_sk) references household_demographics(hd_demo_sk);', 'catalog_returns(cr_warehouse_sk) references warehouse(w_warehouse_sk);', 'catalog_returns(cr_ship_mode_sk) references ship_mode(sm_ship_mode_sk);']
- The original query must employ 2 joins.
- 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 is a pair of original and rewritten SQL queries following the guidelines and rewrite pattern provided.
[ORIGINAL QUERY]
WITH high_value_customers AS ( SELECT c_customer_sk FROM customer WHERE c_current_cdemo_sk IN ( SELECT cd_demo_sk FROM customer_demographics WHERE cd_marital_status = 'M' ) ), refunded_web_sales AS ( SELECT ws_order_number FROM web_sales WHERE ws_order_number IN ( SELECT cr_order_number FROM catalog_returns ) ) SELECT ws.* FROM web_sales ws JOIN high_value_customers hc ON ws.ws_bill_customer_sk = hc.c_customer_sk JOIN refunded_web_sales rws ON ws.ws_order_number = rws.ws_order_number WHERE ws.ws_sold_date_sk IN (SELECT d_date_sk FROM date_dim WHERE d_year = 2022);[REWRITTEN QUERY]
WITH high_value_customers AS ( SELECT c_customer_sk FROM customer WHERE c_current_cdemo_sk IN ( SELECT cd_demo_sk FROM customer_demographics WHERE cd_marital_status = 'M' ) ) SELECT ws.* FROM web_sales ws JOIN high_value_customers hc ON ws.ws_bill_customer_sk = hc.c_customer_sk WHERE ws.ws_order_number IN ( SELECT cr_order_number FROM catalog_returns ) AND ws.ws_sold_date_sk IN (SELECT d_date_sk FROM date_dim WHERE d_year = 2022);Explanation of rewrite:
- The original query used a CTE
refunded_web_salesto compute web sales that have corresponding returns in thecatalog_returnstable. - In the rewritten query, we simplify this by directly placing the condition related to
catalog_returnsin theWHEREclause of the main query. - Thus, the unnecessary inner join with CTE
refunded_web_salesis removed, similar to eliminating redundant subqueries from the original pattern presented.
- The original query used a CTE