Skip to content

Commit 4ff758a

Browse files
committed
Update with screenshot
1 parent ac3d517 commit 4ff758a

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

articles/sql-data-warehouse/sql-data-warehouse-materialized-view-best-practices.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ SELECT c_customer_id customer_id
155155
,sum(isnull(ss_ext_list_price-ss_ext_wholesale_cost-ss_ext_discount_amt+ss_ext_sales_price, 0)/2) year_total
156156
,'s' sale_type
157157
FROM customer
158-
,store_sales_partitioned
158+
,store_sales
159159
,date_dim
160160
WHERE c_customer_sk = ss_customer_sk
161161
AND ss_sold_date_sk = d_date_sk
@@ -179,7 +179,7 @@ SELECT c_customer_id customer_id
179179
,sum(isnull(cs_ext_list_price-cs_ext_wholesale_cost-cs_ext_discount_amt+cs_ext_sales_price, 0)/2) year_total
180180
,'c' sale_type
181181
FROM customer
182-
,catalog_sales_partitioned
182+
,catalog_sales
183183
,date_dim
184184
WHERE c_customer_sk = cs_bill_customer_sk
185185
AND cs_sold_date_sk = d_date_sk
@@ -203,7 +203,7 @@ SELECT c_customer_id customer_id
203203
,sum(isnull(ws_ext_list_price-ws_ext_wholesale_cost-ws_ext_discount_amt+ws_ext_sales_price, 0)/2) year_total
204204
,'w' sale_type
205205
FROM customer
206-
,web_sales_partitioned
206+
,web_sales
207207
,date_dim
208208
WHERE c_customer_sk = ws_bill_customer_sk
209209
AND ws_sold_date_sk = d_date_sk
@@ -258,7 +258,7 @@ ORDER BY t_s_secyear.customer_id
258258
OPTION ( LABEL = 'Query04-af359846-253-3');
259259
```
260260

261-
Check the estimated execution plan for this query. There are 18 shuffles and 17 joins operations that take more time to execution. Now create one materialized view for each sub-SELECT statement.
261+
Check the query's estimated execution plan. There are 18 shuffles and 17 joins operations which take more time to execute. Now let's create one materialized view for each of the three sub-SELECT statements.
262262

263263
```sql
264264
CREATE materialized view nbViewSS WITH (DISTRIBUTION=HASH(customer_id)) AS
@@ -285,7 +285,7 @@ GROUP BY c_customer_id
285285
,c_login
286286
,c_email_address
287287
,d_year
288-
288+
GO
289289
CREATE materialized view nbViewCS WITH (DISTRIBUTION=HASH(customer_id)) AS
290290
SELECT c_customer_id customer_id
291291
,c_first_name customer_first_name
@@ -311,7 +311,7 @@ GROUP BY c_customer_id
311311
,c_email_address
312312
,d_year
313313

314-
314+
GO
315315
CREATE materialized view nbViewWS WITH (DISTRIBUTION=HASH(customer_id)) AS
316316
SELECT c_customer_id customer_id
317317
,c_first_name customer_first_name
@@ -338,5 +338,9 @@ GROUP BY c_customer_id
338338
,d_year
339339

340340
```
341-
Check the original query's estimated execution plan again. The number of shuffles changes from 18 to # and the number of joins changes from 17 to #. Hover over the join operator icons in the plan, the Output List shows the data is produced from the materialized views instead of base tables. By getting data from materialized views, the query execution has fewer expensive operations and finishes much faster with no code change.
341+
Check the execution plan of the original query again. Now the number of joins changes from 17 to 5 and there's no shuffle anymore. Click the Filter operation icon in the plan, its Output List shows the data is read from the materialized views instead of base tables.
342+
343+
![Plan_Output_List_with_Materialized_Views](media/Output_List_MVs.png)
344+
345+
With materialized views, the same query runs much faster without any code change.
342346

media/Output_List_MVs.png

18.3 KB
Loading

0 commit comments

Comments
 (0)