The document discusses MongoDB performance best practices. It covers topics like schema design, indexing, Linux tuning, and MongoDB performance analysis tools. Specifically, it recommends embedding data where possible, following the ESR rule for compound indexes, enabling transparent huge pages and NUMA for Linux, and using tools like MongoDB explain, mtools, and Keyhole to analyze query performance and server logs.
2. Interested in Open Source technologies
Interested in MongoDB, DevOps & DevOpSec Practices
Tech Speaker/Blogger
CTO, Mydbops IT Solution
Manosh Malai
About Me
10. Modelling Approach RDBMS
Develop Application
and Queries
Define/Re-Define Data
Model
Production
Denormalize/Poor
Performance
R
D
B
M
S
Design normalized Data Model/Schema
Develop Application
Data model dictates how to write queries for
application operation
Application evolve and data became denormalized
Re-structured the Data Model and normalized
This cause poor performance and required downtime
11. Modelling Approach MongoDB
Develop Application
and Queries
Define Data Model
Production
New Requirement
M
o
n
g
o
D
B
MQL
Develop the Application
Define the Data Model
Application evolve
Improve the Data Model
Application Evolve and improve data model will happen
recursively without any downtime and complication
Design is part of each phase of the application lifetime
12. Strategy of Modelling
Goal 1 Goal 2
Goal 3 Goal 4
Deep Knowledge about
Application behaviour
Predict C, U, R, D
Operation perform
on Database and
priorities
Based on Prediction,
map the relationship
between entities and
C, U, R, D
Finalize the Data model,
which suite to the
application
15. Choose Embedded VS Reference
How frequently does the embedded data get
accessed?
Does the embedded information change/update
often?
Is the data queried using the embedded
information?
17. Understand your application’s query patterns, Design your data model, Select the appropriate
indexes.
MongoDB has a flexible schema does not mean you can ignore schema design.
Prioritize embedding, unless there is an unavoidable reason.
Don't be afraid of application-level joins: If the index is built correctly and the returned results
are limited by projection conditions, then application-level joins will not be much more
expensive than joins in relational databases.
Key Consideration(RECAP TOO)
18. Array should not grow without bound
When the array size growing outbound, index performance on the array will fall down
Avoid lookup if they can avoided
Avoid huge number of collection
Avoid default _id Field: 12 bytes is too large and some computational cost
Optimization for keys: Every Document had schema, so every document store keyname in document
and it consume more space.
Key Consideration(RECAP TOO)
19. In the Database world, index plays a vital role in a performance, that not an exception with MongoDB
Indexing
Single Field Indexes
Compound Indexes
Multikey Indexes
Text Indexes
Wildcard Indexes
2dsphere Indexes
2d Indexes
geoHaystack Indexes
Hashed Indexes
Index Type Index Properties
TTL Indexes
Unique Indexes
Partial Indexes
Case Insensitive Indexes
Hidden Indexes
Sparse Indexes
20. Follow ESR Rule in Compound Indexes
Use Covered Queries as much possible
How Prefix Compression improves query performance and Disk usage
Indexing Strategies
and so on.
21. Follow ESR Rule in Compound Indexes
EQUAL SORT
RANGE
In Single Field Index, the document can either be ascending or descending sort regardless of the physical
ordering of the index key
ESR is no strict rule. Its just a guideline, help to produce better query performance
If we put equality key first, we will limit the amount of data we looking
Avoid blocking/In-memory sorting
Fail to follow ESR guidelines in index creation drives us to unwanted totalKeysExamined, totalDocsExamined
traversal, and put stress on memory and CPU resource. finally, executionTimeMillis of the query too more than
the advised value.
22. ESR db.emp.find({role: "mongodb-dba", exp: {$gt: 5}}).sort({location: 1})
MongoDB-DBA MySQL-DBA
ROLE:
6
LOCATION:
Bangalore Bangalore
1
Hyderabad
3
Chennai
2
EXP:
10
7
5
5
BLOCKING SORT
Chennai
2
Bangalore
1
Hyderabad
3
RESULT
ROOT db.emp.createIndex({role:1, exp:1, location:1})
E
R
S
23. ESR db.emp.find({role: "mongodb-dba", exp: {$gt: 5}}).sort({location: 1})
MongoDB-DBA MySQL-DBA
ROLE:
6
LOCATION:
Chennai
2
Hyderabad
3
EXP: 5
7 5
10
ROOT db.emp.createIndex({role:1, location:1, exp:1})
Bangalore
1
E
S
R
Bangalore
24. Key Consideration
Index creation in foreground will do collection level locking.
Index creation in the background helps to overcome the locking bottleneck but decrease the efficiency of index
traversal.
In MongoDB 4.2 version index creation system was reconstructed. This new indexing method help to
overcome the above-specified incompetence or inefficiency.
Recommend the developer to write the covered query. This kind of query will be entirely satisfied with an index.
So zero documents need to be inspected to satisfy the query, and this makes the query run lot faster. All the
projection keys need to be indexed.
25. Cont...
Combine: Possibly a Range
Use Index to sort the result and avoid blocking sort.
Remove Duplicate and unused index, it also improve the disk throughput and memory optimization.
Operator name mislead between Equality and Range, use index bound to make sure operator your using is
Range or Equality
$ne
$nin
Regex
$in Alone: Equality Match
26. B-Tree & Prefix Compression: Query performance & Disk usage
In B-Tree indexes, Low Cardinality value actually harm performance
In Low Cardinality value preference to use Partial Index
prefix Index compression- Repeated prefix value is not written
WITHOUT PREFIX COMPRESSION
MongoDB-DBA,Bangalore,10
MongoDB-DBA,Chennai,5
MySQL-DBA,Bangalore,7
MongoDB-DBA,Hyderabad,5
MongoDB-DBA,Bangalore,10
,Chennai,5
,Hyderabad,5
MySQL-DBA,Bangalore,7
Without Prefix Comp
With Prefix Comp
32. Linux Tuning
vim /etc/security/limits.conf
mongo hard cpu unlimited
mongo soft cpu unlimited
mongo hard memlock unlimited
mongo soft memlock unlimited
mongo hard nofile 64000
mongo soft nofile 64000
mongo hard nproc 192276
mongo soft nproc 192276
mongo hard fsize unlimited
mongo soft fsize unlimited
mongo hard as unlimited
mongo soft as unlimited
ulimit Settings
36. MongoDB Mtools
mtools is a collection of helper scripts to parse, filter, and visualize MongoDB log files. For every DBA this is a
Swiss army knives tools.
mlogfilter
mloginfo
mlaunch
39. Keyhole
Keyhole help to produce performance analytics summaries. The information includes MongoDB
configurations, cluster statistics, database schema, indexes, and index usages.
Analyzing mongo logs and Full-Time Diagnostic data Capture (FTDC),
Cluster Info:
keyhole --allinfo "mongodb://user:secret@host.local/test?replicaSet=rs"
FTDC Data and Grafana Integration:
keyhole --web --diag /data/db/diagnostic.data
Logs Analytics:
keyhole --loginfo -v /var/log/mongodb/mongod.log.2018-06-07T11-08-32.gz