SlideShare a Scribd company logo
1 of 29
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

What's hot

MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introductionsethfloydjr
 
Building a Directed Graph with MongoDB
Building a Directed Graph with MongoDBBuilding a Directed Graph with MongoDB
Building a Directed Graph with MongoDBTony Tam
 
Sekilas PHP + mongoDB
Sekilas PHP + mongoDBSekilas PHP + mongoDB
Sekilas PHP + mongoDBHadi Ariawan
 
MongoDB - visualisation of slow operations
MongoDB - visualisation of slow operationsMongoDB - visualisation of slow operations
MongoDB - visualisation of slow operationsKay1A
 
MongoDB Thinkorswim
MongoDB ThinkorswimMongoDB Thinkorswim
MongoDB ThinkorswimSethEdwards
 
MongoDB Knowledge Shareing
MongoDB Knowledge ShareingMongoDB Knowledge Shareing
MongoDB Knowledge ShareingPhilip Zhong
 
Mongo db improve the performance of your application codemotion2016
Mongo db improve the performance of your application codemotion2016Mongo db improve the performance of your application codemotion2016
Mongo db improve the performance of your application codemotion2016Juan Antonio Roy Couto
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDBCésar Trigo
 
MongoDB NYC Python
MongoDB NYC PythonMongoDB NYC Python
MongoDB NYC PythonMike Dirolf
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptDr. Ahmed Al Zaidy
 
MongoDB Aggregation MongoSF May 2011
MongoDB Aggregation MongoSF May 2011MongoDB Aggregation MongoSF May 2011
MongoDB Aggregation MongoSF May 2011Chris Westin
 
Introduction to Apache Tajo: Data Warehouse for Big Data
Introduction to Apache Tajo: Data Warehouse for Big DataIntroduction to Apache Tajo: Data Warehouse for Big Data
Introduction to Apache Tajo: Data Warehouse for Big DataJihoon Son
 
Introduction to Apache Tajo: Future of Data Warehouse
Introduction to Apache Tajo: Future of Data WarehouseIntroduction to Apache Tajo: Future of Data Warehouse
Introduction to Apache Tajo: Future of Data WarehouseJihoon Son
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 

What's hot (20)

MongoDB Concepts
MongoDB ConceptsMongoDB Concepts
MongoDB Concepts
 
MongoDB Basics Unileon
MongoDB Basics UnileonMongoDB Basics Unileon
MongoDB Basics Unileon
 
Mongo db
Mongo dbMongo db
Mongo db
 
Mongo db
Mongo dbMongo db
Mongo db
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
 
Building a Directed Graph with MongoDB
Building a Directed Graph with MongoDBBuilding a Directed Graph with MongoDB
Building a Directed Graph with MongoDB
 
Sekilas PHP + mongoDB
Sekilas PHP + mongoDBSekilas PHP + mongoDB
Sekilas PHP + mongoDB
 
MongoDB - visualisation of slow operations
MongoDB - visualisation of slow operationsMongoDB - visualisation of slow operations
MongoDB - visualisation of slow operations
 
MongoDB Thinkorswim
MongoDB ThinkorswimMongoDB Thinkorswim
MongoDB Thinkorswim
 
MongoDB Knowledge Shareing
MongoDB Knowledge ShareingMongoDB Knowledge Shareing
MongoDB Knowledge Shareing
 
Mongo db improve the performance of your application codemotion2016
Mongo db improve the performance of your application codemotion2016Mongo db improve the performance of your application codemotion2016
Mongo db improve the performance of your application codemotion2016
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
MongoDB NYC Python
MongoDB NYC PythonMongoDB NYC Python
MongoDB NYC Python
 
Logs management
Logs managementLogs management
Logs management
 
Chapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScriptChapter 9 Getting Started with JavaScript
Chapter 9 Getting Started with JavaScript
 
MongoDB Aggregation MongoSF May 2011
MongoDB Aggregation MongoSF May 2011MongoDB Aggregation MongoSF May 2011
MongoDB Aggregation MongoSF May 2011
 
Introduction to Apache Tajo: Data Warehouse for Big Data
Introduction to Apache Tajo: Data Warehouse for Big DataIntroduction to Apache Tajo: Data Warehouse for Big Data
Introduction to Apache Tajo: Data Warehouse for Big Data
 
Introduction to Apache Tajo: Future of Data Warehouse
Introduction to Apache Tajo: Future of Data WarehouseIntroduction to Apache Tajo: Future of Data Warehouse
Introduction to Apache Tajo: Future of Data Warehouse
 
MongoDB Schema Design Tips & Tricks
MongoDB Schema Design Tips & TricksMongoDB Schema Design Tips & Tricks
MongoDB Schema Design Tips & Tricks
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 

Similar to Mongodb Performance

Indexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfIndexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfMalak Abu Hammad
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleMongoDB
 
Fast querying indexing for performance (4)
Fast querying   indexing for performance (4)Fast querying   indexing for performance (4)
Fast querying indexing for performance (4)MongoDB
 
