Index Rebuild DOAG Konferenz 17.01.2011, Nürnberg DOAG Konferenz 17.11.2011, Nürnberg Martin Hoermann info@ordix.de Martin Hoermann www.ordix.de info@ordix.de www.ordix.de
Eine kurze Geschichte der Zeit Eine kurze Geschichte der (Oracle) Zeit, Martin Hoermann 1 / 51
Big Ben DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 2
Index Rebuild DOAG Konferenz 17.01.2011, Nürnberg DOAG Konferenz 17.11.2011, Nürnberg Martin Hoermann info@ordix.de Martin Hoermann www.ordix.de info@ordix.de www.ordix.de
www.dba-oracle.com Quelle: www.dba-oracle.com/t_index_rebuilding_indexes.htm DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 4
asktom.oracle.com Quelle: asktom.oracle.com/pls/asktom/f?p=100:11:745954239666467::::p11_question_id:2913600659112 DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 5
Richard Foote - Index Internals Quelle: http://richardfoote.files.wordpress.com/2007/12/index-internals-rebuilding-the-truth-ii.pdf DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 6
DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 7
Es gibt zwei Kategorien von Menschen. Die einen, die den Strick um den Hals haben und die anderen, die ihn abschneiden. Zwei glorreiche Halunken DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 8
Agenda Index-Strukturen Basisbegriffe Zugriffsstrategien Abgrenzung Testszenarien Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 9
Branch- und Leaf-Blocks Root Block... Branch Block 10 18 v$index_stats.height = dba_indexes.blevel + 1 Leaf Block 2 4 10 12 14 16 18 20 22 50% 100% 90% Füllgrad Key + ROWID = NULL DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 10
leaf node splits 10 18 2 4 6 8 10 12 14 16 INSERT 5 INSERT 17 6 10 17 2 4 5 6 8 10 12 14 16 17 leaf node splits leaf node 90-10 split DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 11
branch node split 6 10 18 Row-ID des root Blocks bleibt identisch 2 4 5 6 8 10 12 14 16 18 20 22 24 INSERT 17 branch node splits 16 6 10 16 18 2 4 5 6 8 10 12 14 16 17 18 20 22 24 DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 12
Agenda Index-Strukturen Basisbegriffe Zugriffsstrategien Abgrenzung Testszenarien Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 13
Index Unique Scan 6 10 18 2 4 5 6 8 10 12 14 16 18 20 22 24 SELECT /*+ INDEX(...) */ * FROM my_table WHERE id = 20; = blevel + 1 (leaf) [ + table access ] DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 14
Index Range Scan Ausgabe sortiert 6 10 18 2 4 5 6 8 10 12 14 16 18 20 22 24 SELECT /*+ INDEX_RS_ASC(...) */ * FROM my_table WHERE id BETWEEN 8 AND 20; = blevel + n ( Anzahl leaf) [ + table access je row ] DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 15
Index Full Scan Ausgabe sortiert 6 10 18 2 4 5 6 8 10 12 14 16 18 20 22 24 SELECT /*+INDEX_FS(...) */ * FROM my_table WHERE id > 0; = blevel + leaf blocks [ + table access je row ] DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 16
Index Fast Full Scan db_file_multiblock_read_count Ausgabe nicht sortiert Bitmap Freelist Bitmap Freelist Segment Header 6 10 18 10 12 14 16 25 2 4 5 6 8 18 20 22 24 SELECT /*+ INDEX_FFS(...) */ * FROM my_table WHERE id > 0; = # Blöcke im Segment [ + table access je row ] DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 17
Sonstige Zugriffstrategien Index Skip Scan Range Scan Desc Index Join Bitmap Index DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 18
Agenda Index-Strukturen Basisbegriffe Zugriffsstrategien Abgrenzung Testszenarien Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 19
Abgrenzung Index Typen + Zugriffe Function Based Index Index Joins Bitmap Indizes Reverse Indizes Kosten Cost Based Optimizer Locks & Latches DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 20
Agenda Index-Strukturen Testszenarien Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 21
Agenda Index-Strukturen Testszenarien Nur Inserts Primary Key Index Index mit 1% Selektivität Index mit normalverteilter Selektivität Insert + Delete B-Level Optimierung Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 22
Test nur Insert 1. Durchlauf 2. Durchlauf 10 Millionen Sätze Rebuild Delta Ermittlung für 100.000 Insert Block Splits 100.000 Insert Anzahl Leaf Blocks 900.000 Insert Redo Volumen CPU-Zeit 900.000 Insert Consistent Gets 4.000.000 Insert 4.000.000 Insert DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 23
Varianten Primary Key Index über Sequenz doag_data.nextval 1 Prozent Selektivität, Gleichverteilt mod( doag_data_seq.currval, 100 ) Normalverteilung trunc( dbms_random.normal() * 10000 ) DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 24
Agenda Index-Strukturen Testszenarien Nur Inserts Primary Key Index Index mit 1% Selektivität Index mit normalverteilter Selektivität Insert + Delete B-Level Optimierung Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 25
PK-Index über Sequenzen 5 9 1 2 3 4 5 6 7 8 9 10 11 12 INSERT 13 5 9 13 1 2 3 4 5 6 7 8 9 10 11 12 13 Index 90:10 Split DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 26
Rebuild DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 27
Primary Key Index nach Rebuild 5 9 13 1 2 3 4 5 6 7 8 9 10 11 12 13 Rebuild 10 4 7 10 13 1 2 3 4 5 6 7 8 9 10 11 12 13 PCT_FREE = 90% DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 28
Rebuild Effects Mehr Leaf Blocks PCT-Used wird bei streng monoton steigenden Werten nie wieder genutzt PCT-Used = 0 % entspricht Aufbau wie zuvor Augenscheinlich ist es unsinnig Primary Key Indizes zu reorganisieren.* * für das Szenario Insert ohne Delete DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 29
Primary Key Index bei RAC Was passiert bei RAC bei Sequenzen mit Cache? DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 30
Agenda Index-Strukturen Testszenarien Nur Inserts Primary Key Index Index mit 1% Selektivität Index mit normalverteilter Selektivität Insert + Delete B-Level Optimierung Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 31
Index 1 % Selektivität Bei vielen Daten viele Wiederholungen Daten werden im Index sortiert nach Row-ID eingefügt 9 9 9 9 9 9 9 9 9 9 9 Ausschnitt für einen Wert! DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 32
Rebuild DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 33
Index 1 % Selektivität: Rebuild 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 Ausschnitt für einen Wert! DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 34
Index 1 % Selektivität: Fazit I weniger Leaf Blöcke (bis zu 50%) Für vorhandene Index-Blöcke nachhaltig Besseres Caching Index Read Only oft deutlich schneller! DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 35
Index 1% Selektivität: Fazit II Ohne Rebuild Clustering Factor: 3.179.374 Leaf Blocks: 24.700 Kosten: ~ 3.204.000 Mit Rebuild Clustering Factor: 3.179.374 Leaf Blocks: 14.900 Kosten: ~ 3.194.000 Kostendifferenz für Full Scan: 0,31% weniger Blockzugriffe DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 36
Index 1% Selektivität: Clustering Factor mod( seq.currval, 100 ) 0 1 2 3 4 99 0 1 2 3 4.. 1 1 1 1 1 mod( trunc( seq.currval / 100 ), 100 ) 0 0 0 0 0 1 1 1 1. 1 2 2 2 2 1 1 1 1 1 DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 37
Index 1% Selektivität: Fazit III Ohne Rebuild Clustering Factor: 132.331 Leaf Blocks: 26.000 Kosten: ~ 158.000 Mit Rebuild Clustering Factor: 132.331 Leaf Blocks: 15.400 Kosten: ~ 147.000 Kostendifferenz für Full Scan: 7% weniger Blockzugriffe DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 38
Agenda Index-Strukturen Testszenarien Nur Inserts Primary Key Index Index mit 1% Selektivität Index mit normalverteilter Selektivität Insert + Delete B-Level Optimierung Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 39
Index Normalverteilung Aufbau Normalverteilung trunc( dbms_random.normal() * 10000 ) DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 40
Rebuild DOAG Konferenz 17.01.2011, Nürnberg Martin Hoermann info@ordix.de www.ordix.de DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 41
Entwicklung Leaf Blocks Rebuild 45.000 40.000 35.000 30.000 25.000 20.000 Mit Rebuild Ohne Rebuild 15.000 10.000 5.000 0 Insert 10.000.000 Rebuild Insert 100.000 Insert 900.000 Insert 4.000.000 DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 42
Index Normalverteilung: Fazit 27 % weniger Leaf Blöcke durch Rebuild 12 % weniger Leaf Blöcke nach 10 % Inserts Kosten pro Insert steigen durch Rebuild Aber ich arbeite nach einem festen Prinzip, wenn mich einer bezahlt, dann führe ich den Auftrag auch aus. (Zwei glorreiche Halunken) DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 43
Agenda Index-Strukturen Testszenarien Nur Inserts Insert + Delete B-Level Optimierung Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 44
Test Insert + Delete 1. Durchlauf 2. Durchlauf 1 Million Sätze Rebuild Delta Ermittlung für 100.000 Delete Block Splits 100.000 Delete Anzahl Leaf Blocks 100.000 Insert Redo Volumen CPU-Zeit 100.000 Delete Consistent Gets 10 x 10 x DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 45
Leaf Nodes 3500 3000 2500 2000 1500 Mit Rebuild Ohne Rebuild 1000 500 0 DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 46
Kumulierte Consistent Gets 600000 500000 400000 300000 200000 Mit Rebuild Ohne Rebuild 100000 0 DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 47
Agenda Index-Strukturen Testszenarien Nur Inserts Insert + Delete B-Level Optimierung Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 48
B-Level Optimierung Überlegung: Kosten Index Unique Scan + Table Access B-Level 3: 4 Consistent Gets B-Level 2: 3 Consistent Gets Performance Optimierung von 25% möglich Einfluss auf das Caching positiv? DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 49
B-Level Optimierung Vergleich nur Insert Nur eine Spalte (Number) Tabelle hat einen Wert 2, sonst nur Einsen PCT_USED ~ 50% PCT_USED ~ 90% durch regelmäßigen Rebuild DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 50
Rebuild DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 51
B-Level Optimierung Height 5 4 3 2 1 Rows PCT_USED = 50 % PCT_USED = 90 % Bereich in Anzahl Rows bei der ein Rebuild die Höhe reduziert DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 52
B-Level Optimierung Height 150.000 bis 250.000 50.000.000 bis 70.000.000 18.500.000.000 Bis? 5 4 3 2 1 Rows PCT_USED = 50 % PCT_USED = 90 % Bereich in Anzahl Rows bei der ein Rebuild die Höhe reduziert DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 53
B-Level Optimierung Caching Effekt durch Branch-Blöcke vernachlässigbar: 10.000 Leaf Blöcke: 67-390 Branch Blöcke 100.000 Leaf Blöcke: 390-700 Branch Blöcke Optimierung nur in sehr speziellen Fällen denkbar DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 54
Agenda Index-Strukturen Testszenarien Fazit DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 55
Referenz I http://richardfoote.wordpress.com/ DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 56
Referenzen II DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 57
Referenzen III DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 58
Formel für Rebuild Jetzt kommt die ultimative Formel für den Rebuild von Indizes! DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 59
Formel für Rebuild DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 60
Formel für Rebuild IF ( cost < benefit ) THEN Rebuild Index END; DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 61
Kosten & Nutzen Distinct Keys Index Scan Clustering Factor PCT_FREE Delete Index Entries Concurrency Multicolumn Caching Compress Bitmap CPU Reverse Storage Validate Structure B-Level Deleted Leafs Organisation Block Splits Statistiken Leaf Blocks Primary Key Redo DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 62
Es gibt zwei Arten von Menschen. Die einen haben einen geladenen Revolver und die anderen Buddeln! Zwei glorreiche Halunken DOAG Konferenz, Index Rebuild - the good, the bad and the ugly, 17.11.2011 63
Zentrale Paderborn Westernmauer 12-16 33098 Paderborn Tel.: 05251 1063-0 Seminarzentrum Wiesbaden Kreuzberger Ring 13 65205 Wiesbaden Tel.: 0611 77840-00 Zentrales Fax: 0180 1 67349 0 0180 1 ORDIX 0 Weitere Geschäftsstellen in Köln, Münster und Neu-Ulm E-Mail: info@ordix.de Internet: http://www.ordix.de