Materialized Views. 2 Materialized Views – Agenda What is a Materialized View? – Advantages and Disadvantages How Materialized Views Work – Parameter.

Slides:



Advertisements
Similar presentations
Refreshing Materialized Views
Advertisements

10 Copyright © 2005, Oracle. All rights reserved. Dimensions.
12 Copyright © 2005, Oracle. All rights reserved. Query Rewrite.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Materialized Views.
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Database Systems: Design, Implementation, and Management Tenth Edition
Semantec Ltd. Oracle Performance Tuning Boyan Pavlov Indexes Indexes.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Data Replication with Materialized Views ISYS 650.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Concepts of Database Management Sixth Edition
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Adapted from Afyouni, Database Security and Auditing Database Application Auditing – Ch. 8.
SQL Tuning Ohio Oracle User’s Group October 2002 © Copyright, Kris T. Mason, 2002.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
1.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
Oracle Data Block Oracle Concepts Manual. Oracle Rows Oracle Concepts Manual.
Chapter 6 Additional Database Objects
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Database Technical Session By: Prof. Adarsh Patel.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
Materialized Views Acknowledgement to Author: Willie Albino.
Professor Michael J. Losacco CIS 1110 – Using Computers Database Management Chapter 9.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) In some.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Commercial RDBMSs Access and Oracle. Access DBMS Architchecture  Can be used as a standalone system on a single PC: -JET Engine -Microsoft Data Engine.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Indexes and Views Unit 7.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
13 Copyright © Oracle Corporation, All rights reserved. Maintaining Data Integrity.
Chapter 4 Logical & Physical Database Design
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session id: Darrell Hilliard Senior Delivery Manager Oracle University Oracle Corporation.
Oracle Business Intelligence Foundation - Commonly Used Features in Repository.
Chapter 15 Materialized Views.
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
3 Copyright © 2006, Oracle. All rights reserved. Designing and Developing for Performance.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
9 Copyright © 2006, Oracle. All rights reserved. Summary Management.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
11 Copyright © 2009, Oracle. All rights reserved. Enhancing ETL Performance.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Chapter 8 Dependencies, Privileges and Compilation Oracle11g:
Materialized Views Willie Albino May 15, 2003.
Database.
Chapter 4 Indexes.
CH 4 Indexes.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CH 4 Indexes.
Contents Preface I Introduction Lesson Objectives I-2
SQL Views Presented by: Dr. Samir Tartir
Presentation transcript:

Materialized Views

2 Materialized Views – Agenda What is a Materialized View? – Advantages and Disadvantages How Materialized Views Work – Parameter Settings, Privileges, Query Rewrite Creating Materialized Views – Syntax, Refresh Modes/Options, Build Methods – Examples Dimensions – What are they? – Examples

3 What is a Materialized View? A database object that stores the results of a query – Marries the query rewrite features found in Oracle Discoverer with the data refresh capabilities of snapshots Features/Capabilities – Can be partitioned and indexed – Can be queried directly – Can have DML applied against it – Several refresh options are available – Best in read-intensive environments

4 Advantages and Disadvantages Advantages – Useful for summarizing, pre-computing, replicating and distributing data – Faster access for expensive and complex joins – Transparent to end-users MVs can be added/dropped without invalidating coded SQL Disadvantages – Performance costs of maintaining the views – Storage costs of maintaining the views

5 Database Parameter Settings init.ora parameter – COMPATIBLE=8.1.0 (or above) System or session settings – query_rewrite_enabled={true|false} – query_rewrite_integrity= {enforced|trusted|stale_tolerated} Can be set for a session using – alter session set query_rewrite_enabled=true; – alter session set query_rewrite_integrity=enforced; Privileges which must be granted to users directly – QUERY_REWRITE - for MV using objects in own schema – GLOBAL_QUERY_REWRITE - for objects in other schemas

6 Query Rewrite Details query_rewrite_integrity Settings: – enforced – rewrites based on Oracle enforced constraints Primary key, foreign keys – trusted – rewrites based on Oracle enforced constraints and known, but not enforced, data relationships Primary key, foreign keys Data dictionary information Dimensions – stale_tolerated – queries rewritten even if Oracle knows the mv’s data is out-of-sync with the detail data Data dictionary information