Text search with Elasticsearch on AWS
Text search with Elasticsearch on AWSText search with Elasticsearch on AWS
Text search with Elasticsearch on AWSŁukasz Przybyłek
 
Ledingkart Meetup #2: Scaling Search @Lendingkart
Ledingkart Meetup #2: Scaling Search @LendingkartLedingkart Meetup #2: Scaling Search @Lendingkart
Ledingkart Meetup #2: Scaling Search @LendingkartMukesh Singh
 
Redis Day TLV 2018 - RediSearch Aggregations
Redis Day TLV 2018 - RediSearch AggregationsRedis Day TLV 2018 - RediSearch Aggregations
Redis Day TLV 2018 - RediSearch AggregationsRedis Labs
 
Page 18Goal Implement a complete search engine. Milestones.docx
Page 18Goal Implement a complete search engine. Milestones.docxPage 18Goal Implement a complete search engine. Milestones.docx
Page 18Goal Implement a complete search engine. Milestones.docxsmile790243
 
What are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docxWhat are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docxTechnogeeks
 
unit 4,Indexes in database.docx
unit 4,Indexes in database.docxunit 4,Indexes in database.docx
unit 4,Indexes in database.docxRaviRajput416403
 
Elasticsearch an overview
Elasticsearch   an overviewElasticsearch   an overview
Elasticsearch an overviewAmit Juneja
 
BlaBlaCar Elastic Search Feedback
BlaBlaCar Elastic Search FeedbackBlaBlaCar Elastic Search Feedback
BlaBlaCar Elastic Search Feedbacksinfomicien
 
Elasticsearch selected topics
Elasticsearch selected topicsElasticsearch selected topics
Elasticsearch selected topicsCube Solutions
 
Journey through high performance django application
Journey through high performance django applicationJourney through high performance django application
Journey through high performance django applicationbangaloredjangousergroup
 
MongoDB 3.2 - a giant leap. What’s new?
MongoDB 3.2 - a giant leap. What’s new?MongoDB 3.2 - a giant leap. What’s new?
MongoDB 3.2 - a giant leap. What’s new?Binary Studio
 

Similar to Mongodb Performance (20)

Indexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdfIndexing and Query Performance in MongoDB.pdf
Indexing and Query Performance in MongoDB.pdf
 
Nosql part 2
Nosql part 2Nosql part 2
Nosql part 2
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
Mongodb Introduction
Mongodb IntroductionMongodb Introduction
Mongodb Introduction
 
Mongo indexes
Mongo indexesMongo indexes
Mongo indexes
 
Fast querying indexing for performance (4)
Fast querying   indexing for performance (4)Fast querying   indexing for performance (4)
Fast querying indexing for performance (4)
 
Query Optimization in MongoDB
Query Optimization in MongoDBQuery Optimization in MongoDB
Query Optimization in MongoDB
 
Text search with Elasticsearch on AWS
Text search with Elasticsearch on AWSText search with Elasticsearch on AWS
Text search with Elasticsearch on AWS
 
Ledingkart Meetup #2: Scaling Search @Lendingkart
Ledingkart Meetup #2: Scaling Search @LendingkartLedingkart Meetup #2: Scaling Search @Lendingkart
Ledingkart Meetup #2: Scaling Search @Lendingkart
 
Redis Day TLV 2018 - RediSearch Aggregations
Redis Day TLV 2018 - RediSearch AggregationsRedis Day TLV 2018 - RediSearch Aggregations
Redis Day TLV 2018 - RediSearch Aggregations
 
Page 18Goal Implement a complete search engine. Milestones.docx
Page 18Goal Implement a complete search engine. Milestones.docxPage 18Goal Implement a complete search engine. Milestones.docx
Page 18Goal Implement a complete search engine. Milestones.docx
 
What are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docxWhat are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docx
 
unit 4,Indexes in database.docx
unit 4,Indexes in database.docxunit 4,Indexes in database.docx
unit 4,Indexes in database.docx
 
Mongodb
MongodbMongodb
Mongodb
 
Mongo db
Mongo dbMongo db
Mongo db
 
Elasticsearch an overview
Elasticsearch   an overviewElasticsearch   an overview
Elasticsearch an overview
 
BlaBlaCar Elastic Search Feedback
BlaBlaCar Elastic Search FeedbackBlaBlaCar Elastic Search Feedback
BlaBlaCar Elastic Search Feedback
 
Elasticsearch selected topics
Elasticsearch selected topicsElasticsearch selected topics
Elasticsearch selected topics
 
Journey through high performance django application
Journey through high performance django applicationJourney through high performance django application
Journey through high performance django application
 
MongoDB 3.2 - a giant leap. What’s new?
MongoDB 3.2 - a giant leap. What’s new?MongoDB 3.2 - a giant leap. What’s new?
MongoDB 3.2 - a giant leap. What’s new?
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

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

  1. DungND
  2. 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") } }