Presentation is loading. Please wait.

Presentation is loading. Please wait.

9 Copyright © 2005, Oracle. All rights reserved. Materialized Views.

Similar presentations


Presentation on theme: "9 Copyright © 2005, Oracle. All rights reserved. Materialized Views."— Presentation transcript:

1 9 Copyright © 2005, Oracle. All rights reserved. Materialized Views

2 9-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe how summaries can be used to improve performance Differentiate materialized view types Explain materialized view integrity Create a materialized view List globalization implications for materialized views

3 9-3 Copyright © 2005, Oracle. All rights reserved. The Need for Summary Management How can you improve query response time? –Use indexes. –Partition your data. –Implement parallel execution. What about precomputing query results? Create summaries: Materialized views –Automatically rewrite SQL applications. –Automatically refresh data.

4 9-4 Copyright © 2005, Oracle. All rights reserved. Full Notes Page

5 9-5 Copyright © 2005, Oracle. All rights reserved. Using Summaries to Improve Performance Special types of aggregate views Improve query execution time by precalculating expensive joins and aggregation operations before execution and storing results in a database table Created using a schema object called a materialized view

6 9-6 Copyright © 2005, Oracle. All rights reserved. Using Summaries Original query by user: DBA creates summary table: New query by user using summary table: SELECT * FROM cust_sales_sum; SELECT c.cust_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_id; CREATE TABLE cust_sales_sum AS SELECT c.cust_id, SUM(amount_sold) AS amount FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_id;

7 9-7 Copyright © 2005, Oracle. All rights reserved. Using Materialized Views for Summary Management DBA creates materialized view: User issues original query: Query is rewritten by the Oracle server: SELECT * FROM cust_sales_mv; CREATE MATERIALIZED VIEW cust_sales_mv ENABLE QUERY REWRITE AS SELECT c.cust_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_id; SELECT c.cust_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_id;

8 9-8 Copyright © 2005, Oracle. All rights reserved. Using Materialized Views for Summary Management DBA creates materialized view. End user queries tables and views. Oracle server rewrites SQL query to use materialized view

9 9-9 Copyright © 2005, Oracle. All rights reserved. Summary Management Components Mechanisms to define materialized views and dimensions Refresh mechanism to ensure that materialized views contain the latest data Query rewrite capability to transparently rewrite a query to use a materialized view SQL Access Advisor: Recommends materialized views and indexes to be created DBMS_ADVISOR.TUNE_MVIEW procedure: Shows you how to make your materialized view fast refreshable and use general query rewrite

10 9-10 Copyright © 2005, Oracle. All rights reserved. Using Summary Management 1.Use the SQL Access Advisor to determine how you will use materialized views. 2.Create materialized views and design how queries will be rewritten. 3.Use DBMS_ADVISOR.TUNE_MVIEW to obtain an optimized materialized view as necessary.

11 9-11 Copyright © 2005, Oracle. All rights reserved. How Many Materialized Views? Query rewrite chooses which materialized view to use. One materialized view per query: –Ideal for query performance –Consumes too much disk space –Not recommended One materialized view for multiple queries: –One materialized view can be used to satisfy multiple queries –Less disk space needed –Less time needed to maintain materialized views

12 9-12 Copyright © 2005, Oracle. All rights reserved. One Materialized View for Multiple Queries CREATE MATERIALIZED VIEW cust_sales_mv2 ENABLE QUERY REWRITE AS SELECT cust_last_name, channel_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_last_name, s.channel_id; SELECT cust_last_name, channel_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_last_name, s.channel_id; SELECT * FROM cust_sales_mv2; Query rewrite

13 9-13 Copyright © 2005, Oracle. All rights reserved. One Materialized View for Multiple Queries CREATE MATERIALIZED VIEW cust_sales_mv2 ENABLE QUERY REWRITE AS SELECT cust_last_name, channel_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_last_name, s.channel_id; SELECT cust_last_name, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY cust_last_name; SELECT cust_last_name, SUM(amount) FROM cust_sales_mv2 GROUP BY cust_id; Query rewrite

14 9-14 Copyright © 2005, Oracle. All rights reserved. One Materialized View for Multiple Queries CREATE MATERIALIZED VIEW cust_sales_mv2 ENABLE QUERY REWRITE AS SELECT cust_last_name, channel_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_last_name, s.channel_id; SELECT channel_id, SUM(amount_sold) FROM sales GROUP BY channel_id; SELECT channel_id, SUM(amount) FROM cust_sales_mv2 GROUP BY channel_id; Query rewrite