7 Query Rewrite Details (cont’d) Query Rewrite Methods – Full Exact Text Match Friendlier/more flexible version of text matching – Partial Text Match Compares text starting at FROM clause SELECT clause must be satisfied for rewrite to occur – Data Sufficiency All required data must be present in the MV or retrievable through a join-back operation – Join Compatibility All joined columns are present in the MV

8 Query Rewrite Details (cont’d) – Grouping Compatibility Allows for matches in groupings at higher levels than those defined MV query Required if both query and MV contain a GROUP BY clause – Aggregate Compatibility Allows for interesting rewrites of aggregations – If SUM(x) and COUNT(x) are in MV, the MV may be used if the query specifies AVG(x)

9 Syntax For Materialized Views CREATE MATERIALIZED VIEW TABLESPACE { } REFRESH [ENABLE|DISABLE] QUERY REWRITE AS SELECT ; The determines when MV is built – BUILD IMMEDIATE: view is built at creation time – BUILD DEFFERED: view is built at a later time – ON PREBUILT TABLE: use an existing table as view source Must set QUERY_REWRITE_INTEGRITY to TRUSTED

10 Refresh Options – COMPLETE – totally refreshes the view Can be done at any time; can be time consuming – FAST – incrementally applies data changes A materialized view log is required on each detail table Data changes are recorded in MV logs or direct loader logs Many other requirements must be met for fast refreshes – FORCE – does a FAST refresh in favor of a COMPLETE The default refresh option Materialized View Refresh Options

11 Materialized View Refresh Modes Refresh Modes – ON COMMIT – refreshes occur whenever a commit is performed on one of the view’s underlying detail table(s) Available only with single table aggregate or join based views Keeps view data transactionally accurate Need to check alert log for view creation errors – ON DEMAND – refreshes are initiated manually using one of the procedures in the DBMS_MVIEW package Can be used with all types of materialized views Manual Refresh Procedures – DBMS_MVIEW.REFRESH(, ) – DBMS_MVIEW.REFRESH_ALL_MVIEWS() – START WITH [NEXT] - refreshes start at a specified date/time and continue at regular intervals

12 Materialized View Example CREATE MATERIALIZED VIEW items_summary_mv ON PREBUILT TABLE REFRESH FORCE AS SELECT a.PRD_ID, a.SITE_ID, a.TYPE_CODE, a.CATEG_ID, sum(a.GMS) GMS, sum(a.NET_REV) NET_REV, sum(a.BOLD_FEE) BOLD_FEE, sum(a.BIN_PRICE) BIN_PRICE, sum(a.GLRY_FEE) GLRY_FEE, sum(a.QTY_SOLD) QTY_SOLD, count(a.ITEM_ID) UNITS FROM items a GROUP BY a.PRD_ID, a.SITE_ID, a.TYPE_CODE, a.CATEG_ID; ANALYZE TABLE item_summary_mv COMPUTE STATISTICS;

13 Materialized View Example (cont’d) -- Query to test impact of materialized view select categ_id, site_id, sum(net_rev), sum(bold_fee), count(item_id) from items where prd_id in ('2000M05','2000M06','2001M07','2001M08') and site_id in (0,1) and categ_id in (2,4,6,8,1,22) group by categ_id, site_id save mv_example.sql

