Query performance can either be a constant headache or the unsung hero of an application. MongoDB provides extremely powerful querying capabilities when used properly. As a senior member of the support team I will share more common mistakes observed and some tips and tricks to avoiding them.
8. E – S - R
The ordering of index keys in a
compound index is critically
important. E-S-R provides
guidance that is useful in most
cases:
• Equality first
• Sort next
• Range last
9. E – S - R
The ordering of index keys in a
compound index is critically
important. E-S-R provides
guidance that is useful in most
cases:
• Equality first
• Sort next
• Range last
What is the difference between
Equality and Range?
11. Definitions
Equality Fields
An exact match on a single
value. For example:
• {x:123}
• {x:{$eq:123}}
• {x:"123"}
• {"x.y":123}
Sort
The (entire) requested sort.
.sort({x:1, y:-1})
12. Definitions
Equality Fields
An exact match on a single
value. For example:
• {x:123}
• {x:{$eq:123}}
• {x:"123"}
• {"x.y":123}
Sort
The (entire) requested sort.
.sort({x:1, y:-1})
Range Predicates
Any predicates that are not
exact matches. Some
operators include:
• {x:{$gt:0}}
• {x:{$lte:1000}}
13. Equality
Equality keys are placed first in any order
If present in the query shape, equality fields should always form the
prefix for the index.
db.games.find( {gamertag: "Ace", score: {$gt: 9000}} )
14. Equality
Equality keys are placed first in any order
If present in the query shape, equality fields should always form the
prefix for the index.
db.games.find( {gamertag: "Ace", score: {$gt: 9000}} )
30. Sort
Sort fields are placed next
Placing sort predicates after sequential equality keys allow for the
index to:
• Provide a non-blocking sort.
• Minimize the amount of scanning required.
db.games.find( {gamertag: "Ace"} ).sort( {score: 1} )
48. Range
Range fields are usually last
Generally range predicates should be placed last.
This allows them to still participate in filtering the data, but does not
force a blocking sort.
db.games.find( {gamertag: "Ace", score: {$gt: 9000}} )
77. Predicate Type Check - Inequality
Inequality operators, such as the
following, are E, S, or R?
§ $ne:123
§ $nin:[1,2]
Range!
78. Predicate Type Check - Inequality
Inequality operators, such as the
following, are E, S, or R?
§ $ne:123
§ $nin:[1,2]
Range!
How do you know?
79. Predicate Type Check - Inequality
Inequality operators, such as the
following, are E, S, or R?
§ $ne:123
§ $nin:[1,2]
Range!
"indexBounds" : {
"x" : [
"[MinKey, 123.0)",
"(123.0, MaxKey]"
]
}
82. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
83. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
84. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
planecar racecar
85. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
planecar racecar
86. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
planecar racecar
87. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
88. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
raincarry Carpool
89. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
raincarry Carpool
90. Predicate Type Check - Regex
Regex operators, such as the
following, are E, S, or R?
§ {str:/car/}
§ {str:/^car/i}
Range!
raincarry Carpool
91. Predicate Type Check - $in
$in filters, as demonstrated below,
are E, S, or R?
§ {field:{$in:[1,3]}}
… it depends! Can be Equality or
Range in terms of the key ordering
92. Predicate Type Check - $in
{field:{$in:[1,3]}}
"indexBounds" : {
"field" : [
"[1.0, 1.0]",
"[3.0, 3.0]"
],
"sortField" : [
"[MinKey, MaxKey]"
]
}
1 2 3
C G B F A D
93. Predicate Type Check - $in
{field:{$in:[1,3]}}
"indexBounds" : {
"field" : [
"[1.0, 1.0]",
"[3.0, 3.0]"
],
"sortField" : [
"[MinKey, MaxKey]"
]
}
1 2 3
C G B F A D
94. Predicate Type Check - $in
{field:{$in:[1,3]}}
"indexBounds" : {
"field" : [
"[1.0, 1.0]",
"[3.0, 3.0]"
],
"sortField" : [
"[MinKey, MaxKey]"
]
}
1 2 3
C G B F A D
95. Predicate Type Check - $in
{field:{$in:[1,3]}}
"indexBounds" : {
"field" : [
"[1.0, 1.0]",
"[3.0, 3.0]"
],
"sortField" : [
"[MinKey, MaxKey]"
]
}
1 2 3
C G B F A D
96. Predicate Type Check - $in
{field:{$in:[1,3]}}
"indexBounds" : {
"field" : [
"[1.0, 1.0]",
"[3.0, 3.0]"
],
"sortField" : [
"[MinKey, MaxKey]"
]
}
1 2 3
C G B F A D
97. Predicate Type Check - $in
{field:{$in:[1,3]}}
"indexBounds" : {
"field" : [
"[1.0, 1.0]",
"[3.0, 3.0]"
],
"sortField" : [
"[MinKey, MaxKey]"
]
}
1 2 3
C G B F A D
BS
105. Sequential Equality Keys
Does it matter if there is missing predicate in the equality key list?
For example, given:
§ Query: {gamertag:"Ace", game: "Halo"}
§ Index: {gamertag:1, date:1, game:1}
Is that index as efficient as the following one?
{gamertag:1, game:1, date:1}
142. E-S-R Guidance
A good starting place applicable to most use cases
Place keys in the following order:
• Equality first
• Sort next
• Range last
Remember that equality is an exact match of a single value
143. E-S-R Guidance
A good starting place applicable to most use cases
Place keys in the following order:
• Equality first
• Sort next
• Range last
Remember that equality is an exact match of a single value
I see isMultiKey:true in
the explain output. What
does that mean?
146. Index Nomenclature
Single Field Index
The index entries reference
a single field in the
document
Can be Multikey
Compound Index
The index entries reference
more than one field in the
document
Can be Multikey
Multikey Index
There is separate entry in
the index for every array in
the document
147. Single Field Index
Ace Doyen
{ gamertag: "Ace",
games:["Mario", "Halo"] }
{ gamertag: "Doyen",
games:["Fortnite", "GTA", "Minecraft"] }{} {}
150. Compound Index - Multikey
HaloMinecraftGTAFortnite
Mario
AceDoyen
{}{}
We tried to use arrays but get
incorrect results and queries are
slow. What gives?
173. Semantics - $elemMatch
Use $elemMatch to query multiple fields of a single array element
Not necessary when querying on a single predicate
174. Semantics - $elemMatch
Use $elemMatch to query multiple fields of a single array element
Not necessary when querying on a single predicate
Okay, that change got us
correct results. But what
about the performance?
182. Multikey Path Tracking
Before MongoDB 3.4:
§ Database only contained coarse isMultiKey flag for index
§ Did not know which field was the array
§ Conservative behavior to ensure correct results
184. Multikey Path Tracking
Take advantage of the improvement
§ Index needs to be rebuilt when running 3.4 binaries
§ Just running on 3.4 is not sufficient
§ Look for multiKeyPaths field in explain output
§ v:2 index would be a deterministic check
§ Multikey index cannot cover queries on the array field
185. Multikey Path Tracking
Take advantage of the improvement
§ Index needs to be rebuilt when running 3.4 binaries
§ Just running on 3.4 is not sufficient
§ Look for multiKeyPaths field in explain output
§ v:2 index would be a deterministic check
§ Multikey index cannot cover queries on the array field
Wow! Everything is running at
Web Scale now!
190. Key Ordering Resources
Blog Post
My colleague Jesse wrote an article
which explores the performance
characteristics of different
arrangements.
MongoDB University
My colleague Kirby covers many
indexing topics in greater detail,
including key ordering, in M201:
MongoDB Performance.
191. Please provide Session Feedback
1. Go to slido.com
2. Enter event code #MDBW19
3. Click on Room Name and Provide Session
Feedback
Feedback poll will remain open for 10 minutes after the talk
ends
Questions?
Meet me now in the Leaf Lounge on the second floor of the
Partner Pavilion
YOU ARE IN MURRAY HILL