SlideShare a Scribd company logo
MongoDB
Performance
1. G3 use case
2. How to detect slow queries
3. How to optimize for speed
4. Discussion
Summary
I. G3 Use case
● ~ X M records
● Insert XX K records/day
● Bidding system requirements:
○ Update result near real-time
○ Use updated result for bidding process
○ Many campaigns run at the same time
-> Many queries at the same time, in a short period
-> Many queries have the same result (May be)
● Inactive clients is not used for pushing process
I. G3 Use case
● Before:
4 instances x 4 consumers
I. G3 Use case
● After:
(1 instance x 4 consumers)
How to detect slow queries
● Mongodb logs
○ /var/log/mongodb/mongodb.log
● Use explain function
○ totalDocsExamined
○ totalDocsExamined
● Visualize query explain on Studio 3T
explain() function
● How many documents were scanned
● How many documents were returned
● Which index was used
● How long the query took to be executed
● Which alternative execution plans were evaluated
Example for explain()
db.users.createIndex({ "amount": 1 })
db.users.find({ amount : { $in : [ 15, 305, 292 ] } }).explain(true)
Result:
"nReturned" : 4.0,
"executionTimeMillis" : 0.0,
"totalKeysExamined" : 7.0,
"totalDocsExamined" : 4.0,
Example for explain()
db.users.createIndex({ "amount": 1 })
db.users.find({ amount : { $in : [ 5, 10, 15 ] } }).explain(true)
Result:
"nReturned" : 4.0,
"executionTimeMillis" : 0.0,
"totalKeysExamined" : 5.0,
"totalDocsExamined" : 4.0,
Example for explain()
db.users.createIndex({ "amount":"hashed"})
db.users.find({ amount : { $in : [ 5, 10, 15 ] } }).explain(true)
Result:
"nReturned" : 4.0,
"executionTimeMillis" : 0.0,
"totalKeysExamined" : 7.0,
"totalDocsExamined" : 4.0,
Index types
● _id
● Single field indexes
● MultiKey indexes
● Compound indexes
● Hash indexes
● Text sparse
● TTL indexes
● Unique
● Partial (filtered)
● GeoIndexes (GeoHaystack, 2d spherical, flat 2d indexes)
How does index work?
Costs of maintaining indexes
● Each Index adds cost to the writing process
● A lot of indexes will probably slow down the write performance.
Default index _id
● _id is the default primary key in MongoDB.
● _id is not a clustered index and the database must perform another operation so as to
read all the values of the document.
● Single field is a simple index and it indexes only one field at a time.
● Creating a single field index will help the query optimizer find the desired
document quickly.
Single field index
Compound indexes
● Compound indexes can support queries that match on multiple fields
● If we filter by field A and field B and only the field A is indexed, we may need to open a lot
of documents to read the field B. On the other hand, if field B is already indexed, there is
no need to do so
● Use the higher cardinality field as first field in the btree.
Multikey Indexes
● Multikey Indexes are used when the field value is an array
● For the document { likes : ['mongodb', 'mysql'] }, the multikey will generate 2 different
index keys pointing to the same document.
● Support text search queries on string content. text indexes can include any field
whose value is a string or an array of string elements.
● A collection only create a text indexes, but a compound index can include a text
index key.
● Supported Languages and Stop Words, Scoring, Stemming
Text Index
● Hash indexes are commonly used in shards to create random keys for the writes
and increase the write performance
● Cannot find by range
● Document reference https://www.slideshare.net/daumdna/mongodb-scaling-write-performance
Hashed Index
Other index
● TTL indexes : is a separate thread that runs periodically (usually every minute) and
scans a collection, that has a TTL index defined, for any expired documents and
removes them in the background.
● Unique indexes : A unique index ensures that the indexed fields do not store
duplicate values
● Partial (filtered) indexes: only indexes the documents that meet a specified filter
expression.
● GeoIndexes (GeoHaystack, 2d spherical, flat 2d indexes)
Performance tips for MongoDB
- Should not use queries: $ne / $nin / $or
- Ensure Indexes Fit RAM
Performance tips for MongoDB
- Build the index in the background to not affect the performance of MongoDB.
- Regex should not be used with large collection
Example: Link
Performance tips for MongoDB
● Slow Aggregates
○ Aggregates in Mongo are super powerful! However unlike other queries they will be
touching most data in the Database (because you probably are trying to generate
aggregate data or some kind of report). So they can easily become the bottleneck of
your service.
○ MongoDB has a Hard limit for the data that is passed in the aggregation pipeline of
100MB. You can turn on Disk usage to go around this but it will really slow down
everything. ↗
● The $match and $sort pipeline operators can take advantage of an index
when they occur at the beginning of the pipeline.
-> Hạn chế sử dụng Aggregation và thay bằng việc tính trước ra một collection khác.
Reference document
Selective index
Rules of Compound Index
- Equality fields before range fields
- Order equality fields
from most selective (most unique)
to least selective (least unique)
- Add sorted field to the end of index
Selective index - demo
Query
{
“active”: true
“enqueued_time”: {
$lte: new Date(1565756739076)
},
“modified_at”: {
$lte: new Date(1565756739076)
},
“country_code”: “VN”
}
Sort: { “enqueued_time”: 1 }
Data range
active: true/false
country_code: ~150 countries (VN, ID, TH)
enqueued_time: timestamp
modified_at: timestamp
Index
{ “country_code”: 1, “active”: 1, “enqueued_time”: 1 }
Selective index - demo
Using Partial Indexes
Only index the documents in a collection that meet a specified filter expression
Index
{ “country_code”: 1, “active”: 1, “enqueued_time”: 1 }
Query
{
“active”: true
…
}
New Index
{ “country_code”: 1, “enqueued_time”: 1 }
{ partialFilterExpression: { active: true } }
Selective index - demo
Using Partial Indexes
Only index the documents in a collection that meet a specified filter expression
Index
{ “country_code”: 1, “active”: 1, “enqueued_time”: 1 }
Query
{
“active”: true
…
}
New Index
{ “country_code”: 1, “enqueued_time”: 1 }
{ partialFilterExpression: { active: true } }
Keep in mind
● Use MongoDB logs, query explain… to detect slow query
● Indexes support the efficient execution of queries in MongoDB
● Should create index in background
● Ensure indexes fit in RAM
● Create indexes & queries that ensure selectivity
References
- Mongo Performance https://github.com/danielabar/mongo-performance
- Tips and Tricks for Avoiding Common Query Pitfalls https://www.slideshare.net/mongodb/mongodblocal-dc-2018-tips-
and-tricks-for-avoiding-common-query-pitfalls
- Performance notes for MongoDB: https://blogs.msdn.microsoft.com/shacorn/2016/01/08/performance-notes-for-
mongodb/
- Index strategies:
https://docs.mongodb.com/manual/applications/indexes/
- Mongodb logging config:
https://docs.mongodb.com/v3.2/reference/configuration-options/
- Scaling write performance:
https://www.slideshare.net/daumdna/mongodb-scaling-write-performance
- MongoDB Indexes and Performance
https://hackernoon.com/mongodb-indexes-and-performance-2e8f94b23c0a

