SlideShare a Scribd company logo
Alex Bevilacqua
The Sights (and Smells) of a Bad Query
@alexbevi
INTRODUCTION
{ context: “speaker”, smell: “pleasant” }
Alex Bevilacqua
Technical Services Engineer, MongoDB
IT’S ME … MARIO!
ALEX!
• Application Developer
• Development Lead
• System Architect
• Technical Services
Engineer
• Author
• MongoDB Fanboy
Sights?
What we’ll be looking at
• Generic looks at some queries
• Introduction to some tooling
• High level discussion
• Not focusing on “solutions”
Smells?
What stinks?
https://en.wikipedia.org/wiki/Code_smell
A code smell is any characteristic in the source code of
a program that possibly indicates a deeper problem.
Code smells are usually not bugs; they are not technically incorrect and
do not prevent the program from functioning.
Instead, they indicate weaknesses in design that may slow down
development or increase the risk of bugs or failures in the future.
Scenario
• Atlas M10 Cluster
• 2-3M documents
• Generated and imported
using a template
(mgeneratejs)
• Ran some adhoc queries
SIMPLE QUERY
{ context: “find”, smell: “good?” }
Our Dataset
What’re we looking for?
db.users.find({ age: 38 })
Let’s find some data!
• Found 53,516 Results
• Took a few seconds longer
than we’d like ☹
Who’s seen
this?
ALERTS =
Let’s see that again … as an Explain Plan
• Query time higher than we
would expect
• COLLSCAN
• Large number of documents
scanned (entire collection!)
COLLSCAN =
Adding an Index
db.users.createIndex({ age: 1 })
Explain that again please
• Performance greatly improved
• IXSCAN
• Fewer documents scanned
QUERYING
MULTIPLE FIELDS
AND SORTING
{ context: { $in: [“find”, “sort”] }, smell: “good?” }
What’re we looking for?
db.users.find({ age: { $gt: 25 }, “address.state”: “UT” }).sort({ name: 1 })
Let’s find some data – in order this time
• Looking for everyone older than
25 from Utah, sorted by name
• IXSCAN …. But slow
• Far more keys and documents
examined than returned (we’ll
come back to this one)
• In memory sort!
SORT
=
We know we
need an
index!
Let’s try that again …
What’s the explain telling us?
• IXSCAN using the
expected index
• Faster execution
• Same number of
documents scanned
and returned …
• In memory sort???
(E)quality – (S)ort – (R)ange
db.users.find({ age: { $gt: 25 }, ‘address.state’: ‘UT’ }).sort({ name: 1 })
Rang
e
Equality Sort
Non-ESR
Compound =
Indexes
METRICS AND
LOGS AND
CHARTS …
OH MY!
{ context: “analysis”, smell: “good?” }
What can the Atlas Metrics tell us?
• Sort operation that
couldn’t use an index
• Ratio of documents
scanned to the number of
documents returned
• Increased average
read/write time per
operation
Sharp rise
in one or =
more metrics
Remember
this?
Check the Performance Advisor
Get your logs
Plotting queries by execution time
... COMMAND [conn416] command data.users command: aggregate {
aggregate: "users", pipeline: [ { $match: {} }, { $skip: 0 }, { $group: { _id:
null, n: { $sum: 1 } } } ], ... planSummary: COLLSCAN keysExamined:0
docsExamined:2585100 cursorExhausted:1 numYields:20196
nreturned:1 reslen:243 ... 1892ms
mlogfilter *.log --markers none | mplotqueries --logscale
Queries slower
than slowms =
Plotting queries by docsExamined/n
… COMMAND [conn1136] command data.users … { find: "users",
filter: { age: { $gt: 25.0 }, address.state: "UT" }, sort: { name: 1.0 }, …
planSummary: IXSCAN { age: 1 } cursorid:73909299301
keysExamined:2154391 docsExamined:2154391 hasSortStage:1
numYields:17029 nreturned:101 … 94212ms
mlogfilter *.log --markers none | mplotqueries --type docsExamined/n
Large
docsExamined/n =
What can stink?
• Alerts (from Atlas, Ops Manager, Cloud Manager)
• COLLSCAN (in logs or explain plans)
• SORT (in explain plans or in logs as hasSortStage)
• Non-ESR Compound Indexes
• Sharp rise in one or more metrics (in Atlas, Ops Manager or
Cloud Manager)
• Queries slower than slowms
• Large docsExamined/n ratio
Links
Notes/links from this talk available at:
http://bit.ly/ab-mdbw19
alexbevi
alexbevi
@alexbevi
MongoDB World 2019: The Sights (and Smells) of a Bad Query
THANK YOU FOR ATTENDING!
QUESTIONS?
MongoDB World 2019: The Sights (and Smells) of a Bad Query

More Related Content

PDF
MongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDB
PDF
MongoDB .local Toronto 2019: Tips and Tricks for Effective Indexing
PDF
Data Modeling for MongoDB
PDF
Getting Started: Atlas Search Webinar
PPTX
An Introduction to MongoDB Ops Manager
PPT
MongoDB Schema Design
PPTX
Mongo db intro.pptx
PDF
MongodB Internals
MongoDB World 2019: Tips and Tricks++ for Querying and Indexing MongoDB
MongoDB .local Toronto 2019: Tips and Tricks for Effective Indexing
Data Modeling for MongoDB
Getting Started: Atlas Search Webinar
An Introduction to MongoDB Ops Manager
MongoDB Schema Design
Mongo db intro.pptx
MongodB Internals

What's hot (20)

PDF
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
PDF
Indexing
PPT
Fast querying indexing for performance (4)
PDF
Indexing and Performance Tuning
PPTX
Indexing with MongoDB
PDF
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
PDF
Introduction to Mongodb execution plan and optimizer
PDF
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
PDF
MongoDB Performance Tuning
PDF
Working with JSON Data in PostgreSQL vs. MongoDB
PPTX
An Enterprise Architect's View of MongoDB
PPTX
MongoDB and Indexes - MUG Denver - 20160329
PDF
MongoDB WiredTiger Internals
PPTX
Understanding and tuning WiredTiger, the new high performance database engine...
PDF
Introducing DataFrames in Spark for Large Scale Data Science
PDF
An introduction to MongoDB
PDF
An overview of Neo4j Internals
PDF
MongoDB Fundamentals
PDF
MongoDB Aggregation Framework
PDF
Data Source API in Spark
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
Indexing
Fast querying indexing for performance (4)
Indexing and Performance Tuning
Indexing with MongoDB
Naver속도의, 속도에 의한, 속도를 위한 몽고DB (네이버 컨텐츠검색과 몽고DB) [Naver]
Introduction to Mongodb execution plan and optimizer
MongoDB Schema Design (Event: An Evening with MongoDB Houston 3/11/15)
MongoDB Performance Tuning
Working with JSON Data in PostgreSQL vs. MongoDB
An Enterprise Architect's View of MongoDB
MongoDB and Indexes - MUG Denver - 20160329
MongoDB WiredTiger Internals
Understanding and tuning WiredTiger, the new high performance database engine...
Introducing DataFrames in Spark for Large Scale Data Science
An introduction to MongoDB
An overview of Neo4j Internals
MongoDB Fundamentals
MongoDB Aggregation Framework
Data Source API in Spark
Ad

Similar to MongoDB World 2019: The Sights (and Smells) of a Bad Query (20)

PDF
Finding Love with MongoDB
PDF
Indexing and Query Performance in MongoDB.pdf
PPTX
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
PDF
The Query Engine: The Life of a Read
PPT
Indexing & query optimization
PDF
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
PPTX
Indexing Strategies to Help You Scale
PDF
Mongo db improve the performance of your application codemotion2016
PDF
Indexing and Query Optimizer (Mongo Austin)
PDF
Buildingsocialanalyticstoolwithmongodb
PDF
MongoDB Atlas Workshop - Singapore
PPTX
Indexing In MongoDB
PPTX
Query Optimization in MongoDB
PPTX
Webinar: Performance Tuning + Optimization
PDF
Mongo db a deep dive of mongodb indexes
PPTX
Performance Tuning and Optimization
PPTX
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
PPTX
Automated Slow Query Analysis: Dex the Index Robot
PPTX
Using Compass to Diagnose Performance Problems in Your Cluster
PPTX
Using Compass to Diagnose Performance Problems
Finding Love with MongoDB
Indexing and Query Performance in MongoDB.pdf
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
The Query Engine: The Life of a Read
Indexing & query optimization
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
Indexing Strategies to Help You Scale
Mongo db improve the performance of your application codemotion2016
Indexing and Query Optimizer (Mongo Austin)
Buildingsocialanalyticstoolwithmongodb
MongoDB Atlas Workshop - Singapore
Indexing In MongoDB
Query Optimization in MongoDB
Webinar: Performance Tuning + Optimization
Mongo db a deep dive of mongodb indexes
Performance Tuning and Optimization
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Automated Slow Query Analysis: Dex the Index Robot
Using Compass to Diagnose Performance Problems in Your Cluster
Using Compass to Diagnose Performance Problems
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Electronic commerce courselecture one. Pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Spectroscopy.pptx food analysis technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Cloud computing and distributed systems.
PPTX
Big Data Technologies - Introduction.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Machine Learning_overview_presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation
Electronic commerce courselecture one. Pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectroscopy.pptx food analysis technology
Dropbox Q2 2025 Financial Results & Investor Presentation
The Rise and Fall of 3GPP – Time for a Sabbatical?
Advanced methodologies resolving dimensionality complications for autism neur...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Per capita expenditure prediction using model stacking based on satellite ima...
Cloud computing and distributed systems.
Big Data Technologies - Introduction.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Empathic Computing: Creating Shared Understanding
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Machine Learning_overview_presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Building Integrated photovoltaic BIPV_UPV.pdf

MongoDB World 2019: The Sights (and Smells) of a Bad Query