SlideShare a Scribd company logo
3
Most read
8
Most read
14
Most read
Indexing and Query Performance
in MongoDB
Malak Abu Hammad
Introduction
● The Dual Challenge: Ensuring data retrieval is swift while managing resources
effectively.
● Role of Indexing: The bridge to effective and efficient querying.
So .. What’s an Index?
● Indexes are data structures that support the efficient execution of queries in
MongoDB. They contain copies of parts of the data in documents to make queries
more efficient.
● Without indexes, MongoDB must scan every document in a collection to find the
documents that match each query.
● Types of indexes:
○ Single index
○ Compound index
Query Structure
● Query criteria
● Options, such as read concern
● Projection criteria (optional)
db.collection.find(
{name: “malak”},
{birthdate : 1, _id : 0}
).readConcern("majority")
Why Indexing is Essential?
● Read Performance: Faster data access, reduced wait times.
● Resource Management: Efficient CPU and memory utilization.
Creating and Managing Indexes
● Create single index
db.collection.createIndex(
{field: 1},
{
unique: true,
name: “abc”
}
)
● Viewing Indexes
db.collection.getIndexes();
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"status" : 1
},
"name" : "status_1"
}
]
Compound Indexes
● Create compound index index
db.collection.createIndex(
{
field1: 1,
field2: -1
},
{
unique: true,
name: “abc”
}
)
● Order of fields in index and in a
query matters.
● Prefixes
For instance, if you have a compound index on
{a: 1, b: 1, c: 1}
The possible prefix indexes are
● {a: 1}
● {a: 1, b: 1}
MongoDB can use the compound index for queries
that filter on:
● Only a
● Both a and b
● All a, b, and c
Special Index Types and Use Cases
● Text Indexes: For searching text content in documents.
● Geospatial Indexes: Finding items within proximity.
● Wildcard Indexes: Flexible indexing for evolving schemas.
Performance Analysis
● Explain Method: Understanding how MongoDB executes a query.
● Spotting Slow Queries: Using MongoDB logs and monitoring tools.
○ MongoDB has a built-in profiler that logs all operations taking longer than a specified threshold.
Visual tools can represent this data, making it easy to spot problematic operations or patterns.
● Visualization
○ Visual tools can represent this data, making it easy to spot problematic operations or patterns.
○ Such as: Atlas , Compass
Performance Analysis - Explain Method
db.collection.find({quantity: {$gte: 100, $lte: 200}})
.explain("executionStats")
{// without index
queryPlanner: {
...
winningPlan: {
queryPlan: {
stage: 'COLLSCAN',
}
}
},
executionStats: {
executionSuccess: true,
nReturned: 3,
executionTimeMillis: 0,
totalKeysExamined: 0,
totalDocsExamined: 10,
executionStages: {
stage: 'COLLSCAN',
},
},
}
{ //with index
queryPlanner: {
winningPlan: {
queryPlan: {
stage: 'FETCH',
inputStage: {
stage: 'IXSCAN',
keyPattern: {
quantity: 1
},
}
}
},
rejectedPlans: [ ]
},
executionStats: {
executionSuccess: true,
nReturned: 3,
executionTimeMillis: 0,
totalKeysExamined: 3,
totalDocsExamined: 3,
executionStages: {
},
},
}
Query Optimization
● Index Selection
○ Query Planner
MongoDB's query planner evaluates the available indexes and chooses the most efficient way to
execute the query. The following steps are taken:
■ Candidate Indexes
■ Plan Generation
■ Plan Evaluation
○ Index Intersection
○ Query Selectivity
○ Cache
○ Impact of Write Operations
● Hint Method: Forcing a specific index.
● Covered Queries: Efficiently fetching data without scanning documents.
Covered query
A covered query is a query that can be satisfied entirely using an index and
does not have to examine any documents
An index covers a query if this criteria applies:
● All the fields in the query are part of an index, and
● All the fields returned in the results are in the same index, and
● No fields in the query are equal to null (i.e. {"field" : null} or {"field" : {$eq :
null}} ).
Write Performance & Indexes
● The Trade-off: Every index adds to write overhead.
● Striking a Balance: Periodic assessment is mandatory.
Index Maintenance
● Fragmentation: Over time, index
efficiency can degrade.
● Rebuilding: Periodically refreshing
indexes.
db.collection.reIndex()
● Monitoring: Using built-in tools to
watch index performance.
Best Practices
● Avoid Over-indexing: Too many indexes can backfire.
● Analyze Workloads: Adjust indexing strategies based on real-world usage.
● Operational Overhead: Be aware of the cost of maintaining indexes.
Case Study: E-Commerce Platform Product Search Optimization
An e-commerce platform, named "ShopMMS" was experiencing performance issues. As
they scaled and added more products, users began to report slow search times when
looking for products. The platform was built on a MongoDB backend.
Problem
● As the number of products grew into the millions, searches that used to take
milliseconds started taking several seconds.
● The platform's reviews and ratings system, which allowed users to filter and sort
products based on ratings, added further complexity to the search queries.
Questions & Answers
Further Resources
● Official MongoDB Documentation
https://www.mongodb.com/docs/manual/indexes/
● MongoDB University: M201: MongoDB Performance
https://learn.mongodb.com/courses/m201-mongodb-performance
● “Indexing and Query Performance in MongoDB” presentation will be available on
slideshare
https://www.slideshare.net/mms414/
Feedback & Networking
● MongoDB Arabic Community
Linkedin: https://www.linkedin.com/company/mongodb-arabic-community/
ً‫ا‬‫ﺷﻜﺮ‬
Thank you

