Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | 1
Why Isn't My Query Using an Index?
An introduction to SQL performance
Chris Saxon, Oracle Developer Advocate for SQL
chris.saxon@oracle.com
@chrisrsaxon & @sqldaily
blogs.oracle.com/sql
www.youtube.com/c/TheMagicofSQL
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Why isn’t Oracle using my
#@!%
index?!
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Colour BAG Colour BAG Colour BAG Colour BAG Colour BAG
Blue 73 Brown 56 Green 11 Orange 19 Red 2
Blue 31 Brown 40 Green 31 Orange 19 Red 41
Blue 1 Brown 41 Green 70 Orange 85 Red 21
Blue 64 Brown 65 Green 4 Red 42 Red 32
Blue 69 Brown 23 Green 80 Red 100 Red 12
Blue 85 Brown 21 Green 28 Red 70 Red 83
Blue 84 Brown 73 Green 44 Red 38 Yellow 29
Blue 40 Brown 97 Green 99 Red 33 Yellow 31
Blue 41 Brown 57 Green 32 Red 63 Yellow 51
Blue 88 Brown 63 Orange 85 Red 22 Yellow 33
Blue 100 Brown 100 Orange 21 Red 80 Yellow 4
Blue 6 Brown 80 Orange 76 Red 80 Yellow 9
Blue 64 Brown 75 Orange 32 Red 40 Yellow 27
Blue 90 Green 20 Orange 93 Red 68 Yellow 48
Blue 24 Green 50 Orange 70 Red 54 Yellow 56
Brown 88 Green 85 Orange 2 Red 93 Yellow 15
Brown 50 Green 17 Orange 15 Red 93 Yellow 81
Brown 69 Green 27 Orange 5 Red 20 Yellow 83
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
What’s this got to do with
?
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
M&M => a row
Document => an index
A bag => a block
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Method #1 => TABLE ACCESS FULL
Method #2 => INDEX RANGE SCAN
Multiblock reads
Single block reads
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Source: Pixabay
Which is faster?
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
FULL TABLE SCAN
Visit each bag once
INDEX RANGE SCAN
Visit each bag ~10 times
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
What if there’s a
Promotion?
silver sweets in 1% of bags
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
FULL TABLE SCAN
Visit each bag once
INDEX RANGE SCAN
One bag visit
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
The
Cost Based Optimizer
estimates these calculations
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
DEMO
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
"M & Ms (3685102458)" by Greg Willis from Denver, CO, usa Uploaded by russavia. CC BY-SA 2.0
Colour BAG Colour BAG Colour BAG Colour BAG Colour BAG
Blue 1 Brown 7 Green 13 Orange 19 Red 25
Blue 1 Brown 7 Green 13 Orange 19 Red 25
Blue 1 Brown 7 Green 13 Orange 19 Red 25
Blue 2 Brown 8 Green 14 Red 20 Red 26
Blue 2 Brown 8 Green 14 Red 20 Red 26
Blue 2 Brown 8 Green 14 Red 20 Red 26
Blue 3 Brown 9 Green 15 Red 21 Yellow 27
Blue 3 Brown 9 Green 15 Red 21 Yellow 27
Blue 3 Brown 9 Green 15 Red 21 Yellow 27
Blue 4 Brown 10 Orange 16 Red 22 Yellow 28
Blue 4 Brown 10 Orange 16 Red 22 Yellow 28
Blue 4 Brown 10 Orange 16 Red 22 Yellow 28
Blue 5 Brown 10 Orange 17 Red 23 Yellow 29
Blue 5 Green 11 Orange 17 Red 23 Yellow 29
Blue 5 Green 11 Orange 17 Red 23 Yellow 29
Brown 6 Green 11 Orange 18 Red 24 Yellow 30
Brown 6 Green 12 Orange 18 Red 24 Yellow 30
Brown 6 Green 12 Orange 18 Red 24 Yellow 30
Colour BAG Colour BAG Colour BAG Colour BAG Colour BAG
Blue 1 Brown 3 Green 4 Orange 7 Red 9
Blue 1 Brown 4 Green 4 Orange 8 Red 9
Blue 2 Brown 5 Green 5 Orange 9 Red 9
Blue 2 Brown 5 Green 6 Red 1 Red 9
Blue 3 Brown 5 Green 6 Red 2 Red 10
Blue 4 Brown 6 Green 7 Red 2 Red 10
Blue 6 Brown 7 Green 8 Red 3 Yellow 1
Blue 7 Brown 8 Green 8 Red 3 Yellow 1
Blue 7 Brown 8 Green 10 Red 4 Yellow 2
Blue 8 Brown 9 Orange 1 Red 4 Yellow 2
Blue 9 Brown 9 Orange 2 Red 5 Yellow 3
Blue 9 Brown 10 Orange 3 Red 6 Yellow 5
Blue 10 Brown 10 Orange 3 Red 6 Yellow 5
Blue 10 Green 1 Orange 4 Red 7 Yellow 6
Blue 10 Green 2 Orange 4 Red 7 Yellow 7
Brown 2 Green 3 Orange 5 Red 8 Yellow 8
Brown 2 Green 3 Orange 6 Red 8 Yellow 9
Brown 2 Green 3 Orange 6 Red 8 Yellow 10
Clustering factor
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
clustering factor = 10 (good)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
clustering factor = 120 (bad)
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
DEMO
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
How do I
Improve
the clustering factor?
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Tannin Heap Simon A. Eugster CC-BY-3.0
By default
tables are
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Organized
Part ition ed
Clusters
How do I ensure
consistent
clustering?
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
There can be only 1!
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Colour
Weight
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
Cost
Rows
Full Table Scan
Index Range Scan 1
Index Range Scan 2
Index Range Scan 3
Rows
“Toy Blocks (6831034024)” welcometolearn CC BY 2.0
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |
developer.oracle.com
#MakeDataGreatAgain
Why Isn't My Query Using an Index?: An Introduction to SQL Performance

