Mongo Performance Optimization Using Indexing
-
Upload
chinmay-naik -
Category
Technology
-
view
585 -
download
1
description
Transcript of Mongo Performance Optimization Using Indexing
![Page 1: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/1.jpg)
Performance Optimization Strategies for MongoDB
choosing right database server hardware
schema design (denormalizing schema)
query optimization ($in, $nin)
Indexing
choosing approapriate shard key in sharding clusters
![Page 2: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/2.jpg)
What are indexes?
![Page 3: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/3.jpg)
Chemist Drawer
![Page 4: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/4.jpg)
Indexing = technique used to make search faster
![Page 5: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/5.jpg)
Computer Science definition
Index = any data structure that improves the performance of lookup.
![Page 6: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/6.jpg)
DB Index datastructures
Binary Tree B+ Tree Balanced Tree Hashes
![Page 7: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/7.jpg)
Binary Search Tree
![Page 8: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/8.jpg)
Our Favourite Employee Table
![Page 9: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/9.jpg)
![Page 10: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/10.jpg)
![Page 11: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/11.jpg)
![Page 12: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/12.jpg)
![Page 13: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/13.jpg)
![Page 14: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/14.jpg)
![Page 15: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/15.jpg)
![Page 16: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/16.jpg)
![Page 17: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/17.jpg)
![Page 18: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/18.jpg)
Search By Employee Id
select * from employee where employee_id= 3
![Page 19: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/19.jpg)
![Page 20: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/20.jpg)
B+ Tree
The B-tree is a generalization of a binary search tree in that a node can have more than two children
Order of B-Tree= max no of child nodes The left subtree of a node contains only nodes
with keys less than the node's key. he right subtree of a node contains only nodes
with keys greater than the node's key.
![Page 21: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/21.jpg)
![Page 22: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/22.jpg)
A database index improves data retrieval operations but they come up
with the cost. slower writes and the use of more
storage space.
3 Gigabytes of collection, if you have 1 index, approx it uses 500 Mb for that index
![Page 23: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/23.jpg)
INDEX CARDINALITY
Cardinality: Unique values in the column
![Page 24: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/24.jpg)
![Page 25: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/25.jpg)
![Page 26: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/26.jpg)
MONGO DOCUMENT
{
employee_id : 8
Name : “john”
Salary : 2000
}
{
employee_id : 5
Name : “james”
Salary : 3000
}
![Page 27: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/27.jpg)
![Page 28: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/28.jpg)
![Page 29: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/29.jpg)
TAKE AWAY...
Index Datastructure Index Cardinality Indexing is not the only solution to improve the
performance
![Page 30: Mongo Performance Optimization Using Indexing](https://reader036.fdocuments.us/reader036/viewer/2022062513/5554ac27b4c90502618b52d0/html5/thumbnails/30.jpg)
Points to consider while creating index
Keys (columns) frequently involved in search conditions of a query
Indexes can be created on Array, Sub-documents and also Embedded Fields
Use Indexes to Sort Query Results Queries that return a range of values using operators such as $gt,$lt Negation: Inequality queries are inefficient with respect to indexes
High cardinality (firstname). If low cardinality (gender) then indexing is not efficient Low selectivity indexes: An index should radically reduce the set of possible documents to select
from. Creating multiple indexes in support of a single query: MongoDB will use a single index to optimize a
query. If you need
to specify multiple predicates, you need a compound index. Compound index are ordered by field and order matters
Indexes have storage requirements, and impacts insert/update speed to some degree
For queries with the $or operator, each clause of an $or query executes in parallel, and can each use a different index.