More Related Content

PPTX
Mongodb Performance
DOCX
What are the major components of MongoDB and the major tools used in it.docx
PPTX
Introduction To MongoDB
PDF
Quick overview on mongo db
PDF
Nosql part 2
PDF
MongoDB World 2019: Fast Machine Learning Development with MongoDB
PDF
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
PDF
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
Mongodb Performance
What are the major components of MongoDB and the major tools used in it.docx
Introduction To MongoDB
Quick overview on mongo db
Nosql part 2
MongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB

Similar to Indexing and Query Performance in MongoDB.pdf (20)

PPTX
MongoDB_ppt.pptx
PPT
Fast querying indexing for performance (4)
PPTX
Indexing Strategies to Help You Scale
PDF
Introduction to MongoDB and its best practices
DOCX
unit 4,Indexes in database.docx
PPTX
MOOC_PRESENTATION_FINAL_PART_1[1].pptx
PPTX
Mongo db
PPTX
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
PPTX
MongoDB using Grails plugin by puneet behl
PDF
MongoDB NoSQL database a deep dive -MyWhitePaper
PDF
Mongodb
PPTX
LlamaIndex_HassGeek_Workshop_for_AI.pptx
PPTX
MongoDB - An Introduction
PPTX
Novedades de MongoDB 3.6
PPTX
Elasticsearch an overview
PDF
Mongodb in-anger-boston-rb-2011
PPTX
How to learn MongoDB for beginner's
PDF
Introduction to Databases - query optimizations for MySQL
PDF
MongoDB
PDF
Introduction to mongo db
MongoDB_ppt.pptx
Fast querying indexing for performance (4)
Indexing Strategies to Help You Scale
Introduction to MongoDB and its best practices
unit 4,Indexes in database.docx
MOOC_PRESENTATION_FINAL_PART_1[1].pptx
Mongo db
MongoDB.local DC 2018: Tips and Tricks for Avoiding Common Query Pitfalls
MongoDB using Grails plugin by puneet behl
MongoDB NoSQL database a deep dive -MyWhitePaper
Mongodb
LlamaIndex_HassGeek_Workshop_for_AI.pptx
MongoDB - An Introduction
Novedades de MongoDB 3.6
Elasticsearch an overview
Mongodb in-anger-boston-rb-2011
How to learn MongoDB for beginner's
Introduction to Databases - query optimizations for MySQL
MongoDB
Introduction to mongo db
Ad

Recently uploaded (20)

