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 case when (select count(*) from store_sales where ss_quantity between 1 and 20) > 1071 then (select avg(ss_ext_tax) from store_sales where ss_quantity between 1 and 20) else (select avg(ss_net_paid_inc_tax) from store_sales where ss_quantity between 1 and 20) end bucket1,case when (select count(*) from store_sales where ss_quantity between 21 and 40) > 39161 then (select avg(ss_ext_tax) from store_sales where ss_quantity between 21 and 40) else (select avg(ss_net_paid_inc_tax) from store_sales where ss_quantity between 21 and 40) end bucket2,case when (select count(*) from store_sales where ss_quantity between 41 and 60) > 29434 then (select avg(ss_ext_tax) from store_sales where ss_quantity between 41 and 60) else (select avg(ss_net_paid_inc_tax) from store_sales where ss_quantity between 41 and 60) end bucket3,case when (select count(*) from store_sales where ss_quantity between 61 and 80) > 6568 then (select avg(ss_ext_tax) from store_sales where ss_quantity between 61 and 80) else (select avg(ss_net_paid_inc_tax) from store_sales where ss_quantity between 61 and 80) end bucket4,case when (select count(*) from store_sales where ss_quantity between 81 and 100) > 21216 then (select avg(ss_ext_tax) from store_sales where ss_quantity between 81 and 100) else (select avg(ss_net_paid_inc_tax) from store_sales where ss_quantity between 81 and 100) end bucket5 from reason where r_reason_sk = 1 ;[REWRITTEN QUERY]
WITH bucket AS ( SELECT CASE WHEN ss_quantity BETWEEN 1 AND 20 THEN 1 WHEN ss_quantity BETWEEN 21 AND 40 THEN 2 WHEN ss_quantity BETWEEN 41 AND 60 THEN 3 WHEN ss_quantity BETWEEN 61 AND 80 THEN 4 WHEN ss_quantity BETWEEN 81 AND 100 THEN 5 END AS quantity_range, COUNT(*) AS cnt, AVG(ss_ext_tax) AS avg_ext_tax, AVG(ss_net_paid_inc_tax) AS avg_net_paid_inc_tax FROM store_sales GROUP BY CASE WHEN ss_quantity BETWEEN 1 AND 20 THEN 1 WHEN ss_quantity BETWEEN 21 AND 40 THEN 2 WHEN ss_quantity BETWEEN 41 AND 60 THEN 3 WHEN ss_quantity BETWEEN 61 AND 80 THEN 4 WHEN ss_quantity BETWEEN 81 AND 100 THEN 5 END ) SELECT CASE WHEN b1.cnt > 1071 THEN b1.avg_ext_tax ELSE b1.avg_net_paid_inc_tax END AS bucket1, CASE WHEN b2.cnt > 39161 THEN b2.avg_ext_tax ELSE b2.avg_net_paid_inc_tax END AS bucket2, CASE WHEN b3.cnt > 29434 THEN b3.avg_ext_tax ELSE b3.avg_net_paid_inc_tax END AS bucket3, CASE WHEN b4.cnt > 6568 THEN b4.avg_ext_tax ELSE b4.avg_net_paid_inc_tax END AS bucket4, CASE WHEN b5.cnt > 21216 THEN b5.avg_ext_tax ELSE b5.avg_net_paid_inc_tax END AS bucket5 FROM (SELECT * FROM bucket WHERE quantity_range = 1) b1, (SELECT * FROM bucket WHERE quantity_range = 2) b2, (SELECT * FROM bucket WHERE quantity_range = 3) b3, (SELECT * FROM bucket WHERE quantity_range = 4) b4, (SELECT * FROM bucket WHERE quantity_range = 5) b5, reason WHERE r_reason_sk = 1;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_bill_cdemo_sk) references customer_demographics(cd_demo_sk);', 'web_sales(ws_web_page_sk) references web_page(wp_web_page_sk);', 'web_sales(ws_ship_customer_sk) references customer(c_customer_sk);']
- The original query must employ 1 joins.
- The original query must employ the following aggregate functions: ['avg', 'min', 'sum'].
- The original query must employ the following operators in its WHERE clause: ['avg'].
- 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
Sure, let's create a pair of original and rewritten SQL queries using the TPC-DS schema and the constraints you've provided.
[ORIGINAL QUERY]
SELECT CASE WHEN (SELECT AVG(ws_net_profit) FROM web_sales WHERE ws_net_profit > 50) > 100 THEN (SELECT AVG(ws_ext_list_price) FROM web_sales WHERE ws_net_profit > 50) ELSE (SELECT MIN(ws_ext_discount_amt) FROM web_sales WHERE ws_net_profit > 50) END AS value1, CASE WHEN (SELECT AVG(ws_net_profit) FROM web_sales WHERE ws_net_profit > 100) < 100 THEN (SELECT AVG(ws_ext_list_price) FROM web_sales WHERE ws_net_profit > 100) ELSE (SELECT SUM(ws_ext_tax) FROM web_sales WHERE ws_net_profit > 100) END AS value2 FROM customer_demographics, web_sales WHERE ws_bill_cdemo_sk = cd_demo_sk AND cd_marital_status = 'M' AND cd_education_status = 'Advanced Degree';[REWRITTEN QUERY]
WITH profit_ranges AS ( SELECT ws_net_profit, CASE WHEN ws_net_profit > 50 AND ws_net_profit <= 100 THEN 1 WHEN ws_net_profit > 100 THEN 2 END AS profit_range, AVG(ws_ext_list_price) AS avg_list_price, MIN(ws_ext_discount_amt) AS min_discount_amt, SUM(ws_ext_tax) AS sum_tax, AVG(ws_net_profit) AS avg_net_profit FROM web_sales GROUP BY CASE WHEN ws_net_profit > 50 AND ws_net_profit <= 100 THEN 1 WHEN ws_net_profit > 100 THEN 2 END ) SELECT CASE WHEN r1.avg_net_profit > 100 THEN r1.avg_list_price ELSE r1.min_discount_amt END AS value1, CASE WHEN r2.avg_net_profit < 100 THEN r2.avg_list_price ELSE r2.sum_tax END AS value2 FROM (SELECT * FROM profit_ranges WHERE profit_range = 1) r1, (SELECT * FROM profit_ranges WHERE profit_range = 2) r2, customer_demographics WHERE cd_demo_sk = r1.profit_range AND cd_marital_status = 'M' AND cd_education_status = 'Advanced Degree';Explanation
-
Original Query: The original query uses two expressions in the
SELECTclause, each of which uses a subquery to determine which aggregate to return. The subqueries are repeated with a conditionws_net_profit > 50orws_net_profit > 100. -
Rewritten Query: The rewritten query uses a CTE to pre-calculate the aggregates by creating a profit range. This reduces redundancy by computing aggregates once and reusing those results, similar to the pattern you've provided. This typically improves performance by minimizing redundant computation and scanning through the
web_salestable only once.
-