More Related Content

PDF
MongoDB performance
PPTX
Amazon services ec2
PPTX
Splunk overview
PDF
AWS EC2
PDF
Intro to Elasticsearch
PPTX
Introduction to Azure Functions
PPTX
Introduction to AWS VPC, Guidelines, and Best Practices
PDF
CodeBuild CodePipeline CodeDeploy CodeCommit in AWS | Edureka
MongoDB performance
Amazon services ec2
Splunk overview
AWS EC2
Intro to Elasticsearch
Introduction to Azure Functions
Introduction to AWS VPC, Guidelines, and Best Practices
CodeBuild CodePipeline CodeDeploy CodeCommit in AWS | Edureka

What's hot (20)

PPTX
AWS SQS SNS
PPTX
Introduction to Elasticsearch
PPTX
Radware - WAF (Web Application Firewall)
PDF
2020 07-30 elastic agent + ingest management
PDF
클라우드 비용, 어떻게 줄일 수 있을까? - 구본민, AWS 클라우드 파이넌셜 매니저 :: AWS Builders 100
PPTX
AWS VPC Fundamental
ODP
Query DSL In Elasticsearch
PPTX
PPTX
Elastic Compute Cloud (EC2) on AWS Presentation
PPTX
AWS VPC & Networking basic concepts
PDF
Deep Dive: Amazon DynamoDB (db tech showcase 2016)
PDF
AWS RDS
PDF
Elasticsearch From the Bottom Up
PPT
Cloud Computing and Amazon Web Services
PPT
Lucene basics
PPTX
Burn down chart | Coepd
PDF
Le novità di SQL Server 2022
PDF
Extracting keywords from texts - Sanda Martincic Ipsic
PPTX
Indexing with MongoDB
PPTX
AWS network services
AWS SQS SNS
Introduction to Elasticsearch
Radware - WAF (Web Application Firewall)
2020 07-30 elastic agent + ingest management
클라우드 비용, 어떻게 줄일 수 있을까? - 구본민, AWS 클라우드 파이넌셜 매니저 :: AWS Builders 100
AWS VPC Fundamental
Query DSL In Elasticsearch
Elastic Compute Cloud (EC2) on AWS Presentation
AWS VPC & Networking basic concepts
Deep Dive: Amazon DynamoDB (db tech showcase 2016)
AWS RDS
Elasticsearch From the Bottom Up
Cloud Computing and Amazon Web Services
Lucene basics
Burn down chart | Coepd
Le novità di SQL Server 2022
Extracting keywords from texts - Sanda Martincic Ipsic
Indexing with MongoDB
AWS network services
Ad