PPTX
assetexplorer- product-overview - presentation
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Nekopoi APK 2025 free lastest update
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
System and Network Administraation Chapter 3
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Introduction to Artificial Intelligence
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
ai tools demonstartion for schools and inter college
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
assetexplorer- product-overview - presentation
PTS Company Brochure 2025 (1).pdf.......
Nekopoi APK 2025 free lastest update
Wondershare Filmora 15 Crack With Activation Key [2025
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
System and Network Administraation Chapter 3
Navsoft: AI-Powered Business Solutions & Custom Software Development
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Migrate SBCGlobal Email to Yahoo Easily
Introduction to Artificial Intelligence
Design an Analysis of Algorithms II-SECS-1021-03
wealthsignaloriginal-com-DS-text-... (1).pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
ai tools demonstartion for schools and inter college
Softaken Excel to vCard Converter Software.pdf
L1 - Introduction to python Backend.pptx
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Ad

Indexing and Query Performance in MongoDB.pdf

  • 1. Indexing and Query Performance in MongoDB Malak Abu Hammad
  • 2. Introduction ● The Dual Challenge: Ensuring data retrieval is swift while managing resources effectively. ● Role of Indexing: The bridge to effective and efficient querying.
  • 3. So .. What’s an Index? ● Indexes are data structures that support the efficient execution of queries in MongoDB. They contain copies of parts of the data in documents to make queries more efficient. ● Without indexes, MongoDB must scan every document in a collection to find the documents that match each query. ● Types of indexes: ○ Single index ○ Compound index
  • 4. Query Structure ● Query criteria ● Options, such as read concern ● Projection criteria (optional) db.collection.find( {name: “malak”}, {birthdate : 1, _id : 0} ).readConcern("majority")
  • 5. Why Indexing is Essential? ● Read Performance: Faster data access, reduced wait times. ● Resource Management: Efficient CPU and memory utilization.
  • 6. Creating and Managing Indexes ● Create single index db.collection.createIndex( {field: 1}, { unique: true, name: “abc” } ) ● Viewing Indexes db.collection.getIndexes(); [ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" }, { "v" : 2, "key" : { "status" : 1 }, "name" : "status_1" } ]
  • 7. Compound Indexes ● Create compound index index db.collection.createIndex( { field1: 1, field2: -1 }, { unique: true, name: “abc” } ) ● Order of fields in index and in a query matters. ● Prefixes For instance, if you have a compound index on {a: 1, b: 1, c: 1} The possible prefix indexes are ● {a: 1} ● {a: 1, b: 1} MongoDB can use the compound index for queries that filter on: ● Only a ● Both a and b ● All a, b, and c
  • 8. Special Index Types and Use Cases ● Text Indexes: For searching text content in documents. ● Geospatial Indexes: Finding items within proximity. ● Wildcard Indexes: Flexible indexing for evolving schemas.
  • 9. Performance Analysis ● Explain Method: Understanding how MongoDB executes a query. ● Spotting Slow Queries: Using MongoDB logs and monitoring tools. ○ MongoDB has a built-in profiler that logs all operations taking longer than a specified threshold. Visual tools can represent this data, making it easy to spot problematic operations or patterns. ● Visualization ○ Visual tools can represent this data, making it easy to spot problematic operations or patterns. ○ Such as: Atlas , Compass
  • 10. Performance Analysis - Explain Method db.collection.find({quantity: {$gte: 100, $lte: 200}}) .explain("executionStats") {// without index queryPlanner: { ... winningPlan: { queryPlan: { stage: 'COLLSCAN', } } }, executionStats: { executionSuccess: true, nReturned: 3, executionTimeMillis: 0, totalKeysExamined: 0, totalDocsExamined: 10, executionStages: { stage: 'COLLSCAN', }, }, } { //with index queryPlanner: { winningPlan: { queryPlan: { stage: 'FETCH', inputStage: { stage: 'IXSCAN', keyPattern: { quantity: 1 }, } } }, rejectedPlans: [ ] }, executionStats: { executionSuccess: true, nReturned: 3, executionTimeMillis: 0, totalKeysExamined: 3, totalDocsExamined: 3, executionStages: { }, }, }
  • 11. Query Optimization ● Index Selection ○ Query Planner MongoDB's query planner evaluates the available indexes and chooses the most efficient way to execute the query. The following steps are taken: ■ Candidate Indexes ■ Plan Generation ■ Plan Evaluation ○ Index Intersection ○ Query Selectivity ○ Cache ○ Impact of Write Operations ● Hint Method: Forcing a specific index. ● Covered Queries: Efficiently fetching data without scanning documents.
  • 12. Covered query A covered query is a query that can be satisfied entirely using an index and does not have to examine any documents An index covers a query if this criteria applies: ● All the fields in the query are part of an index, and ● All the fields returned in the results are in the same index, and ● No fields in the query are equal to null (i.e. {"field" : null} or {"field" : {$eq : null}} ).
  • 13. Write Performance & Indexes ● The Trade-off: Every index adds to write overhead. ● Striking a Balance: Periodic assessment is mandatory.
  • 14. Index Maintenance ● Fragmentation: Over time, index efficiency can degrade. ● Rebuilding: Periodically refreshing indexes. db.collection.reIndex() ● Monitoring: Using built-in tools to watch index performance.
  • 15. Best Practices ● Avoid Over-indexing: Too many indexes can backfire. ● Analyze Workloads: Adjust indexing strategies based on real-world usage. ● Operational Overhead: Be aware of the cost of maintaining indexes.
  • 16. Case Study: E-Commerce Platform Product Search Optimization An e-commerce platform, named "ShopMMS" was experiencing performance issues. As they scaled and added more products, users began to report slow search times when looking for products. The platform was built on a MongoDB backend. Problem ● As the number of products grew into the millions, searches that used to take milliseconds started taking several seconds. ● The platform's reviews and ratings system, which allowed users to filter and sort products based on ratings, added further complexity to the search queries.
  • 18. Further Resources ● Official MongoDB Documentation https://www.mongodb.com/docs/manual/indexes/ ● MongoDB University: M201: MongoDB Performance https://learn.mongodb.com/courses/m201-mongodb-performance ● “Indexing and Query Performance in MongoDB” presentation will be available on slideshare https://www.slideshare.net/mms414/
  • 19. Feedback & Networking ● MongoDB Arabic Community Linkedin: https://www.linkedin.com/company/mongodb-arabic-community/