Speaker: Charlie Swanson, Software Engineer, MongoDB
Level: 200 (Intermediate)
Track: How We Build MongoDB
Learn how MongoDB answers your queries from a query system engineer. If you've ever had a performance problem with a query but didn't know how to find the cause, or if you've ever needed to confirm that your shiny new index is being put to work, the explain command is an excellent place to start. MongoDB's explain system is a powerful tool for solving this type of problem, but can be intimidating and unwieldy to use. In this talk, we will discuss how the explain command works and break down its output into digestible pieces.
What You Will Learn:
- Exactly how indexes are used during your queries and aggregations
- How to diagnose your poorly performing operations
- How to tune your most important operations to ensure that they scale seamlessly
4. #MDBW17
OVERVIEW
01 02 03 04 05
Motivation "queryPlanner"
Verbosity
"executionStats"
Verbosity
"allPlansExecution"
Versbosity
Beyond Queries
Why do you care? Describing considered
plans
More details about
winning plan
More details about
plan selection
Log messages
What is explain? The profile
Other commands
5. #MDBW17
01 02 03 04 05
Motivation "queryPlanner"
Verbosity
"executionStats"
Verbosity
"allPlansExecution"
Versbosity
Beyond Queries
Why do you care? Describing considered
plans
More details about
winning plan
More details about
plan selection
Log messages
What is explain? The profile
Other commands
7. #MDBW17
TARGET QUESTIONS
• Is your query using an index? Which one?
• Is your query using an index to provide the sort?
🤔
8. #MDBW17
TARGET QUESTIONS
• Is your query using an index? Which one?
• Is your query using an index to provide the sort?
• How many of the examined documents ended up matching?
🤔
9. #MDBW17
TARGET QUESTIONS
• Is your query using an index? Which one?
• Is your query using an index to provide the sort?
• How many of the examined documents ended up matching?
• Why did the server choose to answer the query the way it did?
🤔
10. #MDBW17
TARGET QUESTIONS
• Is your query using an index? Which one?
• Is your query using an index to provide the sort?
• How many of the examined documents ended up matching?
• Why was your winning plan chosen?
• Can my queries go faster?
🤔
32. #MDBW17
01 02 03 04 05
Motivation "queryPlanner"
Verbosity
"executionStats"
Verbosity
"allPlansExecution"
Versbosity
Beyond Queries
Why do you care? Describing considered
plans
More details about
winning plan
More details about
plan selection
Log messages
What is explain? The profile
Other commands
42. #MDBW17
IS YOUR QUERY USING THE INDEX YOU EXPECT?
db.tweets.explain().find(
{nFavorites: {$gte: 100000}},
{_id: 0, nFavorites: 1, username: 1})
.sort({nFavorites: -1})
43. #MDBW17
IS YOUR QUERY USING THE INDEX YOU EXPECT?
db.tweets.explain().find(
{nFavorites: {$gte: 100000}},
{_id: 0, nFavorites: 1, username: 1}).sort({nFavorites: -1})
{ "queryPlanner": {
"winningPlan": {
"stage": "PROJECTION",
"inputStage": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {"nFavorites": -1},
"indexBounds": {
"nFavorites": ["[inf.0, 100000.0]"]
} } } },
"rejectedPlans": [ ] } }
58. #MDBW17
THE POWER OF "QUERYPLANNER"
• Is your query using an index? Which one?
• Is your query using an index to provide the sort?
• Is your query using an index to provide the projection?
59. #MDBW17
NEXT UP: "IT'S USING AN INDEX, SO WHAT'S TAKING
SO LONG?"
db.tweets.explain().find({
createdDate: {$gte: <today>},
favorites: "@eliothorowitz"
})
FETCH
INDEX SCAN
keyPattern: {createdDate: 1}
60. #MDBW17
IT'S USING AN INDEX, SO WHAT'S TAKING SO LONG?
db.tweets.explain("executionStats").find({
createdDate: {$gte: <today>},
favorites: "@eliothorowitz"
})
FETCH
INDEX SCAN
keyPattern: {createdDate: 1}
12:02
12:03
12:04
…
INDEX SCAN
INDEX:
{createdAt: 1}
64. #MDBW17
SO HOW MANY OF THEM WERE THROWN OUT?
• What percentage of the index keys in the scanned range ended up
matching the predicate?
• What's the selectivity?
65. #MDBW17
01 02 03 04 05
Motivation "queryPlanner"
Verbosity
"executionStats"
Verbosity
"allPlansExecution"
Versbosity
Beyond Queries
Why do you care? Describing considered
plans
More details about
winning plan
More details about
plan selection
Log messages
What is explain? The profile
Other commands
67. #MDBW17
01 02 03 04 05
Motivation "queryPlanner"
Verbosity
"executionStats"
Verbosity
"allPlansExecution"
Versbosity
Beyond Queries
Why do you care? Describing considered
plans
More details about
winning plan
More details about
plan selection
Log messages
What is explain? The profile
Other commands
76. #MDBW17
EXECUTION STATS: WORKS, ADVANCED, ETC.
• These are all PlanStages
‒SortStage
‒FetchStage
‒IndexScanStage
FETCH
SORT
INDEX SCAN
keyPattern: {hashtags: 1}
77. #MDBW17
EXECUTION STATS: WORKS, ADVANCED, ETC.
• These are all PlanStages
• Each PlanStage implements
work()
FETCH
SORT
INDEX SCAN
keyPattern: {hashtags: 1}
78. #MDBW17
EXECUTION STATS: WORKS, ADVANCED, ETC.
• These are all PlanStages
• Each PlanStage implements
work(), returns one of:
• ADVANCED
• NEED_TIME
• IS_EOF
FETCH
SORT
INDEX SCAN
keyPattern: {hashtags: 1}
93. #MDBW17
WHAT'S THE MOST EXPENSIVE PART OF YOUR PLAN?
db.tweets.explain("executionStats").find({
createdDate: {$gte: <today>},
favorites: "@eliothorowitz"
}) FETCH
filter: {
favorites: "@eliothorowitz"
}
INDEX SCAN
keyPattern: {createdDate: 1}
94. #MDBW17
WHAT'S THE MOST EXPENSIVE PART OF YOUR PLAN?
db.tweets.explain("executionStats").find(…)
FETCH
executionTimeMillisEstimate: 431
INDEX SCAN
executionTimeMillisEstimate: 67
95. #MDBW17
WHAT'S THE MOST EXPENSIVE PART OF YOUR PLAN?
db.tweets.explain("executionStats").find(…)
FETCH
works: 2705
advanced: 314
needTime: 2391
INDEX SCAN
96. #MDBW17
OUR PROGRESS
• "queryPlanner"
• Is your query using the index you expect?
• Is your query using an index to provide the sort?
• Is your query using an index to provide the projection?
• "executionStats"
• How selective is your index?
• Which part of your plan is the most expensive?
97. #MDBW17
NEXT UP: "WHY WAS THIS PLAN CHOSEN?"
db.tweets.explain("executionStats").find({
createdDate: {$gte: <today>},
favorites: "@eliothorowitz"
})
• We had an index on {favorites: 1}, would that have been
faster?
🤔
98. #MDBW17
01 02 03 04 05
Motivation "queryPlanner"
Verbosity
"executionStats"
Verbosity
"allPlansExecution"
Versbosity
Beyond Queries
Why do you care? Describing considered
plans
More details about
winning plan
More details about
plan selection
Log messages
What is explain? The profile
Other commands
110. #MDBW17
01 02 03 04 05
Motivation "queryPlanner"
Verbosity
"executionStats"
Verbosity
"allPlansExecution"
Versbosity
Beyond Queries
Why do you care? Describing considered
plans
More details about
winning plan
More details about
plan selection
Log messages
What is explain? The profile
Other commands
125. #MDBW17
01 02 03 04 05
Motivation "queryPlanner"
Verbosity
"executionStats"
Verbosity
"allPlansExecution"
Versbosity
Beyond Queries
Why do you care? Describing considered
plans
More details about
winning plan
More details about
plan selection
Log messages
What is explain? The profile
Other commands
SUMMARY
138. #MDBW17
Your Application MongoDB
What took you
so long?!
- network latency
- large result set
{…} {…} {…} {…} {…} {…} {…}
{…}
{…}
02: EXPLAIN WON'T SOLVE ALL YOUR PROBLEMS
139. #MDBW17
Your Application MongoDB
What took you
so long?!
- network latency
- large result set
- server contention
02: EXPLAIN WON'T SOLVE ALL YOUR PROBLEMS
140. #MDBW17
Your Application MongoDB
What took you
so long?!
- network latency
- large result set
- server contention
- query planning problem
02: EXPLAIN WON'T SOLVE ALL YOUR PROBLEMS