Similar to Mongodb Performance (20)

PPTX
Query Optimization in MongoDB
ODP
Mongo indexes
DOCX
unit 4,Indexes in database.docx
PDF
Mongo db a deep dive of mongodb indexes
PPTX
MongoDB and Indexes - MUG Denver - 20160329
PDF
Indexing and Query Performance in MongoDB.pdf
PPTX
Indexing Strategies to Help You Scale
PDF
Nosql part 2
PPT
Fast querying indexing for performance (4)
PPTX
MongoDB - Indexing- Types iNDEXING.pptx
PPT
Mongo db tutorials
PDF
Mongodb Introduction
PPTX
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
PDF
Quick overview on mongo db
PDF
Mongophilly indexing-2011-04-26
PPTX
Scaling MongoDB
PDF
Indexing and Query Optimizer (Mongo Austin)
PDF
MongoDB Tips and Tricks
PPTX
MongoDB's index and query optimize
PPTX
Indexing and Query Optimizer (Aaron Staple)
Query Optimization in MongoDB
Mongo indexes
unit 4,Indexes in database.docx
Mongo db a deep dive of mongodb indexes
MongoDB and Indexes - MUG Denver - 20160329
Indexing and Query Performance in MongoDB.pdf
Indexing Strategies to Help You Scale
Nosql part 2
Fast querying indexing for performance (4)
MongoDB - Indexing- Types iNDEXING.pptx
Mongo db tutorials
Mongodb Introduction
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
Quick overview on mongo db
Mongophilly indexing-2011-04-26
Scaling MongoDB
Indexing and Query Optimizer (Mongo Austin)
MongoDB Tips and Tricks
MongoDB's index and query optimize
Indexing and Query Optimizer (Aaron Staple)
Ad

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Approach and Philosophy of On baking technology
PDF
Electronic commerce courselecture one. Pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Machine learning based COVID-19 study performance prediction
Digital-Transformation-Roadmap-for-Companies.pptx
sap open course for s4hana steps from ECC to s4
20250228 LYD VKU AI Blended-Learning.pptx
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...
Review of recent advances in non-invasive hemoglobin estimation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Approach and Philosophy of On baking technology
Electronic commerce courselecture one. Pdf
NewMind AI Weekly Chronicles - August'25 Week I
Machine learning based COVID-19 study performance prediction