14 Materialized View Example (cont’d) SQL> ALTER SESSION SET QUERY_REWRITE_INTEGRITY=TRUSTED; SQL> ALTER SESSION SET QUERY_REWRITE_ENABLED=FALSE; CATEG_ID SITE_ID SUM(NET_REV) SUM(BOLD_FEE) COUNT(ITEM_ID) Elapsed: 01:32:17.93 Execution Plan SELECT STATEMENT Optimizer=HINT: FIRST_ROWS (Cost= Card=6 Bytes=120) 1 0 SORT (GROUP BY) (Cost= Card=6 Bytes=120) 2 1 PARTITION RANGE (INLIST 3 2 TABLE ACCESS (FULL) OF ‘ITEMS' (Cost= Card= Bytes= )

15 Materialized View Example (cont’d) SQL> ALTER SESSION SET QUERY_REWRITE_ENABLED=TRUE; CATEG_ID SITE_ID SUM(NET_REV) SUM(BOLD_FEE) COUNT(ITEM_ID) Elapsed: 00:01:40.47 Execution Plan SELECT STATEMENT Optimizer=HINT: FIRST_ROWS (Cost=3749 Card=12 Bytes=276) 1 0 SORT (GROUP BY) (Cost=3749 Card=12 Bytes=276) 2 1 PARTITION RANGE (INLIST) 3 2 TABLE ACCESS (FULL) OF ‘ITEMS_SUMMARY_MV' (Cost=3723 Card=7331 Bytes=168613)

16 Example of FAST REFRESH MV CREATE MATERIALIZED VIEW LOG ON ITEMS TABLESPACE MV_LOGS STORAGE(INITIAL 10M NEXT 10M) WITH ROWID; CREATE MATERIALIZED VIEW LOG ON CUSTOMERS TABLESPACE MV_LOGS STORAGE(INITIAL 1M NEXT 1M) WITH ROWID; CREATE MATERIALIZED VIEW cust_activity BUILD IMMEDIATE REFRESH FAST ON COMMIT AS SELECT u.ROWID cust_rowid, l.ROWID item_rowid, u.cust_id, u.custname, u. , l.categ_id, l.site_id, sum(gms), sum(net_rev_fee) FROM customers u, items l WHERE u.cust_id = l.seller_id GROUP BY u.cust_id, u.custname, u. , l.categ_id, l.site_id;

17 Getting Information About an MV Getting information about the key columns of a materialized view: SELECT POSITION_IN_SELECT POSITION, CONTAINER_COLUMN COLUMN, DETAILOBJ_OWNER OWNER, DETAILOBJ_NAME SOURCE, DETAILOBJ_ALIAS ALIAS, DETAILOBJ_TYPE TYPE, DETAILOBJ_COLUMN SRC_COLUMN FROM USER_MVIEW_KEYS WHERE MVIEW_NAME=‘ITEMS_SUMMARY_MV’; POS COLUMN OWNER SOURCE ALIAS TYPE SRC_COLUMN PRD_ID TAZ ITEMS A TABLE PRD_ID 2 SITE_ID TAZ ITEMS A TABLE SITE_ID 3 TYPE_CODE TAZ ITEMS A TABLE TYPE_CODE 4 CATEG_ID TAZ ITEMS A TABLE CATEG_ID

18 Getting Information About an MV Getting information about the aggregate columns of a materialized view: SELECT POSITION_IN_SELECT POSITION, CONTAINER_COLUMN COLUMN, AGG_FUNCTION FROM USER_MVIEW_AGGREGATES WHERE MVIEW_NAME=‘ITEMS_SUMMARY_MV’; POSITION COLUMN AGG_FUNCTION GMS SUM 7 NET_REV SUM : : : 11 QTY_SOLD SUM 12 UNITS COUNT

19 Dimensions A way of describing complex data relationships – Used to perform query rewrites, but not required – Defines hierarchical relationships between pairs of columns Hierarchies can have multiple levels Each child in the hierarchy has one and only one parent Each level key can identify one or more attribute Child join keys must be NOT NULL Dimensions should be validated using the DBMS_OLAP.VALIDATE_DIMENSION package – Bad row ROWIDs stored in table: mview$_exceptions

20 Syntax For Creating A Dimension CREATE DIMENSION LEVEL [ IS IS …] HIERARCHY ( CHILD OF CHILD OF …] ATTRIBUTE DETERMINES DETERMINES,…); To validate a dimension: exec dbms_olap.validate_dimension(,,FALSE,FALSE);