15 9-15 Copyright © 2005, Oracle. All rights reserved. Determining Which Materialized View to Create One materialized view can be used to satisfy multiple queries. Multiple materialized views can satisfy the same query. A balance between performance and space usage must be found. Which materialized view should you create? –Analyze your workload. –Use the SQL Access Advisor. –Use DBMS_MVIEW.EXPLAIN_REWRITE to see why a materialized view is used or ignored.

16 9-16 Copyright © 2005, Oracle. All rights reserved. Types of Materialized Views Materialized views with aggregates: Materialized views containing only joins: CREATE MATERIALIZED VIEW cust_sales_mv AS ENABLE QUERY REWRITE AS SELECT c.cust_id, s.channel_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_id, s.channel_id; CREATE MATERIALIZED VIEW sales_products_mv AS ENABLE QUERY REWRITE AS SELECT s.time_id, p.prod_name FROM sales s, products p WHERE s.prod_id = p.prod_id(+);

17 9-17 Copyright © 2005, Oracle. All rights reserved. The Need for Nested Materialized Views Typical data warehouse need: Create aggregate materialized views using different grouping columns on a single join. Maintaining the materialized views is time consuming because the underlying join is performed multiple times. SALES_TIME_PROD_MV PRODUCTS SALES TIMES SALES_PROD_TIME_MV Same tables joined Different grouping columns PRODUCTS SALES TIMES

18 9-18 Copyright © 2005, Oracle. All rights reserved. Using Nested Materialized Views Definition is based on another materialized view. Definition can also reference normal tables. PRODUCTS SALES TIMES SALES_PROD_TIME_JOIN SALES_TIME_PROD_MV SALES_PROD_TIME_MV Tables are joined one time.

19 9-19 Copyright © 2005, Oracle. All rights reserved. Nested Materialized Views: Restrictions A nested materialized view cannot be a parent and a grandparent as shown: SALES TIMES SALES_TIMES_JOIN PRODUCTS SALES_TIME_PROD_SUM Parent of PRODUCTS Grandparent of PRODUCTS

20 9-20 Copyright © 2005, Oracle. All rights reserved. Materialized View: Example CREATE MATERIALIZED VIEW cust_sales_mv PCTFREE 0 TABLESPACE example STORAGE (INITIAL 1M NEXT 1M PCTINCREASE 0) BUILD DEFERRED REFRESH COMPLETE ENABLE QUERY REWRITE AS SELECT c.cust_id, s.channel_id, SUM(amount_sold) FROM sales s, customers c WHERE s.cust_id = c.cust_id GROUP BY c.cust_id, s.channel_id ORDER BY c.cust_id, s.channel_id; Name Storage options When to build it How to refresh the data Use this for query rewrite Detail query Detail tables MV keys

21 9-21 Copyright © 2005, Oracle. All rights reserved. Materialized Views Storage When a materialized view is created, the following objects are created: –A container table to store the materialized view rows –The materialized view itself –One index for materialized views with aggregates only The OBJECT_TYPE column of DBA_OBJECTS contains MATERIALIZED VIEW for the object. The container table has the same name as the materialized view object. A container table can be prebuilt.

22 9-22 Copyright © 2005, Oracle. All rights reserved. Estimating Materialized View Size > VARIABLE num_rows NUMBER; > VARIABLE mv_size NUMBER; EXEC DBMS_MVIEW.ESTIMATE_MVIEW_SIZE( - 'simple_store', - 'SELECT c.cust_state_province, ' ||- ' SUM(amount_sold) ' ||- 'FROM sales s, customers c ' ||- 'WHERE s.cust_id= c.cust_id ' ||- 'GROUP BY c.cust_state_province', - :num_rows, :mv_size); Estimated rows Estimated storage in bytes Statement ID for EXPLAIN PLAN

23 9-23 Copyright © 2005, Oracle. All rights reserved. Specifying Build Methods Two build methods are available when creating the materialized view: – BUILD DEFERRED : Created but not populated – BUILD IMMEDIATE : Created and populated The BUILD_MODE column in DBA_MVIEWS contains the method used.

24 9-24 Copyright © 2005, Oracle. All rights reserved. Specifying Refresh Options Specify how the materialized view should be refreshed from the detail tables: – COMPLETE – FAST – FORCE – NEVER The REFRESH_METHOD column in DBA_MVIEWS contains the option value.

