Informationsintegration Dynamische Programmierung 8 12 2005 Felix Naumann

  • Slides: 31
Download presentation
Informationsintegration Dynamische Programmierung 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06

Informationsintegration Dynamische Programmierung 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06

Überblick l Dynamische Programmierung (DP) zur Anfrageoptimierung l l Grundlagen der DP Das Grundproblem

Überblick l Dynamische Programmierung (DP) zur Anfrageoptimierung l l Grundlagen der DP Das Grundproblem der Anfrageoptimierung DP für verteilte DBMS 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 2

Richard Bellman l l l 1920 – 1984 Ph. D Princeton (3 Monate) Los

Richard Bellman l l l 1920 – 1984 Ph. D Princeton (3 Monate) Los Alamos (1944 -1946) 1953 Rand Corporation: Erfindung der Dynamischen Programmierung Viele andere Beiträge zur Mathematik l Bellman-Ford Algorithmus 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 3

The Stagecoach story l some 150 years ago there was a salesman travelling west

The Stagecoach story l some 150 years ago there was a salesman travelling west by stagecoach. . B E H A C F New York J I D San Francisco G Quelle: Folien Ioana Popescu http: //faculty. insead. edu/popescu/ioana/ 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 4

Versicherungskosten 2 A 4 7 B 4 6 C 4 H 3 2 6

Versicherungskosten 2 A 4 7 B 4 6 C 4 H 3 2 6 F 4 4 3 D 1 E 1 5 G J 3 3 3 4 I 3 Frage: Welches ist der beste (billigste) Weg? Quelle: Folien Ioana Popescu http: //faculty. insead. edu/popescu/ioana/ 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 5

Billigster Weg – Greedy Suche 2 A 4 7 B 4 6 C E

Billigster Weg – Greedy Suche 2 A 4 7 B 4 6 C E 4 H 3 2 6 F 4 4 3 D 1 J 3 4 I 3 1 5 Greedy: A-B-F-I-J = 13 G 3 3 Frage: Gibt es einen besseren Weg? Quelle: Folien Ioana Popescu http: //faculty. insead. edu/popescu/ioana/ 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 6

Billigster Weg – Vollständige Suche 2 A 4 7 B 4 6 C 4

Billigster Weg – Vollständige Suche 2 A 4 7 B 4 6 C 4 H 3 2 6 F 4 4 3 D 5 G 3 J 3 3 1 Besser: A-D-F-I-J = 11 1 E 4 I 3 Anzahl der möglichen Wege: 3 x 2 = 18 Quelle: Folien Ioana Popescu http: //faculty. insead. edu/popescu/ioana/ 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 7

The Stagecoach Solution l Idee der Dynamischen Programmierung l l Voraussetzung: Prinzip der Optimalität

The Stagecoach Solution l Idee der Dynamischen Programmierung l l Voraussetzung: Prinzip der Optimalität l l Hier: “Rückwärtsberechnung” Teilplan eines optimalen Plans ist ebenfalls optimal Idee l l Ausgehend vom Zielknoten stufenweise rückwärts beste Teilpfade berechnen F(X) : = minimale Kosten von X nach J Quelle: Folien Ioana Popescu http: //faculty. insead. edu/popescu/ioana/ 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 8

Billigster Weg - DP F(B)=11 2 B A C F(C)=7 3 D F(D)=8 4

Billigster Weg - DP F(B)=11 2 B A C F(C)=7 3 D F(D)=8 4 6 F(A)=11 4 7 F(E)=4 1 E F(H)=3 4 H 3 2 3 6 F 4 J 3 4 F(F)=7 4 I 3 1 5 G 3 F(G)=6 F(X) : = min Kosten von X nach J 8. 12. 2005 F(J)=0 Felix Naumann, VL Informationsintegration, WS 05/06 F(I)=4 Quelle: Folien Ioana Popescu http: //faculty. insead. edu/popescu/ioana/ 9

Dynamische Programmierung l l Optimaler Algorithmus „Schwierigkeiten“ l l Prinzip der Optimalität muss gelten.

