BOOSTING MONGODB PERFORMANCE
Panin Alexei, IT enthusiast
➤ Facebook: https://www.facebook.com/panin.alexei
➤ Twitter: https://twitter.com/AlexeiPanin
➤ Linkedin: https://www.linkedin.com/in/alexeipanin/
➤ Email: panin.alexei@gmail.com
MONGOBD
WHAT’S THAT
?
H
A
R
DWARE
PERFORM
A
N
C
E
CPU
( ALU + CU )
RAM / MEMORY I/O devices
VON NEUMANN
ARCHITECTURE
RANDOM ACCESS MEMORY
➤ Aggregation
➤ Index traversal
➤ Write operations
➤ Query engine
➤ Connections
RAM / MEMORY
CENTRAL PROCESSING UNIT
CPU
( ALU + CU )
➤ Storage engine
➤ Page compression, data calculation, aggregation framework, map
reduce
➤ Concurrency model
➤ By default MongoDB will use all available CPU cores for non
blocking operations
WRITES
READS
CENTRAL PROCESSING UNIT - SEQUENTIAL OPERATIONS
CPU
( ALU + CU )
➤ db.users.update({“id”: 1 }, {$inc: {“score”: 1}})
➤ db.users.update({“id”: 1 }, {$inc: {“score”: 1}})
➤ db.users.update({“id”: 1 }, {$inc: {“score”: 1}})
➤ db.users.update({“id”: 1 }, {$inc: {“score”: 1}})
➤ db.users.update({“id”: 1 }, {$inc: {“score”: 1}})
➤ db.users.update({“id”: 1 }, {$inc: {“score”: 1}})
WRITES
READS
INPUT / OUTPUT
➤ Higher IOPS - is better
Type IOPS
7200 rpm SATA ~75 - 100
15000 rpm SAS
SSD Intel X25-E ~5000
SSD Intel X25-M-GE
~8000
FUSION IO ~135 000
Violin Memory 6000 ~1 000 000
INPUT / OUTPUT - RECOMMENDED ARCHITECTURE
http://www.10gen.com/presentations/mongodb-austin/2012/mongodb-on-amazon-ec2
INPUT / OUTPUT - NOT RECOMMENDED ARCHITECTURE
http://www.10gen.com/presentations/webinar-best-
practices-mongodb-aws
INDEXES
MONGODB FILES
>> mongod --dbpath ./wired-tiger-data/
Index data
Collection data
Lock file
WiredTiger files
MONGODB FILES
>> mongod --dbpath ./wired-tiger-data/ 
--directoryperdb 
--wiredTigerDirectoryForIndexes
Index data
Collection data
Lock file
WiredTiger files
local database
admin database
SINGLE FIELD INDEX
>> db.users.createIndex({name: 1}) // ascending
>> db.users.createIndex({surname: -1}) // descending
>> mongoimport -d mongoperfdb -c items 
--drop ~/Desktop/mongo-data/assets/items.json
SINGLE FIELD INDEX
>> db.users.createIndex({name: 1}) // ascending
>> db.users.createIndex({surname: -1}) // descending
>> mongoimport -d mongoperfdb -c items 
--drop ~/Desktop/mongo-data/assets/items.json
SINGLE FIELD INDEX - SORT
>> use mongoperfdb
>> db.items.dropIndexes()
>> db.items.find({"ssn": /^880/}).sort({"ssn": 1}).explain("executionStats")
>> db.items.createIndex({"ssn": 1})
>> db.items.find({"ssn": /^880/}).sort({"ssn": 1}).explain(“executionStats")
>> db.items.find({"ssn": /^880/}).sort({"ssn": -1}).explain("executionStats")
INDEX PREFIX
>> db.items.createIndex({"first_name": 1, "last_name": 1, "job": 1})
// INDEX PREFIXES
{“first_name”}
{“first_name”, “last_name”}
MULTIKEY INDEX
PARTIAL INDEX
>> db.items.createIndex({
"first_name": 1,
"last_name": 1
}, {"partialFilterExpression": {
"job": {"$gte": “M”}
}
})
SPARSE INDEX
>> db.items.createIndex({
"first_name": 1,
"last_name": 1
}, {“sparse": true}
})
TEXT INDEX
>> db.items.createIndex({"description": “text”})
COLLATIONS
>> db.items.createIndex({“first_name”: 1}, {“colation”: “fr”})
>> db.items.find({“first_name”: “françois”})

>> db.items.find({“first_name”:
“françois”}).collation({“locale”: “fr”})
BUILDING INDEXES
Background index Foreground index
currnetOp() | killOp()
QUERY PLANS
HINTS
>> db.items.find({“first_name”: “Alexei”}).hint(“first_name”: 1)
EQUALITY | SORT | RANGE
COVERED QUERIES
db.items.dropIndexes()
db.items.createIndex({"ssn": 1})
db.items.find({"ssn": {"$gte": "5"}}, {"ssn": 1, "_id":
0}).explain("executionStats")
REGEX PERFORMANCE
db.items.dropIndexes()
db.items.createIndex({“first_name”: 1})
db.items.find({“first_name”:/alexei/}) // COLLSCAN
db.items.find({“first_name”:/^alexei/}) // IXSCAN
WRITE PERF IMPACT FACTORS
Write concern Index overhead
AGGREGATION PERFORMANCE
AGGREGATION - MEMORY CONSTRAINTS
➤ Results are subject to 16 MB
➤ Use $limit and $project
➤ 100MB limit per stage
➤ Use indexes
➤ db.orders.aggregate([], {allowDiskUse: true})
QUESTIONS ?

Boosting MongoDB performance