Keine andere Datenbank zeigt so schön an, wie einzelne Prädikate verwendet werden, wie DB2. Das liegt daran, dass im Ausführungsplan explizit anführt ob ein Prädikat als START
und/oder STOP
-Bedingung für einen IXSCAN genutzt wird – also nicht bloß ob es sich um ein Filter- oder Zugriffsprädikat handelt. Dennoch ist die Anzeige verwirrend, da sie eine alte Definition des Begriffes „SARG“ verwendet.
In the early days, IBM researchers named these kinds of search conditions “sargable predicates” because SARG is a contraction for Search ARGument. In later days, Microsoft and Sybase redefined “sargable” to mean “can be looked up via the index.”
Ich finde beide Definitionen unnütz und vermeide den Begriff in meinen Büchern und Artikeln. In DB2 Ausführungsplänen werden Filterprädikate mit SARG
gekennzeichnet – daher ist es wichtig klarzustellen, dass es sich dabei um die „Original“-Definition handelt. Das lässt sich natürlich auch in der DB2-Dokumentation nachlesen:
Index sargable predicates are not used to bracket a search, but are evaluated from the index if one is chosen, because the columns involved in the predicate are part of the index key. […]
Data sargable predicates […] require the access of individual rows from a base table. If necessary, DMS will retrieve the columns needed to evaluate the predicate, as well as any others to satisfy the columns in the SELECT list that could not be obtained from the index.
— Predicate processing for queries, DB2 LUW 10.5 documentation
Das bedeutet, dass Prädikate die in einem DB2-Ausführungsplan mit SARG
gekennzeichnet sind, Filterprädikate sind – entweder auf Index- oder Tabellen-Ebene.
Das schöne an den Prädikatinformationen eines DB2-Ausführungsplanes ist jedoch, dass Zugriffsprädikate nicht nur gekennzeichnet sind, sondern explizit aufscheint, ob ein Prädikat als START
und/oder STOP
-Bedingung genutzt wird.
Das folgende Beispiel zeigt alle Prädikatstypen wie sie vom last_explained-View ausgegeben werden:
Explain Plan
--------------------------------------------------------------
ID | Operation | Rows | Cost
1 | RETURN | | 23550
2 | GRPBY (COMPLETE) | 1 of 96480 ( .00%) | 23550
3 | IXSCAN SCALE_SLOW | 96480 of 60299800 ( .16%) | 23544
Predicate Information
3 - START (Q1.SECTION = ?)
STOP (Q1.SECTION = ?)
SARG (Q1.ID2 = ?)
Explain plan by Markus Winand - NO WARRANTY
http://use-the-index-luke.com/s/last_explained
Der durchsuchte Indexbereich kann direkt daraus abgelesen werden – er wird nur von den START
und STOP
-Prädikaten bestimmt (in diesem Fall dasselbe). Das Prädikat auf der Spalte ID2
ist mit SARG
gekennzeichnet und daher nur ein Filterprädikat.
SARG
-Prädikate können auch bei anderen Operationen vorkommen (z.B. TBSCAN
), START
und STOP
-Prädikate gibt es jedoch nur bei IXSCAN
. Wenn entweder START
oder STOP
fehlt, bedeutet dass, das es eine einseitig begrenzte Suche handelt (z.B. WHERE x > ?
). Fehlen sowohl START
als auch STOP
-Prädikate bei einem IXSCAN
, wird der ganze Index gelesen.