Dynamische Programmierung l l Optimaler Algorithmus „Schwierigkeiten“ l l Prinzip der Optimalität muss gelten. Aufteilung des Problems in Teilprobleme Aufwand kann exponentiell sein Klassische Anwendungen l l Knapsack Problem Traveling Salesman Problem Maschinenbelegung Transportproblem 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 10

Überblick l Dynamische Programmierung (DP) zur Anfrageoptimierung l l Grundlagen der DP Das Grundproblem

Überblick l Dynamische Programmierung (DP) zur Anfrageoptimierung l l Grundlagen der DP Das Grundproblem der Anfrageoptimierung DP für verteilte DBMS 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 11

Architektur zur Anfragebearbeitung Logische Optimierung (unabhängig von System und Konfiguration): Entschachtelung, redundante Prädikate, .

Architektur zur Anfragebearbeitung Logische Optimierung (unabhängig von System und Konfiguration): Entschachtelung, redundante Prädikate, . . . Anfrage Parser Anfrageumschreibung Katalog/ Metadaten Syntax und etwas Semantik Erzeugt Anfragegraph 8. 12. 2005 Anfrageoptimierung Wandelt Plan (Baum) in ausführbaren Plan (Code) um. Anfrageergebnis Code Generierung Anfragebearbeitung (Engine) Optimierung für System und Konfiguration: Indices, Joinreihenfolge, Selektion der Datenquelle Daten Schema, Statistik, Partitionierung, Lage der Daten, . . . Felix Naumann, VL Informationsintegration, WS 05/06 12

Anfrageoptimierung l Suchraum l l Kostenmodell l Alle gültigen Anfragepläne Join-Operator Netzwerkkosten Optimierungsproblem l

Anfrageoptimierung l Suchraum l l Kostenmodell l Alle gültigen Anfragepläne Join-Operator Netzwerkkosten Optimierungsproblem l Finde im Suchraum den kostenoptimalen Anfrageplan. 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 13

Anfrageplanung l Heuristische Einschränkung des Suchraums l Keine Kreuzprodukte Außer explizite Kreuzprodukte in der

Anfrageplanung l Heuristische Einschränkung des Suchraums l Keine Kreuzprodukte Außer explizite Kreuzprodukte in der Anfrage l l l Prädikate so früh wie möglich Nur links-tiefe (left-deep) Bäume Bushy ⋈ ⋈ S 8. 12. 2005 T T R Pipelined Execution ⋈ ⋈ ⋈ U ⋈ Left-Deep ⋈ ⋈ R ⋈ Zig-Zag S U R Felix Naumann, VL Informationsintegration, WS 05/06 S T U 14

Überblick l Dynamische Programmierung (DP) zur Anfrageoptimierung l l Grundlagen der DP Das Grundproblem

Überblick l Dynamische Programmierung (DP) zur Anfrageoptimierung l l Grundlagen der DP Das Grundproblem der Anfrageoptimierung DP für verteilte DBMS 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 15

Dynamische Programmierung: Optimierung im System-R l l A. k. a. “Selinger-style query optimization” l

Dynamische Programmierung: Optimierung im System-R l l A. k. a. “Selinger-style query optimization” l Der klassische Artikel zur Anfrageoptimierung: [SAC+79] l Ursprünglich im IBM System-R l Heutzutage weit verbreitet Grundidee: l Nur “Left-deep” Anfragebäume l l l Bottom-up Generierung von Anfrageplänen l l D. h. nur Joinreihenfolge interessant Innere und äußere Relation unberücksichtigt Dynamische Programmierung (DP) Zusätzlich: interesting orders (interessante Sortierungen) Zusätzlich: interesting sites (interessante Ausführungsorte) Literatur mit Beispiel: [GMUW 00] Quelle: u. a. Folien Prof. Chen Li 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 16

8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 17

8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 17

Bottom-up Anfrageplangenerierung l Grundannahme 1: Nach dem Join über k Relationen ist die Join-Methode

