Materialized Views
Total Page:16
File Type:pdf, Size:1020Kb
MATERIALIZED VIEWS - WISSENSWERTES UND NEUERUNGEN IN 12C AUTHOR: JONAS GASSENMEYER www.syntegris.de © SYNTEGRIS INFORMATION SOLUTIONS GMBH Mein Haus, mein Auto,… [email protected] @gassenmj Stand 42 © SYNTEGRIS INFORMATION SOLUTIONS GMBH AGENDA . Vorstellung (2 Minuten) . Use Case Pharma Konzern (3 Minuten) •Wissenswertes (vor 12c) .Was ist eine MV, (SQL Hinterlegen und Index drauf) (2 Minuten) .Query Rewrite (1 Minuten) .Atomic_refresh (5 Minuten) .Tablespace (1 Minute) .Fast refresh (incrementell) (5 Minuten) .„es kommt drauf an“ (1 Minuten) • Out of place refresh . Prebuilt table (3 Minuten) . Demo (3 Minuten) • On Statement MVs . Erklärung (3 Minuten) . Demo (3 Minuten) • Real time MVs . Erklärung (3 Minuten) . Demo (3 Minuten) Refresh Statistiken .Erklärung (3 Minuten) .Demo (3 Minuten) .Bug? Fazit (3 Minuten) © SYNTEGRIS INFORMATION SOLUTIONS GMBH Use Case - Aggregation Table Table Table complex Tables MView fast select Report aggregation runs when user runs at night requests the report Materialized View pre-computes result sets © SYNTEGRIS INFORMATION SOLUTIONS GMBH Use Case - Replication › Use Case - Replication Database Local DB MView Link Table Remote DB Data Materialized View copies remote data to local DB © SYNTEGRIS INFORMATION SOLUTIONS GMBH Real World Example? © SYNTEGRIS INFORMATION SOLUTIONS GMBH Chemical Substances › Ordering Vials/Tubes/Plates Differ on each Site → Aggregation From ca. 50 Mio aggregated to ca. 6.5 Mio › Metadata about Substances Same on each Site → Replication Ca. 18.5 Mio © SYNTEGRIS INFORMATION SOLUTIONS GMBH Example – Pharma R&D © SYNTEGRIS INFORMATION SOLUTIONS GMBH Example – Registration Registration System PL/SQL API Frankfurt (Master) MV MV MV MV Boston Tucson Paris Toulouse © SYNTEGRIS INFORMATION SOLUTIONS GMBH Example – Aggregation of Orderable Substances Web Interface Synonyms Global Ordering MV MV MV MV ~50 Mio aggregated to ~6.5 Mio Boston Tucson Paris Toulouse © SYNTEGRIS INFORMATION SOLUTIONS GMBH Code? © SYNTEGRIS INFORMATION SOLUTIONS GMBH Simple Syntax CREATE MATERIALIZED VIEW abc_mv AS SELECT * from abc@remotedb; DROP MATERIALIZED VIEW abc_mv; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Method CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG BUILD DEFERRED USING INDEX REFRESH COMPLETE ON DEMAND DISABLE QUERY REWRITE AS (SELECT /*+ driving_site (r1)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table r1, schema.table2@db_link alias_remote_table r2 WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Tipp! › Driving site Hint places costs on local/remote site › Consider to use Oracles JOIN Syntax! CREATE MATERIALIZED VIEW schema.mv_name AS (SELECT /*+ driving_site (alias_remote_table)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table1, schema.table2@db_link alias_table2 WHERE alias_table2.join = alias_remote_table1.join col3 = ‘xy‘; http://docs.oracle.com/database/122/SQLRF/Comments.htm#GUID-DDABFD2E-FE80-4A2D-99D2-87E61EFA2182 © SYNTEGRIS INFORMATION SOLUTIONS GMBH Tipp! ? › Nolog Tablespace › Backup/Redo might not be needed › In case of Recovery: complete refresh against master table CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG AS (SELECT col1 ,col2 ,… FROM schema.table1@db_link WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Parameters © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Methods --------------- complete fast atomic © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Methods CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG BUILD DEFERRED USING INDEX REFRESH COMPLETE ON DEMAND DISABLE QUERY REWRITE AS (SELECT /*+ driving_site (r1)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table r1, schema.table2@db_link alias_remote_table r2 WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Methods - Complete Refresh Table Complete refresh MView © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Methods - Fast Refresh Incremental refresh Table MView MV Incremental refresh LOG Incremental refresh © SYNTEGRIS INFORMATION SOLUTIONS GMBH Fast Refresh - Details › AFTER ROW TRIGGER logs changes in master table › additional Table MLOG$_master_name › master instance must provide extra space for those logs › Special: fast refresh direct path operations on masters unregister MVs which are not used anymore! © SYNTEGRIS INFORMATION SOLUTIONS GMBH Materialized View Logs › PRIMARY KEY LOGIC CREATE MATERIALIZED VIEW LOG ON master_table › ROWID PHYSICAL WITH PRIMARY KEY,ROWID INCLUDING NEW VALUES; CREATE MATERIALIZED VIEW mv_name REFRESH WITH ROWID AS SELECT * FROM table@db_link; › IMPACT: Reorganization and truncating of the master table is difficult FAST REFRESH will TURN into a COMPLETE REFRESH © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Methods CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG ON PREBUILD TABLE USING INDEX REFRESH COMPLETE ON DEMAND DISABLE QUERY REWRITE AS (SELECT /*+ driving_site (r1)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table r1, schema.table2@db_link alias_remote_table r2 WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Scheduled Refreshes BEGIN DBMS_SCHEDULER.CREATE_JOB ( job_name => ‘JOB_REFRESH_MAT_VIEWS_MASTER', job_type => 'STORED_PROCEDURE', job_action => 'REFRESH_MAT_VIEWS_MASTER’, repeat_interval => 'FREQ=MINUTELY;INTERVAL=10’, comments => 'fast refresh for MV'); END; / dbms_mview.refresh('MV', 'C’, atomic_refresh => FALSE ); Is not possible in MVs Definition! © SYNTEGRIS INFORMATION SOLUTIONS GMBH ATOMIC REFRESH = TRUE › Special Options DELETE › MV is readable all the time › Index maintenance during delete and INSERT insert SELECT › Default since 10g › REDO and UNDO COMMIT MView © SYNTEGRIS INFORMATION SOLUTIONS GMBH ATOMIC REFRESH = FALSE TRUNCATE › truncate faster than delete (high water mark) INSERT › minimal Index maintenance /*+append*/ SELECT › append hint used during insert (direct path load) COMMIT › MView empty during refresh (MV readable?) › No UNDO (truncate) and less REDO MView (append) © SYNTEGRIS INFORMATION SOLUTIONS GMBH DEMO! © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Time --------------- on demand on commit on statement (12c) © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Time - On DEMAND manual execution or regular job dbms_mview.refresh Table MView Refresh © SYNTEGRIS INFORMATION SOLUTIONS GMBH Refresh Time - On COMMIT commit Table MView Refresh © SYNTEGRIS INFORMATION SOLUTIONS GMBH Statement-Level Refresh (12c) CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG BUILD DEFERRED USING INDEX REFRESH FAST ON STATEMENT DISABLE QUERY REWRITE AS (SELECT /*+ driving_site (r1)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table r1, schema.table2@db_link alias_remote_table r2 WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Statement-Level Refresh Any DML before commit Table join MView Table Refresh © SYNTEGRIS INFORMATION SOLUTIONS GMBH DEMO! © SYNTEGRIS INFORMATION SOLUTIONS GMBH Build and Change ------------------ build deferred prebuild table out of place (12c) © SYNTEGRIS INFORMATION SOLUTIONS GMBH Build Deferred CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG BUILD DEFERRED USING INDEX REFRESH COMPLETE ON DEMAND DISABLE QUERY REWRITE AS (SELECT /*+ driving_site (r1)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table r1, schema.table2@db_link alias_remote_table r2 WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Build Deferred create MV build deferred MV empty Table MV complete refresh with Data DEFAULT = BUILD IMMEDIATE © SYNTEGRIS INFORMATION SOLUTIONS GMBH On prebuild Table CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG ON PREBUILD TABLE USING INDEX REFRESH COMPLETE ON DEMAND DISABLE QUERY REWRITE AS (SELECT /*+ driving_site (r1)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table r1, schema.table2@db_link alias_remote_table r2 WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH On prebuild Table create table as select prebuild table create MV Table Refresh MView © SYNTEGRIS INFORMATION SOLUTIONS GMBH Out-of-Place Refresh dbms_mview.refresh( 'MV’ , method => ‘?’ , atomic_refresh => false , out_of_place => true ); © SYNTEGRIS INFORMATION SOLUTIONS GMBH Out-of-Place Refresh RV$xxx Refresh table Table New Segment MView SQL © SYNTEGRIS INFORMATION SOLUTIONS GMBH DEMO! © SYNTEGRIS INFORMATION SOLUTIONS GMBH Performance ------------------ Query Rewrite Real Time (12c) © SYNTEGRIS INFORMATION SOLUTIONS GMBH Query Rewrite CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG ON PREBUILD TABLE USING INDEX REFRESH COMPLETE ON DEMAND ENABLE QUERY REWRITE AS (SELECT /*+ driving_site (r1)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table r1, schema.table2@db_link alias_remote_table r2 WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Query Rewrite Select CBO Tables rewrites the query to use MV Select the MV © SYNTEGRIS INFORMATION SOLUTIONS GMBH Statement-Level Refresh CREATE MATERIALIZED VIEW schema.mv_name TABLESPACE TBS_NOLOG BUILD DEFERRED USING INDEX REFRESH FAST ON DEMAND ENABLE QUERY REWRITE ENABLE ON QUERY COMPUTATION AS (SELECT /*+ driving_site (r1)*/ col1 ,col2 ,… FROM schema.table1@db_link alias_remote_table r1, schema.table2@db_link alias_remote_table r2 WHERE …; © SYNTEGRIS INFORMATION SOLUTIONS GMBH Real-Time Materialized Views Select CBO Table rewrites the Select query to use MV MV the MV and Incremental LOG reads Delta from refresh MV Logs © SYNTEGRIS INFORMATION SOLUTIONS GMBH Further Reading / Code Examples › Chris Saxon: https://blogs.oracle.com/sql/12-things- developers-will-love-about-oracle-database-12c-release- 2#real-time-mv › Uwe