Mongodb Performance

  • 2. 1. G3 use case 2. How to detect slow queries 3. How to optimize for speed 4. Discussion Summary
  • 3. I. G3 Use case ● ~ X M records ● Insert XX K records/day ● Bidding system requirements: ○ Update result near real-time ○ Use updated result for bidding process ○ Many campaigns run at the same time -> Many queries at the same time, in a short period -> Many queries have the same result (May be) ● Inactive clients is not used for pushing process
  • 4. I. G3 Use case ● Before: 4 instances x 4 consumers
  • 5. I. G3 Use case ● After: (1 instance x 4 consumers)
  • 6. How to detect slow queries ● Mongodb logs ○ /var/log/mongodb/mongodb.log ● Use explain function ○ totalDocsExamined ○ totalDocsExamined ● Visualize query explain on Studio 3T
  • 7. explain() function ● How many documents were scanned ● How many documents were returned ● Which index was used ● How long the query took to be executed ● Which alternative execution plans were evaluated
  • 8. Example for explain() db.users.createIndex({ "amount": 1 }) db.users.find({ amount : { $in : [ 15, 305, 292 ] } }).explain(true) Result: "nReturned" : 4.0, "executionTimeMillis" : 0.0, "totalKeysExamined" : 7.0, "totalDocsExamined" : 4.0,
  • 9. Example for explain() db.users.createIndex({ "amount": 1 }) db.users.find({ amount : { $in : [ 5, 10, 15 ] } }).explain(true) Result: "nReturned" : 4.0, "executionTimeMillis" : 0.0, "totalKeysExamined" : 5.0, "totalDocsExamined" : 4.0,
  • 10. Example for explain() db.users.createIndex({ "amount":"hashed"}) db.users.find({ amount : { $in : [ 5, 10, 15 ] } }).explain(true) Result: "nReturned" : 4.0, "executionTimeMillis" : 0.0, "totalKeysExamined" : 7.0, "totalDocsExamined" : 4.0,
  • 11. Index types ● _id ● Single field indexes ● MultiKey indexes ● Compound indexes ● Hash indexes ● Text sparse ● TTL indexes ● Unique ● Partial (filtered) ● GeoIndexes (GeoHaystack, 2d spherical, flat 2d indexes)
  • 12. How does index work?
  • 13. Costs of maintaining indexes ● Each Index adds cost to the writing process ● A lot of indexes will probably slow down the write performance.
  • 14. Default index _id ● _id is the default primary key in MongoDB. ● _id is not a clustered index and the database must perform another operation so as to read all the values of the document.
  • 15. ● Single field is a simple index and it indexes only one field at a time. ● Creating a single field index will help the query optimizer find the desired document quickly. Single field index
  • 16. Compound indexes ● Compound indexes can support queries that match on multiple fields ● If we filter by field A and field B and only the field A is indexed, we may need to open a lot of documents to read the field B. On the other hand, if field B is already indexed, there is no need to do so ● Use the higher cardinality field as first field in the btree.
  • 17. Multikey Indexes ● Multikey Indexes are used when the field value is an array ● For the document { likes : ['mongodb', 'mysql'] }, the multikey will generate 2 different index keys pointing to the same document.
  • 18. ● Support text search queries on string content. text indexes can include any field whose value is a string or an array of string elements. ● A collection only create a text indexes, but a compound index can include a text index key. ● Supported Languages and Stop Words, Scoring, Stemming Text Index
  • 19. ● Hash indexes are commonly used in shards to create random keys for the writes and increase the write performance ● Cannot find by range ● Document reference https://www.slideshare.net/daumdna/mongodb-scaling-write-performance Hashed Index
  • 20. Other index ● TTL indexes : is a separate thread that runs periodically (usually every minute) and scans a collection, that has a TTL index defined, for any expired documents and removes them in the background. ● Unique indexes : A unique index ensures that the indexed fields do not store duplicate values ● Partial (filtered) indexes: only indexes the documents that meet a specified filter expression. ● GeoIndexes (GeoHaystack, 2d spherical, flat 2d indexes)
  • 21. Performance tips for MongoDB - Should not use queries: $ne / $nin / $or - Ensure Indexes Fit RAM
  • 22. Performance tips for MongoDB - Build the index in the background to not affect the performance of MongoDB. - Regex should not be used with large collection Example: Link
  • 23. Performance tips for MongoDB ● Slow Aggregates ○ Aggregates in Mongo are super powerful! However unlike other queries they will be touching most data in the Database (because you probably are trying to generate aggregate data or some kind of report). So they can easily become the bottleneck of your service. ○ MongoDB has a Hard limit for the data that is passed in the aggregation pipeline of 100MB. You can turn on Disk usage to go around this but it will really slow down everything. ↗ ● The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the pipeline. -> Hạn chế sử dụng Aggregation và thay bằng việc tính trước ra một collection khác. Reference document
  • 24. Selective index Rules of Compound Index - Equality fields before range fields - Order equality fields from most selective (most unique) to least selective (least unique) - Add sorted field to the end of index
  • 25. Selective index - demo Query { “active”: true “enqueued_time”: { $lte: new Date(1565756739076) }, “modified_at”: { $lte: new Date(1565756739076) }, “country_code”: “VN” } Sort: { “enqueued_time”: 1 } Data range active: true/false country_code: ~150 countries (VN, ID, TH) enqueued_time: timestamp modified_at: timestamp Index { “country_code”: 1, “active”: 1, “enqueued_time”: 1 }
  • 26. Selective index - demo Using Partial Indexes Only index the documents in a collection that meet a specified filter expression Index { “country_code”: 1, “active”: 1, “enqueued_time”: 1 } Query { “active”: true … } New Index { “country_code”: 1, “enqueued_time”: 1 } { partialFilterExpression: { active: true } }
  • 27. Selective index - demo Using Partial Indexes Only index the documents in a collection that meet a specified filter expression Index { “country_code”: 1, “active”: 1, “enqueued_time”: 1 } Query { “active”: true … } New Index { “country_code”: 1, “enqueued_time”: 1 } { partialFilterExpression: { active: true } }
  • 28. Keep in mind ● Use MongoDB logs, query explain… to detect slow query ● Indexes support the efficient execution of queries in MongoDB ● Should create index in background ● Ensure indexes fit in RAM ● Create indexes & queries that ensure selectivity
  • 29. References - Mongo Performance https://github.com/danielabar/mongo-performance - Tips and Tricks for Avoiding Common Query Pitfalls https://www.slideshare.net/mongodb/mongodblocal-dc-2018-tips- and-tricks-for-avoiding-common-query-pitfalls - Performance notes for MongoDB: https://blogs.msdn.microsoft.com/shacorn/2016/01/08/performance-notes-for- mongodb/ - Index strategies: https://docs.mongodb.com/manual/applications/indexes/ - Mongodb logging config: https://docs.mongodb.com/v3.2/reference/configuration-options/ - Scaling write performance: https://www.slideshare.net/daumdna/mongodb-scaling-write-performance - MongoDB Indexes and Performance https://hackernoon.com/mongodb-indexes-and-performance-2e8f94b23c0a

Editor's Notes

  • #25: DungND
  • #26: DungND Note: VN: 1M2 active ID: 800k active TH: 200k active Query mkt_tool_notification.clients { "active": true, "country_code": "TH", "enqueued_time": { $lte: ISODate("2019-08-14T04:25:39.076+0000") }, "modified_at": { $lte: ISODate("2019-08-14T04:25:39.076+0000") } }