25 9-25 Copyright © 2005, Oracle. All rights reserved. Specifying Refresh Execution Modes Two refresh execution modes: – ON DEMAND : Manual – ON COMMIT : Refresh done at transaction commit; only possible for fast-refreshable materialized views. In case of failure, subsequent refreshes are manual. Schedule: At regular intervals The REFRESH_MODE column in DBA_MVIEWS contains the refresh execution mode value.

26 9-26 Copyright © 2005, Oracle. All rights reserved. Using Column Aliases in Materialized Views CREATE MATERIALIZED VIEW sales_mv ENABLE QUERY REWRITE AS SELECT s.time_id sales_tid, c.time_id costs_tid FROM sales s, products p, costs c WHERE s.prod_id = p.prod_id AND c.prod_id = p.prod_id AND p.prod_name IN (SELECT prod_name FROM products); SELECT s.time_id, c.time_id FROM sales s, products p, costs c WHERE s.prod_id = p.prod_id AND c.prod_id = p.prod_id AND p.prod_name IN (SELECT prod_name FROM products); Query rewrite

27 9-27 Copyright © 2005, Oracle. All rights reserved. CREATE MATERIALIZED VIEW sales_mv (sales_tid, costs_tid) ENABLE QUERY REWRITE AS SELECT s.time_id, c.time_id FROM sales s, products p, costs c WHERE s.prod_id = p.prod_id AND c.prod_id = p.prod_id AND p.prod_name IN (SELECT prod_name FROM products); SELECT s.time_id, c.time_id FROM sales s, products p, costs c WHERE s.prod_id = p.prod_id AND c.prod_id = p.prod_id AND p.prod_name IN (SELECT prod_name FROM products); Using Materialized View Column Alias Lists Query rewrite

28 9-28 Copyright © 2005, Oracle. All rights reserved. Registering User-Defined Materialized View Tables Register existing materialized view tables with ON PREBUILT TABLE. The registered materialized view can be used for query rewrites. It can be maintained by refresh methods. Requirements: –Table and materialized view have the same name. –Column aliases in detail query must correspond. –Column data types must match; can use the WITH REDUCED PRECISION clause. –Table columns not referenced in the defining query are unmanaged columns. Table remains after the materialized view is dropped.

29 9-29 Copyright © 2005, Oracle. All rights reserved. Partitioning and Materialized Views Partitioning the fact tables: –Improves the opportunity of fast refreshing the materialized view –May enable Partition Change Tracking (PCT) refresh on the materialized view Partitioning the materialized view: –Partition pruning can be used for query rewrite.

30 9-30 Copyright © 2005, Oracle. All rights reserved. Partitioned Materialized View: Example CREATE MATERIALIZED VIEW part_sales_mv PARALLEL PARTITION BY LIST (gid) (PARTITION g1 VALUES (0), PARTITION g2 VALUES (1), PARTITION g3 VALUES (3)) BUILD IMMEDIATE REFRESH COMPLETE ENABLE QUERY REWRITE AS SELECT prod_id,cust_id,time_id, GROUPING_ID(prod_id,cust_id,time_id) AS gid, sum(amount_sold) AS sum_sales FROM sales GROUP BY GROUPING SETS ((prod_id,cust_id,time_id), (prod_id,cust_id),(prod_id));

31 9-31 Copyright © 2005, Oracle. All rights reserved. Using Enterprise Manager to Create Materialized Views cr_mv_gen.gif

32 9-32 Copyright © 2005, Oracle. All rights reserved. Privileges Required to Create Materialized Views Must be granted directly, not through roles To create a materialized view in your schema: – CREATE MATERIALIZED VIEW, and – CREATE [ANY] TABLE, and – SELECT privilege on each detail table not owned To create a materialized view in another schema: – CREATE ANY MATERIALIZED VIEW, and –Materialized view owner must have CREATE [ANY] TABLE, and SELECT privilege on each detail table not owned

33 9-33 Copyright © 2005, Oracle. All rights reserved. Additional Privileges Required to Create Materialized Views To create a materialized view refreshed at commit time: – ON COMMIT REFRESH object privilege on each detail table not owned, or – ON COMMIT REFRESH system privilege To enable query rewrite: –Detail table owner must have QUERY REWRITE system privilege –If you do not own detail tables: GLOBAL QUERY REWRITE system privilege or QUERY REWRITE on each detail table not owned SELECT WITH GRANT OPTION if materialized view is defined on a prebuilt table

