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 a.ca_state state,count(*) cnt from customer_address a,customer c,store_sales s,date_dim d,item i where a.ca_address_sk = c.c_current_addr_sk and c.c_customer_sk = s.ss_customer_sk and s.ss_sold_date_sk = d.d_date_sk and s.ss_item_sk = i.i_item_sk and d.d_month_seq = (select distinct (d_month_seq) from date_dim where d_year = 2002 and d_moy = 3 ) and i.i_current_price > 1.2 * (select avg(j.i_current_price) from item j where j.i_category = i.i_category) group by a.ca_state having count(*) >= 10 order by cnt,a.ca_state limit 100;[REWRITTEN QUERY]
WITH avg_price AS ( SELECT i_category, AVG(i_current_price) avg_price FROM item GROUP BY i_category ), month_seq AS ( SELECT d_month_seq FROM date_dim WHERE d_year = 2002 AND d_moy = 3 LIMIT 1 ) SELECT a.ca_state state, COUNT(*) cnt FROM customer_address a JOIN customer c ON a.ca_address_sk = c.c_current_addr_sk JOIN store_sales s ON c.c_customer_sk = s.ss_customer_sk JOIN date_dim d ON s.ss_sold_date_sk = d.d_date_sk JOIN item i ON s.ss_item_sk = i.i_item_sk JOIN avg_price ap ON i.i_category = ap.i_category JOIN month_seq ms ON d.d_month_seq = ms.d_month_seq WHERE i.i_current_price > 1.2 * ap.avg_price GROUP BY a.ca_state HAVING COUNT(*) >= 10 ORDER BY cnt, a.ca_state 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_ship_hdemo_sk) references household_demographics(hd_demo_sk);', 'web_sales(ws_ship_addr_sk) references customer_address(ca_address_sk);', 'web_sales(ws_ship_customer_sk) references customer(c_customer_sk);']
- The original query must employ 3 joins.
- The original query must employ the following operators in its WHERE clause: ['max', 'avg', 'sum'].
- 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
Here is a new pair of original and rewritten queries based on the TPC-DS schema, following the same pattern as the provided example:
[ORIGINAL QUERY]
SELECT ca.ca_state AS state, COUNT(*) AS cnt FROM web_sales ws, customer c, customer_address ca WHERE ws.ws_ship_customer_sk = c.c_customer_sk AND ws.ws_ship_addr_sk = ca.ca_address_sk AND ws.ws_ship_hdemo_sk = (SELECT hd_demo_sk FROM household_demographics WHERE hd_income_band_sk = (SELECT MAX(hd_income_band_sk) FROM household_demographics)) AND ws.ws_net_profit < 1.5 * (SELECT AVG(ws2.ws_net_profit) FROM web_sales ws2 WHERE ws2.ws_ship_customer_sk = ws.ws_ship_customer_sk) GROUP BY ca.ca_state HAVING COUNT(*) >= 5 ORDER BY cnt DESC LIMIT 50;[REWRITTEN QUERY]
WITH max_income_band AS ( SELECT MAX(hd_income_band_sk) AS max_band FROM household_demographics ), hd_demo AS ( SELECT hd_demo_sk FROM household_demographics WHERE hd_income_band_sk = (SELECT max_band FROM max_income_band) ), avg_profit AS ( SELECT ws_ship_customer_sk, AVG(ws_net_profit) AS avg_profit FROM web_sales GROUP BY ws_ship_customer_sk ) SELECT ca.ca_state AS state, COUNT(*) AS cnt FROM web_sales ws JOIN customer c ON ws.ws_ship_customer_sk = c.c_customer_sk JOIN customer_address ca ON ws.ws_ship_addr_sk = ca.ca_address_sk JOIN hd_demo ON ws.ws_ship_hdemo_sk = hd_demo.hd_demo_sk JOIN avg_profit ap ON ws.ws_ship_customer_sk = ap.ws_ship_customer_sk WHERE ws.ws_net_profit < 1.5 * ap.avg_profit GROUP BY ca.ca_state HAVING COUNT(*) >= 5 ORDER BY cnt DESC LIMIT 50;Explanation:
- Original Query: This query joins the
web_sales,customer, andcustomer_addresstables. It uses a subquery to get thehd_demo_skbased on the maximumhd_income_band_skin thehousehold_demographicstable and filtersws_net_profitusing another subquery for average net profit conditioned on the customer. - Rewritten Query: The rewritten query improves performance by using common table expressions (CTEs). The CTEs help by computing
max_income_band,hd_demo, andavg_profitin independent steps, which can then be efficiently used in the main query for joining and filtering. This follows a similar restructuring pattern as the example provided in the prompt.
- Original Query: This query joins the