Bottom-up Anfrageplangenerierung l Grundannahme 1: Nach dem Join über k Relationen ist die Join-Methode die k+1 te Relation um hinzuzujoinen unabhängig von den vorigen Join-Methoden. l l Grundannahme 2: Jeder Teilplan eines optimalen Plans ist ebenfalls optimal. l l Joinmethoden: Nested Loops, Hashjoin, Sort-Merge Join usw. Entspricht dem Prinzip der Optimalität: Wenn sich zwei Pläne nur in einem Teilplan unterscheiden, so ist der Plan mit dem besseren Teilplan auch der bessere Gesamtplan Bottom-up Anfrageplangenerierung: l Berechne die optimalen Pläne für den Join über (jede Kombination von) k Relationen l l l 8. 12. 2005 Suboptimale Pläne werden verworfen Erweitere diese Pläne zu optimalen Plänen für k+1 Relationen. usw. bis k = n Felix Naumann, VL Informationsintegration, WS 05/06 18

Dynamische Programmierung {} {R} {S} {T} {U} {R S} {R T} {R U} {S

Dynamische Programmierung {} {R} {S} {T} {U} {R S} {R T} {R U} {S T} {S U} {T U} {R S T} {R S U} {S T U} {R T U} {R S T U} 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 19

DP – Grundidee für Anfrageoptimierung l Für jede Kombination merke (in einer Hilfstabelle): l

DP – Grundidee für Anfrageoptimierung l Für jede Kombination merke (in einer Hilfstabelle): l l Geschätzte Größe des Ergebnisses (Kardinalität) Geschätzte minimale Kosten l l l Hier zur Vereinfachung: Größe des Zwischenergebnisses Joinreihenfolge, diese Kosten verursacht (= optimaler Teilplan) Induktion über Anzahl der Relationen im Plan l N=1: Für jede Relation l l N=2: Für jedes Relationenpaar R, S l l l Kardinalität = |R| x |S| x sf Kosten = 0 Joinreihenfolge: kleinere Relation links Clou: R und S jeweils mit besten access-path N=3: Für jedes Tripel R, S, T l l Kardinalität = Kardinalität der Relation Kosten = 0 (zur Vereinfachung) Joinreihenfolge: n/a Clou: Nur bestes Relationenpaar aus dem Tripel wird um dritte Relation ergänzt . . . 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 20

DP – Beispiel S • Anfrage über Relationen R, S, T, U. • Vier

DP – Beispiel S • Anfrage über Relationen R, S, T, U. • Vier Join-Bedingungen T Anfragegraph R U u. U. auch Index. Scan(. . . ) 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 21

DP – Beispiel X X Kreuzprodukte nicht berücksichtigen! 8. 12. 2005 Besser als z.

DP – Beispiel X X Kreuzprodukte nicht berücksichtigen! 8. 12. 2005 Besser als z. B. S ⋈ (T ⋈ R) oder (R ⋈ S) ⋈ T Felix Naumann, VL Informationsintegration, WS 05/06 S T R U 22

DP – Beispiel S T Anfragegraph R Bisher unberücksichtigt: Wahl des Join-Algorithmus 8. 12.

DP – Beispiel S T Anfragegraph R Bisher unberücksichtigt: Wahl des Join-Algorithmus 8. 12. 2005 U Optimaler (leftdeep) Plan Felix Naumann, VL Informationsintegration, WS 05/06 23

8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 24

8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 24

DP - interesting orders (Interessante Sortierung) l l Wd. H. : Prinzip der Optimalität:

DP - interesting orders (Interessante Sortierung) l l Wd. H. : Prinzip der Optimalität: Wenn sich zwei Pläne nur in einem Teilplan unterscheiden, so ist der Plan mit dem besseren Teilplan auch der bessere Gesamtplan. Gegenbeispiel: l l l R(A, B) ⋈ S(A, C) ⋈ T(A, D) Bester (lokaler) Plan für R ⋈ S: Hash-Join Best (globaler) Gesamtplan: l l l 1. Sort-merge Join über R und S 2. Sort-merge Join mit T Warum könnte dies so sein? l l Das Zwischenergebnis von R ⋈sort-merge. S ist nach Join-Attribut A sortiert. Dies ist eine interesting order, die später ausgenutzt werden kann: l l 8. 12. 2005 Spätere sort-merge Joins Gruppierung (GROUP BY) Sortierung (ORDER BY) Eindeutige Tupel (DISTINCT) Felix Naumann, VL Informationsintegration, WS 05/06 25

DP - interesting orders (Interessante Sortierung) Bei Auswahl des besten Teilplans: l Kostenvergleich genügt

DP - interesting orders (Interessante Sortierung) Bei Auswahl des besten Teilplans: l Kostenvergleich genügt nicht. l Es gibt keine vollständige Ordnung der Teilpläne nach Kosten. Auch Sortierungen müssen berücksichtigt werden. l l Lösung: Für jede Kombination von Relationen, speichere mehrere Sortiervarianten: l 1. 2. l l Nach jeder Variante der beteiligten Teilpläne Die “leere” Sortierung DP Tabellen werden „breiter“. Merke außerdem Join- und Sortieroperationen, diese Sortierung erzeugen. 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 26

DP – Algorithmus Alle Zugriffspläne für jede Relation Schlechtere Zugriffspläne verwerfen Achtung: Nicht left-deep!

DP – Algorithmus Alle Zugriffspläne für jede Relation Schlechtere Zugriffspläne verwerfen Achtung: Nicht left-deep! Quelle: [Ko 00] 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 27

Überblick l Dynamische Programmierung (DP) zur Anfrageoptimierung l l Grundlagen der DP Das Grundproblem

Überblick l Dynamische Programmierung (DP) zur Anfrageoptimierung l l Grundlagen der DP Das Grundproblem der Anfrageoptimierung DP für verteilte DBMS 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 28

DP in verteilten Systemen l Entscheidungen des Optimierers l Zugriffpfade auf Relationen l l

DP in verteilten Systemen l Entscheidungen des Optimierers l Zugriffpfade auf Relationen l l l Joinreihenfolge Art der Join-Berechnung l l Table-scan, Index-scan Nested loops, sort-merge usw. Neu: Ort der Join-Berechnung l l 8. 12. 2005 Am Speicherort der inneren Relation / des Zwischenergebnisses Am Speicherort der äußere Relation / des Zwischenergebnisses Am Ort der Weiterverarbeitung Anderswo, z. B. Ort der schnellsten CPU Felix Naumann, VL Informationsintegration, WS 05/06 29

DP in verteilten Systemen Berücksichtigung (und Repräsentation) mehrerer Speicherorte ⇒ mehr Zugrifspläne Berücksichtigung (und

DP in verteilten Systemen Berücksichtigung (und Repräsentation) mehrerer Speicherorte ⇒ mehr Zugrifspläne Berücksichtigung (und Repräsentation) des Ausführungsortes des Joins ⇒ interesting order und interesting site ⇒ mehr Zugrifspläne Am Ende gegebenenfalls noch ship Operator anfügen. 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 30

Literatur l Das grundlegende Paper l l Englisches Lehrbuch l l [SAC+79] Patricia G.

Literatur l Das grundlegende Paper l l Englisches Lehrbuch l l [SAC+79] Patricia G. Selinger, Morton M. Astrahan, Donald D. Chamberlin, Raymond A. Lorie, Thomas G. Price: Access Path Selection in a Relational Database Management System. SIGMOD Conference 1979: 23 -34 [GMUW 00] Hector Garcia-Molina, Jeffrey D. Ullman, Jennifer Widom: Database System Implementation Prentice-Hall 2000 Weiteres l l [Ko 00] The State of the Art in Distributed Query Processing, Donald Kossmann, ACM Computing Surveys 32(4), pages 422 -469. (Link auf WWW) [OK 00] Kiyoshi Ono, Guy M. Lohman: Measuring the Complexity of Join Enumeration in Query Optimization. VLDB 1990: 314 -325 [HFLP 89] Laura M. Haas, Johann Christoph Freytag, Guy M. Lohman, Hamid Pirahesh: Extensible Query Processing in Starburst. SIGMOD Conference 1989: 377 -388 [Graefe 93] Goetz Graefe: Query Evaluation Techniques for Large Databases. ACM Comput. Surv. 25(2): 73 -170 (1993) 8. 12. 2005 Felix Naumann, VL Informationsintegration, WS 05/06 31