21 Example of Creating A Dimension CREATE DIMENSION time_dim LEVEL CAL_DATE IS calendar.CAL_DATE LEVEL PRD_ID IS calendar.PRD_ID LEVEL QTR_ID IS calendar.QTR_ID LEVEL YEAR_ID IS calendar.YEAR_ID LEVEL WEEK_IN_YEAR_ID IS calendar.WEEK_IN_YEAR_ID HIERARCHY calendar_rollup (CAL_DATE CHILD OF PRD_ID CHILD OF QTR_ID CHILD OF YEAR_ID) HIERARCHY week_rollup (CAL_DATE CHILD OF WEEK_IN_YEAR_ID CHILD OF YEAR_ID) ATTRIBUTE PRD_ID DETERMINES PRD_DESC ATTRIBUTE QTR_ID DETERMINES QTR_DESC;

22 Example of Validating A Dimension SQL> exec dbms_olap.validate_dimension(‘time_dim’, USER, FALSE, FALSE); PL/SQL procedure successfully completed. SQL> select * from mview$_exceptions; no rows selected. -- Main cause of errors is a child level having multiple parents -- If above query returns rows, the bad rows can be found as follows: select * from calendar where rowid in (select bad_rowid from mview$_exceptions);

23 Example of Using Dimensions -- Step 1 of 4 -- Create materialized view (join-aggregate type) CREATE MATERIALIZED VIEW items_mv BUILD IMMEDIATE REFRESH ON DEMAND ENABLE QUERY REWRITE AS SELECT l.slr_id, c.cal_date, sum(l.gms) gms FROM items l, calendar c WHERE l.end_date=c.cal_date GROUP BY l.slr_id, c.cal_date;

24 Example of Using Dimensions (cont’d) -- Step 2 of 4: (not really required, for demonstration only) -- Execute query based on “quarter”, not “date”, without a time dimension -- Note that the detail tables are accessed SQL> select c.qtr_id, sum(l.gms) gms 2 from items l, calendar c 3 where l.end_date=c.cal_date 4 group by l.slr_id, c.qtr_id; Execution Plan SELECT STATEMENT Optimizer=CHOOSE (Cost=16174 Card=36258 Bytes= ) SORT (GROUP BY) (Cost=16174 Card=36258 Bytes= ) HASH JOIN (Cost=81 Card= Bytes= ) TABLE ACCESS (FULL) OF ’CALENDAR' (Cost=2 Card=8017 Bytes=128272) TABLE ACCESS (FULL) OF ’ITEMS' (Cost=76 Card=69993 Bytes= )

25 Example of Using Dimensions (cont’d) -- Step 3 of 4: Create time dimension (see slide #21 for Dimension Created -- Step 4 of 4: Rerun query based on “quarter” with time dimension SQL> select c.qtr_id, sum(l.gms) gms 2 from items l, calendar c 3 where l.end_date=c.cal_date 4 group by l.slr_id, c.qtr_id; Execution Plan SELECT STATEMENT Optimizer=CHOOSE (Cost=3703 Card= Bytes= ) SORT (GROUP BY) (Cost=3703 Card= Bytes= ) HASH JOIN (Cost=31 Card= Bytes= ) VIEW (Cost=25 Card=8017 Bytes=128272) SORT (UNIQUE) (Cost=25 Card=8017 Bytes=128272) TABLE ACCESS (FULL) OF ‘CALENDAR’ (Cost=2 Card=8017 Bytes=128272) TABLE ACCESS (FULL) OF ‘ITEMS_MV’ (Cost=3 Card=10962 Bytes=383670)

26 Summary Materialized Views – reduce system cpu/io resource requirements by pre- calculating and storing results of intensive queries – allow for the automatic rewriting of intensive queries – are transparent to the application – have storage/maintenance requirements – can understand complex data relationships – can be refreshed on demand or on a schedule Dimensions – allow you to “tell” Oracle about complex data relationships which can be used to rewrite queries

27 References Using Oracle9 i Materialized Views (Technet Oracle By Example) – dwh/html/mv/mv.htm dwh/html/mv/mv.htm Oracle Expert-One-On-One – Thomas Kyte The Secrets of Materialized Views – OLAP DB-Design with Dimensions – The Secrets of Dimensions –

28 Requirements for FAST REFRESH

29 Rqmts For FAST REFRESH (cont’d)