optimale performance durch constraints im data...

34
BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENF HAMBURG KOPENHAGEN LAUSANNE MÜNCHEN STUTTGART WIEN ZÜRICH Optimale Performance durch Constraints im Data Warehouse DOAG Konferenz, 17. November 2016 Dani Schnider, Trivadis AG @dani_schnider

Upload: others

Post on 06-Feb-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

  • BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENF HAMBURG KOPENHAGEN LAUSANNE MÜNCHEN STUTTGART WIEN ZÜRICH

    Optimale Performance durch Constraints im Data Warehouse DOAG Konferenz, 17. November 2016 Dani Schnider, Trivadis AG

    @dani_schnider

  • Unser Unternehmen.

    Optimale Performance durch Constraints im DWH 2 17.11.2016

    Trivadis ist führend bei der IT-Beratung, der Systemintegration, dem Solution Engineering und der Erbringung von IT-Services mit Fokussierung auf - und -Technologien in der Schweiz, Deutschland, Österreich und Dänemark. Trivadis erbringt ihre Leistungen aus den strategischen Geschäftsfeldern: Trivadis Services übernimmt den korrespondierenden Betrieb Ihrer IT Systeme.

    B E T R I E B

  • KOPENHAGEN

    MÜNCHEN

    LAUSANNE BERN

    ZÜRICH BRUGG

    GENF

    HAMBURG

    DÜSSELDORF

    FRANKFURT

    STUTTGART

    FREIBURG

    BASEL

    WIEN

    Mit über 600 IT- und Fachexperten bei Ihnen vor Ort.

    Optimale Performance durch Constraints im DWH 3 17.11.2016

    14 Trivadis Niederlassungen mit über 600 Mitarbeitenden.

    Über 200 Service Level Agreements.

    Mehr als 4'000 Trainingsteilnehmer.

    Forschungs- und Entwicklungsbudget: CHF 5.0 Mio. / EUR 4.0 Mio.

    Finanziell unabhängig und nachhaltig profitabel.

    Erfahrung aus mehr als 1'900 Projekten pro Jahr bei über 800 Kunden.

  • Dani Schnider

    4

    Principal Consultant, Teacher und DWH Lead Architect bei Trivadis in Zürich

      Co-Autor der Bücher «Data Warehousing mit Oracle» und «Data Warehouse Blueprints»

    17.11.2016 Optimale Performance durch Constraints im DWH

    @dani_schnider danischnider.wordpress.com

  • Constraints im Data Warehouse

    Optimale Performance durch Constraints im DWH 5 17.11.2016

    „In unserem Data Warehouse verzichten wir auf Constraints.

    Sie führen zu Fehlern und Abhängigkeiten in den ETL-Prozessen und sind mühsam

    zu definieren“

    „Constraints sind nicht nur wichtig zur Überprüfung der

    Datenintegrität und als Dokumentation des

    Datenmodells, sondern vor allem auch für gute

    Performance im Data Warehouse“

    „Was sind Constraints?“

  • Zweck von Constraints

    Optimale Performance durch Constraints im DWH 6 17.11.2016

    „Constraints sind nicht nur wichtig zur Überprüfung der

    Datenintegrität und als Dokumentation des

    Datenmodells, sondern vor allem auch für gute

    Performance im Data Warehouse“

    Datenintegrität

    Dokumentation

    Performance

  • Optimale Performance durch Constraints im DWH 7 17.11.2016

    Foreign Key Constraints

  • Datenmodell ohne Foreign Key Constraints

    Optimale Performance durch Constraints im DWH 8 17.11.2016

  • Datenmodell mit Foreign Key Constraints

    Optimale Performance durch Constraints im DWH 9 17.11.2016

  • Problematik von Foreign Keys in ETL-Prozessen

    Optimale Performance durch Constraints im DWH 10 17.11.2016

      Abhängigkeiten der Ladereihenfolge –  Tabellen sollen gleichzeitig geladen werden –  Unabhängigkeit z.B. durch Hash Keys

      Verwendung von Key Lookups –  keine zusätzliche Prüfung notwendig –  ev. Hash Keys zur Vermeidung von Lookups

      Foreign Key Constraints verhindern gleichzeitiges Laden

  • Reliable Constraints

    Optimale Performance durch Constraints im DWH 11 17.11.2016

      Foreign Key Constraints werden ausgeschaltet (DISABLE)

      Foreign Key Constraints werden nicht geprüft (NOVALIDATE)

      Für Dokumentation weiterhin vorhanden (sind im Data Dictionary sichtbar)

      Primary und Foreign Key Constraints werden als vertrauenswürdig definiert (RELY)

    Datenintegrität

    Dokumentation

    Performance

  • Reliable Constraints

    Optimale Performance durch Constraints im DWH 12 17.11.2016

    ALTER TABLE customer_vers ADD CONSTRAINT customer_vers_head_fk FOREIGN KEY (dwh_head_id) REFERENCES customer_head (dwh_id) RELY DISABLE NOVALIDATE;

    Definition von Foreign Key:

    ALTER TABLE customer_head ADD CONSTRAINT customer_head_pk PRIMARY KEY (dwh_id) RELY;

    Definition von Primary Key:

  • Optimale Performance durch Constraints im DWH 13 17.11.2016

    Join Elimination

  • Join Elimination (Star Schema)

    Optimale Performance durch Constraints im DWH 14 17.11.2016

    SELECT p.prod_cat_desc, SUM(s.amount_sold) FROM sales s JOIN products p ON (s.prod_id = p.prod_id) JOIN customers c ON (s.cust_id = c.cust_id) JOIN times t ON (s.time_id = t.time_id) WHERE t.calendar_month_desc = '2014-03' GROUP BY p.prod_cat_desc

    TIMES

    CUSTOMERS

    PRODUCTS

    SALES

    SELECT p.prod_cat_desc, SUM(s.amount_sold) FROM sales s JOIN products p ON (s.prod_id = p.prod_id) JOIN customers c ON (s.cust_id = c.cust_id) JOIN times t ON (s.time_id = t.time_id) WHERE t.calendar_month_desc = '2014-03' GROUP BY p.prod_cat_desc

  • Abfrage ohne Foreign Keys

    Optimale Performance durch Constraints im DWH 15 17.11.2016

    ---------------------------------------------------------------- | Id | Operation | Name | ---------------------------------------------------------------- | 0 | SELECT STATEMENT | | | 1 | HASH GROUP BY | | | 2 | NESTED LOOPS | | |* 3 | HASH JOIN | | | 4 | TABLE ACCESS FULL | PRODUCTS | | 5 | NESTED LOOPS | | | 6 | NESTED LOOPS | | |* 7 | TABLE ACCESS FULL | TIMES | | 8 | PARTITION RANGE ITERATOR | | | 9 | BITMAP CONVERSION TO ROWIDS | | |* 10 | BITMAP INDEX SINGLE VALUE | SALES_TIME_BIX | | 11 | TABLE ACCESS BY LOCAL INDEX ROWID| SALES | |* 12 | INDEX UNIQUE SCAN | CUSTOMERS_PK | ----------------------------------------------------------------

  • Join Elimination durch Foreign Keys

    Optimale Performance durch Constraints im DWH 16 17.11.2016

    --------------------------------------------------------------- | Id | Operation | Name | --------------------------------------------------------------- | 0 | SELECT STATEMENT | | | 1 | HASH GROUP BY | | |* 2 | HASH JOIN | | | 3 | TABLE ACCESS FULL | PRODUCTS | | 4 | NESTED LOOPS | | | 5 | NESTED LOOPS | | |* 6 | TABLE ACCESS FULL | TIMES | | 7 | PARTITION RANGE ITERATOR | | | 8 | BITMAP CONVERSION TO ROWIDS | | |* 9 | BITMAP INDEX SINGLE VALUE | SALES_TIME_BIX | | 10 | TABLE ACCESS BY LOCAL INDEX ROWID| SALES | ---------------------------------------------------------------

  • Join Elimination (View Layer)

    Optimale Performance durch Constraints im DWH 17 17.11.2016

    CREATE OR REPLACE VIEW v1 AS SELECT /*+ no_merge */ t1.id , c1.code c11_cd , c1.description c11_desc , c2.code c12_cd , c2.description c12_desc , c3.code c13_cd , c3.description c13_desc , c4.code c14_cd , c4.description c14_desc , c5.code c15_cd , c5.description c15_desc FROM t1 JOIN c c1 ON (c1.id = t1.c11) JOIN c c2 ON (c2.id = t1.c12) JOIN c c3 ON (c3.id = t1.c13) JOIN c c4 ON (c4.id = t1.c14) JOIN c c5 ON (c5.id = t1.c15)

  • Join Elimination (View Layer)

    Optimale Performance durch Constraints im DWH 18 17.11.2016

    -------------------------------------- | Id | Operation | Name | -------------------------------------- | 0 | SELECT STATEMENT | | |* 1 | HASH JOIN | | | 2 | VIEW | V1 | |* 3 | HASH JOIN | | |* 4 | HASH JOIN | | | 5 | TABLE ACCESS FULL| T1 | | 6 | TABLE ACCESS FULL| C | | 7 | TABLE ACCESS FULL | C | | 8 | VIEW | V2 | |* 9 | HASH JOIN | | | 10 | TABLE ACCESS FULL | C | | 11 | TABLE ACCESS FULL | T2 | -------------------------------------- 1 - access("V1"."ID"="V2"."T1_ID") 3 - access("C4"."ID"="T1"."C14") 4 - access("C1"."ID"="T1"."C11") 9 - access("C2"."ID"="T2"."C22")

    SELECT v1.c11_desc , v1.c14_desc , v2.c22_desc FROM v1 JOIN v2 ON v1.id = v2.t1_id

    Joins nur für benötigte Codes

  • Join Elimination (Data Vault)

    Optimale Performance durch Constraints im DWH 20 17.11.2016

    SELECT s1.product_name , s2.list_price , s3.customer_city FROM h1 JOIN s1 ON s1.h1_sid = h1.h1_sid JOIN s2 ON s2.h1_sid = h1.h1_sid JOIN l1 ON l1.h1_sid = h1.h1_sid JOIN h2 ON h2.h2_sid = l1.h2_sid JOIN l2 ON l2.h2_sid = h2.h2_sid JOIN h3 ON h3.h3_sid = l2.h3_sid JOIN s3 ON s3.h3_sid = l2.h3_sid WHERE s1.current_flag = 'Y' AND s2.current_flag = 'Y' AND s3.current_flag = 'Y'

    SELECT s1.product_name , s2.list_price , s3.customer_city FROM h1 JOIN s1 ON s1.h1_sid = h1.h1_sid JOIN s2 ON s2.h1_sid = h1.h1_sid JOIN l1 ON l1.h1_sid = h1.h1_sid JOIN h2 ON h2.h2_sid = l1.h2_sid JOIN l2 ON l2.h2_sid = h2.h2_sid JOIN h3 ON h3.h3_sid = l2.h3_sid JOIN s3 ON s3.h3_sid = l2.h3_sid WHERE s1.current_flag = 'Y' AND s2.current_flag = 'Y' AND s3.current_flag = 'Y'

    Demo

  • Reliable Constraints und Join Elimination

    Optimale Performance durch Constraints im DWH 21 17.11.2016

    If a foreign key constraint is in NOVALIDATE state, join elimination is not done when QUERY_REWRITE_INTEGRITY=enforced. This means that queries with joins over a foreign key constraint that is in RELY NOVALIDATE state can potentially take longer to parse and execute as the optimizer does not trust the RELY.

    Siehe Dokumentation Oracle Database Reference 12c Release 1

    query_rewrite_integrity = trusted

      Bis Oracle 11g: Join Elimination funktioniert auch mit Reliable Constraints   Ab Oracle 12c: zusätzlich muss Parameter query_rewrite_integrity gesetzt werden

  • Optimale Performance durch Constraints im DWH 22 17.11.2016

    Query Rewrite

  • Query Rewrite

    Optimale Performance durch Constraints im DWH 23 17.11.2016

    ?

    SALES

    TIMES

    PRODUCTS

    Query Rewrite

    BI Application

    MV_MONTH_SALES_PROD

    Optimizer

  • Materialialized View für Query Rewrite

    Optimale Performance durch Constraints im DWH 24 17.11.2016

    CREATE MATERIALIZED VIEW mv_product_month_sales ENABLE QUERY REWRITE AS SELECT t.calendar_month_desc , p.prod_id , SUM(s.amount_sold) , COUNT(s.amount_sold) , COUNT(*) FROM sales s , times t , products p WHERE t.time_id = s.time_id AND p.prod_id = s.prod_id GROUP BY t.calendar_month_desc, p.prod_id

    Tipp: In Materialized View keine ANSI Join Syntax verwenden

    SALES

    TIMES

    PRODUCTS

  • Advanced Query Rewrite: Join Back

    Optimale Performance durch Constraints im DWH 25 17.11.2016

    SELECT t.calendar_month_desc , p.prod_name , p.prod_subcategory , p.prod_category , SUM(s.amount_sold) AS amount_sold FROM sales s JOIN times t ON t.time_id = s.time_id JOIN products p ON p.prod_id = s.prod_id GROUP BY t.calendar_month_desc , p.prod_name , p.prod_subcategory , p.prod_category

      Zusätzliche Dimensionsattribute (nicht in Materialized View enthalten)   Join zwischen Materialized View und Dimensionstabelle notwendig

  • Advanced Query Rewrite: Materialized View Delta Joins

    Optimale Performance durch Constraints im DWH 26 17.11.2016

    SELECT p.prod_name , SUM(s.amount_sold) AS amount_sold FROM sales s JOIN products p ON p.prod_id = s.prod_id GROUP BY p.prod_name

      Nicht alle Joins der Materialized View kommen in Query vor   Nur möglich für «lossless» Joins (keine Datensätze gehen durch Join verloren)

    SALES

    TIMES

    PRODUCTS

  • Advanced Query Rewrite und Constraints

    Optimale Performance durch Constraints im DWH 27 17.11.2016

      Join Back: –  Reliable PK/FK Constraints –  query_rewrite_integrity = trusted

      Materialized View Delta Joins: –  Reliable PK/FK Constraints –  NOT NULL Constraints auf Join-Attributen

    –  query_rewrite_integrity = trusted Demo

  • Advanced Query Rewrite und Constraints

    Optimale Performance durch Constraints im DWH 28 17.11.2016 Te

    xt M

    atch

    AN

    SI J

    oin

    Join

    Bac

    k

    Del

    ta J

    oin

    Keine Foreign Key Constraints ✓ ✓ ✗ ✗ Add Foreign Key Constraints ✓ ✗ ✗ ✗ query_rewrite_integrity = trusted ✓ ✓ ✓ ✓ Reliable Constraints, trusted ✓ ✓ ✓ ✓ Keine NOT NULL Constraints ✓ ✗ ✓ ✗

  • Optimale Performance durch Constraints im DWH 29 17.11.2016

    NOT NULL Constraints

  • FCT_SALES

    DATE_ID CUST_ID PROD_ID 14.11.2016 2352 100 14.11.2016 6113 200 15.11.2016 15256 400 16.11.2016 2355 (NULL) 16.11.2016 6113 100 16.11.2016 6113 300 16.11.2016 4252 200

    NOT NULL Constraints im Data Warehouse

    Optimale Performance durch Constraints im DWH 30 17.11.2016

    DIM_DATE

    DIM_CUSTOMER

    DIM_PRODUCT

    PROD_ID PROD_NAME 100 Weisses Einhorn 200 Glatthopfen 300 Schwarze Kobra 400 Lazariter 500 Blauer Pirat

    FCT_SALES

    DATE_ID CUST_ID PROD_ID 14.11.2016 2352 100 14.11.2016 6113 200 15.11.2016 15256 400 16.11.2016 2355 -1 16.11.2016 6113 100 16.11.2016 6113 300 16.11.2016 4252 200

    DIM_PRODUCT

    PROD_ID PROD_NAME -1 Unknown Product 100 Weisses Einhorn 200 Glatthopfen 300 Schwarze Kobra 400 Lazariter 500 Blauer Pirat

  • NOT NULL Constraints zur Performanceoptimierung

    Optimale Performance durch Constraints im DWH 31 17.11.2016

      Keine Outer Joins bei Abfragen notwendig –  Verwendung von Singletons statt NULL-Werten

      Mehr Möglichkeiten bei Advanced Query Rewrite –  Materialized View Delta Joins

      Vermeidung von unnötigen Zugriffen auf Bitmap Index –  Unnötiger Indexzugriff kann eliminiert werden

    Demo

  • Optimale Performance durch Constraints im DWH 32 17.11.2016

    Empfehlungen

  • Constraints im Data Warehouse

    Optimale Performance durch Constraints im DWH 33 17.11.2016

      „Jede“ Tabelle hat eine Primary Key –  Ausnahmen: Staging Area, Faktentabellen –  Ev. zusätzlicher Unique Constraint auf fachlichem Schlüssel

      Beziehungen sollten immer als Foreign Keys implementiert werden –  Foreign Keys können mit RELY DISABLE NOVALIDATE angelegt werden

      Attribute wenn immer möglich als NOT NULL definieren

    –  Verwendung von Singletons (Default-Werte) query_rewrite_integrity = trusted (für Join Elimination und Query Rewrite)

    query_rewrite_enabled = true (default, für Query Rewrite)

  • Weitere Informationen

    Optimale Performance durch Constraints im DWH 34 17.11.2016

    danischnider.wordpress.com

  • Trivadis @ DOAG 2016

      Stand: 3ter Stock – direkt an der Rolltreppe Know how, T-Shirts, Gewinnspiel und Trivadis Power to go   Wir freuen uns wenn Sie vorbei schauen   Weil Sie mit Trivadis immer gewinnen !

    17.11.2016 Optimale Performance durch Constraints im DWH 35