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: ['store_sales']. Below is the join graph for these fact tables: ['store_sales(ss_hdemo_sk) references household_demographics(hd_demo_sk);', 'store_sales(ss_sold_date_sk) references date_dim(d_date_sk);', 'store_sales(ss_promo_sk) references promotion(p_promo_sk);']
- The original query must employ 1 joins.
- The original query must employ the following aggregate functions: ['max', 'sum', 'min'].
- The original query must employ the following operators in its WHERE clause: ['max'].
- 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! Below is a pair of original and rewritten SQL queries based on the TPC-DS schema, following the specified rewrite pattern and constraints:
[ORIGINAL QUERY]
with promotion_effectiveness as ( select ss_promo_sk as promo_sk, ss_sold_date_sk as sold_date, sum(ss_ext_sales_price) as total_sales, sum(ss_ext_list_price) as total_list_price from store_sales join date_dim on ss_sold_date_sk = d_date_sk where d_year = 2001 group by ss_promo_sk, ss_sold_date_sk ) select p_promo_id from promotion_effectiveness pe join promotion on pe.promo_sk = p_promo_sk where total_sales > (select max(total_sales) * 1.1 from promotion_effectiveness pe_max where pe_max.sold_date = pe.sold_date) and total_list_price = (select min(total_list_price) from promotion_effectiveness pe_min where pe_min.promo_sk = pe.promo_sk) order by p_promo_id limit 100;[REWRITTEN QUERY]
with promotion_effectiveness as ( select ss_promo_sk as promo_sk, ss_sold_date_sk as sold_date, sum(ss_ext_sales_price) as total_sales, sum(ss_ext_list_price) as total_list_price from store_sales join date_dim on ss_sold_date_sk = d_date_sk where d_year = 2001 group by ss_promo_sk, ss_sold_date_sk ), max_sales_by_date as ( select sold_date, max(total_sales) as max_sales from promotion_effectiveness group by sold_date ), min_list_price_by_promo as ( select promo_sk, min(total_list_price) as min_list_price from promotion_effectiveness group by promo_sk ) select p_promo_id from promotion_effectiveness pe join promotion on pe.promo_sk = p_promo_sk join max_sales_by_date msd on pe.sold_date = msd.sold_date join min_list_price_by_promo mlp on pe.promo_sk = mlp.promo_sk where total_sales > msd.max_sales * 1.1 and total_list_price = mlp.min_list_price order by p_promo_id limit 100;Explanation:
-
Original Query: A single CTE (
promotion_effectiveness) is used to aggregate sales and list prices for each promotion within a specific year. The main query filters promotions where sales exceed 110% of the maximum sales for that sold date, and the list price matches the minimum for that promotion. -
Rewritten Query: Additional CTEs (
max_sales_by_date,min_list_price_by_promo) break down the subqueries in the WHERE clause of the original query into separable precomputed aggregations. This improves query performance by reducing the need to repeatedly compute these values in subqueries. The rewritten query structure mirrors the pattern of extracting subqueries into separate CTEs shown in the example.
-