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']. Below is the join graph for these fact tables: ['web_sales(ws_bill_addr_sk) references customer_address(ca_address_sk);', 'web_sales(ws_web_site_sk) references web_site(web_site_sk);', 'web_sales(ws_item_sk) references item(i_item_sk);']
- The original query must employ 1 joins.
- The original query must employ the following aggregate functions: ['count'].
- The original query must employ the following operators in its WHERE clause: ['avg', 'sum', '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! Here is a pair of original and rewritten queries that match the criteria you provided:
[ORIGINAL QUERY]
WITH cte1 AS ( SELECT ca_address_sk FROM customer_address GROUP BY ca_address_sk HAVING avg(ca_gmt_offset) > 0 ), cte2 AS ( SELECT ws_bill_addr_sk, count(*) as order_count FROM web_sales GROUP BY ws_bill_addr_sk HAVING sum(ws_ext_sales_price) > 1000 AND max(ws_list_price) < 500 ) SELECT cte1.ca_address_sk, cte2.order_count FROM cte1 JOIN cte2 ON cte1.ca_address_sk = cte2.ws_bill_addr_sk WHERE cte1.ca_address_sk IN (SELECT ca_address_sk FROM customer_address) AND cte2.ws_bill_addr_sk IN (SELECT ca_address_sk FROM customer_address WHERE ca_city = 'Seattle');[REWRITTEN QUERY]
WITH cte2 AS ( SELECT ws_bill_addr_sk, count(*) as order_count FROM web_sales GROUP BY ws_bill_addr_sk HAVING sum(ws_ext_sales_price) > 1000 AND max(ws_list_price) < 500 ) SELECT cte2.ws_bill_addr_sk, cte2.order_count FROM cte2 WHERE cte2.ws_bill_addr_sk IN (SELECT ca_address_sk FROM customer_address WHERE ca_city = 'Seattle');Explanation:
-
Original Query:
- Two CTEs (
cte1andcte2) are defined.cte1filterscustomer_addressbased onavg(ca_gmt_offset). cte2operates onweb_sales, aggregating data and filtering withsumandmax.- The main query joins
cte1andcte2onca_address_skandws_bill_addr_sk. - It utilizes two subqueries in the WHERE clause to filter
ca_address_skincustomer_address, similar to the initial example you provided.
- Two CTEs (
-
Rewritten Query:
- The derived
cte1and redundant filtering conditions are removed. - The
WHEREclause now directly checks against relevant conditions, optimizing the query similar to the example pattern provided (removing unnecessary redundancy).
- The derived
-