34 9-34 Copyright © 2005, Oracle. All rights reserved. Globalization and Materialized Views Materialized views use the settings in effect during materialized view creation. Always specify globalization parameters to ensure that correct results are returned. WHERE varchar_col = TO_DATE('01-FEB-02') TO_DATE('01-FEB-2002', 'DD-MON-YYYY', 'NLS_DATE_LANGUAGE = AMERICAN')

35 9-35 Copyright © 2005, Oracle. All rights reserved. Globalization Parameters Significant for Materialized Views NLS_LANGUAGE NLS_TIMESTAMP_TZ_FORMATNLS_ISO_CURRENCY NLS_TIMESTAMP_FORMATNLS_DUAL_CURRENCY NLS_TIME_TZ_FORMATNLS_DATE_LANGUAGE NLS_TIME_FORMATNLS_DATE_FORMAT NLS_NUMERIC_CHARACTERSNLS_CALENDAR NLS_TERRITORYNLS_CURRENCY NLS_SORTNLS_COMP

36 9-36 Copyright © 2005, Oracle. All rights reserved. Adding Comments to Materialized Views Adding a comment for an existing materialized view: Viewing comments: COMMENT ON MATERIALIZED VIEW cust_sales_mv IS 'sales materialized view'; SELECT mview_name, comments FROM user_mview_comments WHERE mview_name = 'CUST_SALES_MV';

37 9-37 Copyright © 2005, Oracle. All rights reserved. Altering Materialized Views ALTER MATERIALIZED VIEW cust_sales_mv DISABLE QUERY REWRITE; Changing the refresh option and refresh mode: Recompiling the materialized view: Enabling or disabling its use for query rewrite: ALTER MATERIALIZED VIEW cust_sales_mv REFRESH FAST ON COMMIT; ALTER MATERIALIZED VIEW cust_sales_mv COMPILE;

38 9-38 Copyright © 2005, Oracle. All rights reserved. Altering Materialized Views Allocating an extent: Modifying the logging attribute: ALTER MATERIALIZED VIEW cust_sales_mv ALLOCATE EXTENT; ALTER MATERIALIZED VIEW cust_sales_mv NOLOGGING;

39 9-39 Copyright © 2005, Oracle. All rights reserved. Maintaining Partitions of a Materialized View ALTER MATERIALIZED VIEW sales_mv TRUNCATE PARTITION year_1995; ALTER MATERIALIZED VIEW sales_mv DROP PARTITION year_1994; ALTER MATERIALIZED VIEW fact_mv EXCHANGE PARTITION year_2001 WITH TABLE sales_2001;

40 9-40 Copyright © 2005, Oracle. All rights reserved. Dropping Materialized Views You must be the owner or you must have the DROP ANY MATERIALIZED VIEW system privilege. You must have privileges to drop underlying objects. Drop a materialized view using the DROP MATERIALIZED VIEW command: If the materialized view was created on a prebuilt container table, the table is retained. DROP MATERIALIZED VIEW cust_sales_mv;

41 9-41 Copyright © 2005, Oracle. All rights reserved. Viewing Staleness Information Materialized view may diverge from the detail tables. Query the STALENESS column in DBA_MVIEWS : – FRESH – STALE – UNUSABLE – UNKNOWN – UNDEFINED

42 9-42 Copyright © 2005, Oracle. All rights reserved. Materialized View Integrity QUERY_REWRITE_INTEGRITY : Initialization parameter controls materialized view integrity. Parameter values: – ENFORCED – TRUSTED – STALE_TOLERATED

43 9-43 Copyright © 2005, Oracle. All rights reserved. Invalidating Materialized Views Automatic invalidation during dependency changes Automatic revalidation during: –Refresh –Query rewrite STATUS column in DBA_OBJECTS : – INVALID – VALID COMPILE_STATE in DBA_MVIEWS : – VALID – NEEDS_COMPILE Manual revalidation with: ALTER MATERIALIZED VIEW COMPILE

44 9-44 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Use summaries in a data warehouse environment Differentiate types of materialized views Create a materialized view Use the QUERY_REWRITE_INTEGRITY parameter

45 9-45 Copyright © 2005, Oracle. All rights reserved. Practice 9: Overview This practice covers the following topics: Creating materialized views on prebuilt tables Estimating the number of rows of a potential materialized view Creating new materialized views Viewing objects created when materialized views are created

46 9-46 Copyright © 2005, Oracle. All rights reserved.


Download ppt "9 Copyright © 2005, Oracle. All rights reserved. Materialized Views."

Similar presentations


Ads by Google