Why Isn't My Query Using an Index?: An Introduction to SQL Performance

  • 1.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | 1 Why Isn't My Query Using an Index? An introduction to SQL performance Chris Saxon, Oracle Developer Advocate for SQL chris.saxon@oracle.com @chrisrsaxon & @sqldaily blogs.oracle.com/sql www.youtube.com/c/TheMagicofSQL
  • 2.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Why isn’t Oracle using my #@!% index?!
  • 3.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. |
  • 4.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. |
  • 5.
    Colour BAG ColourBAG Colour BAG Colour BAG Colour BAG Blue 73 Brown 56 Green 11 Orange 19 Red 2 Blue 31 Brown 40 Green 31 Orange 19 Red 41 Blue 1 Brown 41 Green 70 Orange 85 Red 21 Blue 64 Brown 65 Green 4 Red 42 Red 32 Blue 69 Brown 23 Green 80 Red 100 Red 12 Blue 85 Brown 21 Green 28 Red 70 Red 83 Blue 84 Brown 73 Green 44 Red 38 Yellow 29 Blue 40 Brown 97 Green 99 Red 33 Yellow 31 Blue 41 Brown 57 Green 32 Red 63 Yellow 51 Blue 88 Brown 63 Orange 85 Red 22 Yellow 33 Blue 100 Brown 100 Orange 21 Red 80 Yellow 4 Blue 6 Brown 80 Orange 76 Red 80 Yellow 9 Blue 64 Brown 75 Orange 32 Red 40 Yellow 27 Blue 90 Green 20 Orange 93 Red 68 Yellow 48 Blue 24 Green 50 Orange 70 Red 54 Yellow 56 Brown 88 Green 85 Orange 2 Red 93 Yellow 15 Brown 50 Green 17 Orange 15 Red 93 Yellow 81 Brown 69 Green 27 Orange 5 Red 20 Yellow 83
  • 6.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | What’s this got to do with ?
  • 7.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | M&M => a row Document => an index A bag => a block
  • 8.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Method #1 => TABLE ACCESS FULL Method #2 => INDEX RANGE SCAN Multiblock reads Single block reads
  • 9.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Source: Pixabay Which is faster?
  • 10.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | FULL TABLE SCAN Visit each bag once INDEX RANGE SCAN Visit each bag ~10 times
  • 11.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | What if there’s a Promotion? silver sweets in 1% of bags
  • 12.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | FULL TABLE SCAN Visit each bag once INDEX RANGE SCAN One bag visit
  • 13.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | The Cost Based Optimizer estimates these calculations
  • 14.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | DEMO
  • 15.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | "M & Ms (3685102458)" by Greg Willis from Denver, CO, usa Uploaded by russavia. CC BY-SA 2.0
  • 16.
    Colour BAG ColourBAG Colour BAG Colour BAG Colour BAG Blue 1 Brown 7 Green 13 Orange 19 Red 25 Blue 1 Brown 7 Green 13 Orange 19 Red 25 Blue 1 Brown 7 Green 13 Orange 19 Red 25 Blue 2 Brown 8 Green 14 Red 20 Red 26 Blue 2 Brown 8 Green 14 Red 20 Red 26 Blue 2 Brown 8 Green 14 Red 20 Red 26 Blue 3 Brown 9 Green 15 Red 21 Yellow 27 Blue 3 Brown 9 Green 15 Red 21 Yellow 27 Blue 3 Brown 9 Green 15 Red 21 Yellow 27 Blue 4 Brown 10 Orange 16 Red 22 Yellow 28 Blue 4 Brown 10 Orange 16 Red 22 Yellow 28 Blue 4 Brown 10 Orange 16 Red 22 Yellow 28 Blue 5 Brown 10 Orange 17 Red 23 Yellow 29 Blue 5 Green 11 Orange 17 Red 23 Yellow 29 Blue 5 Green 11 Orange 17 Red 23 Yellow 29 Brown 6 Green 11 Orange 18 Red 24 Yellow 30 Brown 6 Green 12 Orange 18 Red 24 Yellow 30 Brown 6 Green 12 Orange 18 Red 24 Yellow 30
  • 17.
    Colour BAG ColourBAG Colour BAG Colour BAG Colour BAG Blue 1 Brown 3 Green 4 Orange 7 Red 9 Blue 1 Brown 4 Green 4 Orange 8 Red 9 Blue 2 Brown 5 Green 5 Orange 9 Red 9 Blue 2 Brown 5 Green 6 Red 1 Red 9 Blue 3 Brown 5 Green 6 Red 2 Red 10 Blue 4 Brown 6 Green 7 Red 2 Red 10 Blue 6 Brown 7 Green 8 Red 3 Yellow 1 Blue 7 Brown 8 Green 8 Red 3 Yellow 1 Blue 7 Brown 8 Green 10 Red 4 Yellow 2 Blue 8 Brown 9 Orange 1 Red 4 Yellow 2 Blue 9 Brown 9 Orange 2 Red 5 Yellow 3 Blue 9 Brown 10 Orange 3 Red 6 Yellow 5 Blue 10 Brown 10 Orange 3 Red 6 Yellow 5 Blue 10 Green 1 Orange 4 Red 7 Yellow 6 Blue 10 Green 2 Orange 4 Red 7 Yellow 7 Brown 2 Green 3 Orange 5 Red 8 Yellow 8 Brown 2 Green 3 Orange 6 Red 8 Yellow 9 Brown 2 Green 3 Orange 6 Red 8 Yellow 10
  • 18.
  • 19.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | clustering factor = 10 (good)
  • 20.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | clustering factor = 120 (bad)
  • 21.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | DEMO
  • 22.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | How do I Improve the clustering factor?
  • 23.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Tannin Heap Simon A. Eugster CC-BY-3.0 By default tables are
  • 24.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Organized Part ition ed Clusters How do I ensure consistent clustering?
  • 25.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | There can be only 1!
  • 26.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Colour Weight
  • 27.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | Cost Rows Full Table Scan Index Range Scan 1 Index Range Scan 2 Index Range Scan 3
  • 28.
    Rows “Toy Blocks (6831034024)”welcometolearn CC BY 2.0
  • 29.
    Copyright © 2015,Oracle and/or its affiliates. All rights reserved. | developer.oracle.com #MakeDataGreatAgain