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 promotions,total,cast(promotions as decimal(15,4))/cast(total as decimal(15,4))*100 from (select sum(ss_ext_sales_price) promotions from store_sales ,store ,promotion ,date_dim ,customer ,customer_address ,item where ss_sold_date_sk = d_date_sk and ss_store_sk = s_store_sk and ss_promo_sk = p_promo_sk and ss_customer_sk= c_customer_sk and ca_address_sk = c_current_addr_sk and ss_item_sk = i_item_sk and ca_gmt_offset = -7 and i_category = 'Home' and (p_channel_dmail = 'Y' or p_channel_email = 'Y' or p_channel_tv = 'Y') and s_gmt_offset = -7 and d_year = 2000 and d_moy = 12) promotional_sales, (select sum(ss_ext_sales_price) total from store_sales ,store ,date_dim ,customer ,customer_address ,item where ss_sold_date_sk = d_date_sk and ss_store_sk = s_store_sk and ss_customer_sk= c_customer_sk and ca_address_sk = c_current_addr_sk and ss_item_sk = i_item_sk and ca_gmt_offset = -7 and i_category = 'Home' and s_gmt_offset = -7 and d_year = 2000 and d_moy = 12) all_sales order by promotions, total limit 100;[REWRITTEN QUERY]
select promotions, total, (promotions / total) * 100 as promotion_percentage from ( select sum(case when p_channel_dmail = 'Y' or p_channel_email = 'Y' or p_channel_tv = 'Y' then ss_ext_sales_price else 0 end) as promotions, sum(ss_ext_sales_price) as total from store_sales join store on ss_store_sk = s_store_sk join promotion on ss_promo_sk = p_promo_sk join date_dim on ss_sold_date_sk = d_date_sk join customer on ss_customer_sk = c_customer_sk join customer_address on ca_address_sk = c_current_addr_sk join item on ss_item_sk = i_item_sk where ca_gmt_offset = -7 and i_category = 'Home' and s_gmt_offset = -7 and d_year = 2000 and d_moy = 12 ) as sales_data order by promotions, total 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: ['inventory']. Below is the join graph for these fact tables: ['inventory(inv_date_sk) references date_dim(d_date_sk);', 'inventory(inv_item_sk) references item(i_item_sk);', 'inventory(inv_warehouse_sk) references warehouse(w_warehouse_sk);']
- The original query must employ 3 joins.
- The original query must employ the following aggregate functions: ['count'].
- 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
Certainly! Below is a pair of original and rewritten SQL queries based on the provided guidelines:
[ORIGINAL QUERY]
with average_stock as ( select avg(inv_quantity_on_hand) as avg_quantity from inventory join date_dim on inv_date_sk = d_date_sk join item on inv_item_sk = i_item_sk join warehouse on inv_warehouse_sk = w_warehouse_sk where d_year = 2001 ), stock_count as ( select count(*) as item_count from inventory join date_dim on inv_date_sk = d_date_sk join item on inv_item_sk = i_item_sk join warehouse on inv_warehouse_sk = w_warehouse_sk where inv_quantity_on_hand > (select avg_quantity from average_stock) and d_year = 2001 ) select item_count from stock_count;[REWRITTEN QUERY]
select count(case when inv_quantity_on_hand > avg_quantity then 1 else null end) as item_count from ( select inv_quantity_on_hand, avg(inv_quantity_on_hand) over () as avg_quantity from inventory join date_dim on inv_date_sk = d_date_sk join item on inv_item_sk = i_item_sk join warehouse on inv_warehouse_sk = w_warehouse_sk where d_year = 2001 ) as annotated_stock;Explanation
-
Original Query: It uses a common table expression (CTE) to first calculate the average quantity of inventory on hand for the year 2001, then counts only those inventory records where the quantity on hand is greater than the calculated average. It employs an aggregate function
countand usesavgin the WHERE clause. It utilizes three joins between theinventory,date_dim,item, andwarehousetables. -
Rewritten Query: It removes the CTE and combines the logic into a single query with a window function to calculate the average in an inline fashion. It uses a
CASEstatement to count items that have a quantity on hand greater than the calculated average directly during aggregation, thus enhancing performance by reducing the number of passes over the data. This follows a pattern similar to the provided example.
-