be inclusive welcome non key columns in b tree indexes
play

Be Inclusive: Welcome Non-key Columns in B-Tree Indexes - PowerPoint PPT Presentation

Be Inclusive: Welcome Non-key Columns in B-Tree Indexes @MarkusWinand @SQLPerfTips @ModernSQL Safe Harbour Statement Instead of a Safe Harbour Statement Instead of a Safe Harbour Statement Take this Safe the Planet


  1. 
 B-tree Index: INCLUDE (since 11) } CREATE INDEX … K K K K K K K K … ON … E E E E E E E E Not so deep Y Y Y Y Y Y Y Y ( k ) INCLUDE (data) K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E VM Table 
 (Heap)

  2. 
 B-tree Index: INCLUDE (since 11) CREATE INDEX … K K K K K K K K … ON … E E E E E E E E Y Y Y Y Y Y Y Y ( k ) INCLUDE (data) K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E VM Table 
 (Heap)

  3. 
 B-tree Index: INCLUDE (since 11) SELECT data CREATE INDEX … K K K K K K K K … FROM … ON … E E E E E E E E Y Y Y Y Y Y Y Y WHERE k = $1 ( k ) INCLUDE (data) K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E VM Table 
 (Heap)

  4. 
 B-tree Index: INCLUDE (since 11) SELECT data CREATE INDEX … K K K K K K K K … FROM … ON … E E E E E E E E Y Y Y Y Y Y Y Y WHERE k = $1 ( k ) INCLUDE (data) K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E VM Table 
 (Heap)

  5. 
 B-tree Index: INCLUDE (since 11) SELECT data CREATE INDEX … K K K K K K K K … FROM … ON … E E E E E E E E Y Y Y Y Y Y Y Y WHERE k = $1 ( k ) INCLUDE (data) K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E VM Table 
 (Heap)

  6. 
 B-tree Index: INCLUDE (since 11) SELECT data CREATE INDEX … K K K K K K K K … FROM … ON … E E E E E E E E Y Y Y Y Y Y Y Y WHERE k = $1 ( k ) INCLUDE (data) K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E VM Table 
 (Heap)

  7. 
 B-tree Index: INCLUDE (since 11) SELECT data CREATE INDEX … K K K K K K K K … FROM … ON … E E E E E E E E Y Y Y Y Y Y Y Y WHERE k = $1 ( k ) INCLUDE (data) K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E VM Table 
 (Heap)

  8. INCLUDE — Pro and Con vs. Key-Columns

  9. INCLUDE — Pro and Con vs. Key-Columns Advantages:

  10. INCLUDE — Pro and Con vs. Key-Columns Advantages: ‣ Shallower: < ~40%

  11. INCLUDE — Pro and Con vs. Key-Columns Advantages: ‣ Shallower: < ~40% ‣ Slightly smaller: < ~3%

  12. INCLUDE — Pro and Con vs. Key-Columns Advantages: Disadvantages: ‣ Shallower: < ~40% ‣ … ‣ Slightly smaller: < ~3%

  13. INCLUDE — Disadvantages: WHERE SELECT * FROM … WHERE k = $1 AND data = $2

  14. INCLUDE — Disadvantages: WHERE SELECT * FROM … WHERE k = $1 AND data = $2 CREATE INDEX … ON … ( k, data ) QUERY PLAN -------------------------------------------------- Index Scan using … on … (actual rows=1) Index Cond: ((key = $1) AND (data = $2) Bu ff ers: shared hit=5

  15. INCLUDE — Disadvantages: WHERE SELECT * FROM … WHERE k = $1 AND data = $2 CREATE INDEX … CREATE INDEX … ON … ON … ( k ) ( k, data ) INCLUDE ( data ) QUERY PLAN QUERY PLAN ----------------------------------------------------------- -------------------------------------------------- Bitmap Heap Scan on … (actual rows=1) Index Scan using … on … (actual rows=1) Recheck Cond: (key = 123) Index Cond: ((key = $1) AND (data = $2) Filter: (data = $2) Bu ff ers: shared hit=5 Rows Removed by Filter: 9999 Heap Blocks: exact=10000 Bu ff ers: shared hit=2186 read=7867 -> Bitmap Index Scan on … (actual rows=10000 ) Index Cond: (key = 123) Buffers: shared read=53

  16. INCLUDE — Disadvantages: WHERE SELECT * FROM … WHERE k = $1 AND data = $2 CREATE INDEX … CREATE INDEX … ON … ON … ( k ) ( k, data ) INCLUDE ( data ) QUERY PLAN QUERY PLAN ----------------------------------------------------------- -------------------------------------------------- Bitmap Heap Scan on … (actual rows=1) Index Scan using … on … (actual rows=1) Recheck Cond: (key = 123) Index Cond: ((key = $1) AND (data = $2) Filter: (data = $2) Bu ff ers: shared hit=5 Rows Removed by Filter: 9999 Doesn’t Heap Blocks: exact=10000 Filter on INCLUDE 
 Bu ff ers: shared hit=2186 read=7867 column -> Bitmap Index Scan on … (actual rows=10000 ) Index Cond: (key = 123) Buffers: shared read=53

  17. 
 B-tree Index: INCLUDE (since 11) CREATE INDEX … SELECT * K K K K K K K K … ON … E E E E E E E E FROM … Y Y Y Y Y Y Y Y ( k ) WHERE k = $1 INCLUDE (data) AND data = $2 K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E Table 
 (Heap)

  18. 
 B-tree Index: INCLUDE (since 11) CREATE INDEX … SELECT * K K K K K K K K … ON … E E E E E E E E FROM … Y Y Y Y Y Y Y Y ( k ) WHERE k = $1 INCLUDE (data) AND data = $2 K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E Table 
 (Heap)

  19. 
 B-tree Index: INCLUDE (since 11) CREATE INDEX … SELECT * K K K K K K K K … ON … E E E E E E E E FROM … Y Y Y Y Y Y Y Y ( k ) WHERE k = $1 INCLUDE (data) AND data = $2 K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E Table 
 (Heap)

  20. 
 B-tree Index: INCLUDE (since 11) CREATE INDEX … SELECT * K K K K K K K K … ON … E E E E E E E E FROM … Y Y Y Y Y Y Y Y ( k ) WHERE k = $1 INCLUDE (data) AND data = $2 K K K K K K K K K K K K K K K E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E Table 
 (Heap)

  21. 
 B-tree Index: INCLUDE (since 11) CREATE INDEX … SELECT * K K K K K K K K … ON … E E E E E E E E FROM … Y Y Y Y Y Y Y Y ( k ) WHERE k = $1 INCLUDE (data) AND data = $2 K K K K K K K K K K K K K K K Ignored E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y (unknown if visible) I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C L L L L L L L L L L L L L L U U U U U U U U U U U U U U D D D D D D D D D D D D D D E E E E E E E E E E E E E E Table 
 (Heap)

  22. 
 B-tree Index: INCLUDE (since 11) CREATE INDEX … SELECT * K K K K K K K K … ON … E E E E E E E E FROM … Y Y Y Y Y Y Y Y ( k ) WHERE k = $1 INCLUDE (data) AND data = $2 K K K K K K K K K K K K K K K Ignored E E E E E E E E E E E E E E E Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y (unknown if visible) I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 I 
 … … … N N N N N N N N N N N N N N N C C C C C C C C C C C C C C C Table access 
 L L L L L L L L L L L L L L U U U U U U U U U U U U U U not reduced D D D D D D D D D D D D D D E E E E E E E E E E E E E E Table 
 (Heap)

  23. B-tree Index: INCLUDE (since 11) 1. Use tree 2. Use doubly linked list 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 (in VM and/or in table) 5. Apply remaining filters

  24. B-tree Index: INCLUDE (since 11) O p e r a t i o n s 1. Use tree 2. Use doubly linked list 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 (in VM and/or in table) 5. Apply remaining filters

  25. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 (in b-tree key: <=, <, =, >, >=) 1. Use tree 2. Use doubly linked list 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 (in VM and/or in table) 5. Apply remaining filters

  26. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 Other 
 (in b-tree key: <=, <, =, >, >=) 1. Use tree 2. Use doubly linked list 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 (in VM and/or in table) 5. Apply remaining filters

  27. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 Other 
 (in b-tree key: <=, <, =, >, >=) 1. Use tree 2. Use doubly linked list C o l u m n s 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 (in VM and/or in table) 5. Apply remaining filters

  28. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 Other 
 (in b-tree key: <=, <, =, >, >=) 1. Use tree 2. Use doubly linked list C o l u m n s Key 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 (in VM and/or in table) 5. Apply remaining filters

  29. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 Other 
 (in b-tree key: <=, <, =, >, >=) ✓ 1. Use tree 2. Use doubly linked list C o l u m n s Key 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 (in VM and/or in table) 5. Apply remaining filters

  30. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 Other 
 (in b-tree key: <=, <, =, >, >=) ✓ 1. Use tree ✗ 2. Use doubly linked list C o l u m n s Key 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 (in VM and/or in table) 5. Apply remaining filters

  31. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 Other 
 (in b-tree key: <=, <, =, >, >=) ✓ 1. Use tree ✗ 2. Use doubly linked list C o l u m n s Key 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 INCLUDE (in VM and/or in table) 5. Apply remaining filters

  32. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 Other 
 (in b-tree key: <=, <, =, >, >=) ✓ 1. Use tree ✗ 2. Use doubly linked list C o l u m n s Key 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 INCLUDE ✗ (in VM and/or in table) 5. Apply remaining filters

  33. B-tree Index: INCLUDE (since 11) O p e r a t i o n s Safe 
 Other 
 (in b-tree key: <=, <, =, >, >=) ✓ 1. Use tree ✗ 2. Use doubly linked list C o l u m n s Key 3. Apply “safe” filters (based on the operator class) 4. Check visibility 
 INCLUDE ✗ (in VM and/or in table) Not applicable: 
 INCLUDE columns 
 5. Apply remaining filters have no op class

  34. INCLUDE — Disadvantages: WHERE SELECT data FROM … WHERE k = $1 AND data = $2

  35. INCLUDE — Disadvantages: WHERE SELECT data FROM … WHERE k = $1 AND data = $2 Allow Index Only Scan

  36. INCLUDE — Disadvantages: WHERE SELECT data FROM … WHERE k = $1 AND data = $2 Safe in B-Tree Key

  37. INCLUDE — Disadvantages: WHERE SELECT data FROM … WHERE k = $1 AND data = $2 Safe in B-Tree Key CREATE INDEX … ON … ( k, data ) QUERY PLAN ------------------------------------------------------- Index Only Scan using … on … (actual rows=1) Index Cond: (key = $1) AND ( data =$2 ) Heap Fetches: 0 Bu ff ers: shared hit=5

  38. INCLUDE — Disadvantages: WHERE SELECT data FROM … WHERE k = $1 AND data = $2 CREATE INDEX … CREATE INDEX … ON … ON … ( k ) ( k, data ) INCLUDE ( data ) QUERY PLAN QUERY PLAN --------------------------------------------------------- ------------------------------------------------------- Index Only Scan using … on … (actual rows=1) Index Only Scan using … on … (actual rows=1) Index Cond: (key = $1) Index Cond: (key = $1) AND ( data =$2 ) Filter: (data = $2) Heap Fetches: 0 Rows Removed by Filter: 9999 Bu ff ers: shared hit=5 Heap Fetches: 0 Bu ff ers: shared hit=56

  39. INCLUDE — Pro and Con vs. Key-Columns Advantages: Disadvantages: ‣ Shallower: < ~40% ‣ Slightly smaller: < ~3%

  40. INCLUDE — Pro and Con vs. Key-Columns Advantages: Disadvantages: ‣ Shallower: < ~40% ‣ No safe WHERE conditions 
 (visibility always checked first) ‣ Slightly smaller: < ~3%

  41. INCLUDE — Disadvantages: ORDER BY SELECT * FROM … WHERE k = $1 ORDER BY data LIMIT 1

  42. INCLUDE — Disadvantages: ORDER BY SELECT * FROM … WHERE k = $1 ORDER BY data LIMIT 1 CREATE INDEX … ON … ( k, data ) QUERY PLAN ------------------------------------------------------- Limit (actual rows=1) Buffers: shared hit=5 -> Index Scan using … (actual rows=1) Index Cond: (key = 123) Heap Fetches: 0 Bu ff ers: shared hit=5

  43. INCLUDE — Disadvantages: ORDER BY SELECT * FROM … WHERE k = $1 ORDER BY data LIMIT 1 CREATE INDEX … ON … ( k, data ) QUERY PLAN ------------------------------------------------------- No Sort Limit (actual rows=1) Buffers: shared hit=5 -> Index Scan using … (actual rows=1) Index Cond: (key = 123) Heap Fetches: 0 Bu ff ers: shared hit=5

  44. INCLUDE — Disadvantages: ORDER BY SELECT * FROM … WHERE k = $1 ORDER BY data LIMIT 1 CREATE INDEX … CREATE INDEX … ON … ON … ( k ) ( k, data ) INCLUDE ( data ) QUERY PLAN QUERY PLAN ------------------------------------------------------------ ------------------------------------------------------- No Sort Limit (actual rows=1) Limit (actual rows=1) -> Sort (actual rows=1) Buffers: shared hit=5 -> Bitmap Heap Scan on…(actual rows=10000 ) -> Index Scan using … (actual rows=1) Recheck Cond: (key = 123) Index Cond: (key = 123) Heap Blocks: exact=10000 Heap Fetches: 0 Bu ff ers: shared hit=10053 Bu ff ers: shared hit=5 -> Bitmap Index Scan on…(act rows=10000 ) Index Cond: (key = 123) Buffers: shared hit=53

  45. INCLUDE — Pro and Con vs. Key-Columns Advantages: Disadvantages: ‣ Shallower: < ~40% ‣ Doesn’t help WHERE 
 (except Index Only Scan ) ‣ Slightly smaller: < ~3%

  46. INCLUDE — Pro and Con vs. Key-Columns Advantages: Disadvantages: ‣ Shallower: < ~40% ‣ Doesn’t help WHERE 
 (except Index Only Scan ) ‣ Slightly smaller: < ~3% ‣ Cannot replace sorting 
 ( ORDER BY )

  47. INCLUDE — Differences: Constraints

  48. INCLUDE — Differences: Constraints ALTER TABLE … ADD PRIMARY KEY|UNIQUE 
 (key, data)

  49. INCLUDE — Differences: Constraints ALTER TABLE … INSERT INTO … , (1, 2) ✓ ADD PRIMARY KEY|UNIQUE 
 VALUES (1, 1) (key, data)

  50. INCLUDE — Differences: Constraints ALTER TABLE … INSERT INTO … , (1, 2) ✓ ADD PRIMARY KEY|UNIQUE 
 VALUES (1, 1) (key, data) ALTER TABLE … ADD PRIMARY KEY|UNIQUE 
 (key) INCLUDE(data)

  51. INCLUDE — Differences: Constraints ALTER TABLE … INSERT INTO … ✓ ADD PRIMARY KEY|UNIQUE 
 VALUES (1, 1) (key, data) , (1, 2) ALTER TABLE … INSERT INTO … ✗ ADD PRIMARY KEY|UNIQUE 
 VALUES (1, 1) (key) , (1, 2) INCLUDE(data)

  52. INCLUDE — Pro and Con vs. Key-Columns Advantages: Disadvantages: ‣ Shallower: < ~40% ‣ Doesn’t help WHERE 
 (except Index Only Scan ) ‣ Slightly smaller: < ~3% ‣ Cannot replace sorting 
 ( ORDER BY ) Differences: PRIMARY KEY / UNIQUE don’t take INCLUDE columns into account

  53. INCLUDE and the Three Powers Of B-tree Indexes https://use-the-index-luke.com/

  54. INCLUDE and the Three Powers Of B-tree Indexes ‣ Finding data quickly https://use-the-index-luke.com/

Recommend


More recommend