3. About me
15 years programming experience
Software Developer and MSSQL DBA @ Com-
UT/Sedimap
playing around with MongoDB about 3 years
Quinta-feira, 8 de Agosto de 13
4. Com-UT/Sedimap an Fleet Management Company (Vehicle
track)
using MongoDB in Production about 2 years
replicaset with 5 nodes
about 3.5M of messages arrive from GPS devices and are
processed by our communication server (in Node.js) before
go to MongoDB
can check in http://www.mongodb.org/about/production-
deployments/ and find by COMUT
Quinta-feira, 8 de Agosto de 13
5. MongoDB, some wiki
font: wikipédia
developed and supported by 10gen
NoSQL DB, document-oriented database
stores structured data as JSON-like documents
with dynamic schemas
BSON - binary-encoded serialization
Quinta-feira, 8 de Agosto de 13
7. indexes are the single biggest
tunable performance factor in
MongoDB
absent or suboptimal indexes are
the most common avoidable
MongoDB performance problem
Quinta-feira, 8 de Agosto de 13
8. the application’s queries
the relative frequency of each query
in the application
which indexes the most common
queries use
Indexing Strategies
Quinta-feira, 8 de Agosto de 13
11. What can be indexed?
single field {a: 1}
compound keys / multiple fields {a: 1, b: -1}
multikeys indexes / arrays of values {a: [1,2,3]}
subdocuments
"address": {
"street": "Main”
"zipcode": 53511
"state": "WI"
}
embedded fields {“address.state”: 1}
Quinta-feira, 8 de Agosto de 13
12. _id index, is the primary key for the collection and
every doc must have a unique _id field
ObjectId("5038050ef719dd2122000004")
Instead of using the ObjectID
_id: {a: 1, b: 1, c: 1}
_id
Quinta-feira, 8 de Agosto de 13
15. Sparse indexes
db.collection.ensureIndex({b: 1}, {sparse: true})
db.collection.ensureIndex(
{b: 1},
{sparse: true, unique: true}
)
Missing fields are stored as null(s) in the index
1 - db.collection.insert({a: 12})
2 - db.collection.insert({a: 122, b:12})
Attention with sort
Quinta-feira, 8 de Agosto de 13
17. TTL collections
Document must have a BSON UTC Date field
{
a: 12,
b: 455,
c: 2323,
status: ISODate(“2013-08-08T12:00:00Z”)
}
db.collection.ensureIndex(
{"status": 1},
{expireAfterSeconds: 3600}
)
Documents are removed after ‘expireAfterSeconds’ seconds
Quinta-feira, 8 de Agosto de 13
18. Limitations
collections can not have > 64 indexes
queries can only use one index
indexes have storage requirements, and impacts
insert/update speed to some degree
operator $or
Quinta-feira, 8 de Agosto de 13
20. Explain - case A
> db.events.find().explain()
Quinta-feira, 8 de Agosto de 13
21. Explain - case B
> db.events.find({
tmx: ISODate("2012-09-09T17:00:50Z"),
mid: 41638,
eid: 46511
}).explain()
Quinta-feira, 8 de Agosto de 13
22. Explain - case C
db.events.find({
tmx:ISODate("2012-09-09T17:00:50Z"), mid:41638, eid:46511
}, {
tmx: 1, mid: 1, eid: 1, _id: 0
}).explain()
Quinta-feira, 8 de Agosto de 13
23. Profiling operations
db.setProfilingLevel(level, slowms)
level
0 = profiler off
1 = record ops longer than slowms
2 = record all queries
db.system.profile.find()
the profile collection is a capped collection and fixed in size
Quinta-feira, 8 de Agosto de 13
25. Hint a index
we can tell the database what index to use
db.collection.find({b: {$gt: 120}})
.hint({a: 1})
or tell the database to not use an index
db.collection.find({b: {$gt: 120}})
.hint({$natural: 1})
Quinta-feira, 8 de Agosto de 13
26. The query optimizer
for each “type” of query, MongoDB periodically
tries all useful indexes
aborts the rest as soon as one plan win
the winning plan is temporarily cached for each
“type” of query
Quinta-feira, 8 de Agosto de 13
28. trying to use multiple indexes
db.collection.ensureIndex({a: 1})
db.collection.ensureIndex({b: 1})
// only one of the above indexes is used
db.collection.find({a: 3, b: 10})
Quinta-feira, 8 de Agosto de 13
29. compound indexes
db.collection.ensureIndex({a: 1, b: 1, c: 1})
// can’t use the index
db.collection.find({c: 100})
// but this can’t
db.collection.find({a: 10, c: 20})
// and this ???
db.collection.find({c: 100}).sort({a: 1});
Quinta-feira, 8 de Agosto de 13
31. regular expressions
db.collection.ensureIndex({a: 1})
// left anchored regex queries can use index
db.collection.find({a: /^php/})
// but not generic regex
db.collection.find({a: /php/})
// or insensitive
db.collection.find({a: /Php/i})
Quinta-feira, 8 de Agosto de 13
32. negation
db.collection.ensureIndex({a: 1})
// not equal
db.collection.find({a: {$ne:‘php’}})
// not in
db.collection.find({a: {$nin: [‘java’,‘c++’]}})
// $not operador
db.collection.ensureIndex({a: {$not:‘c#’}})
Quinta-feira, 8 de Agosto de 13
33. TO REMEMBER
choosing the right indexes is
one of the most important
things you can do in
MongoDB
Quinta-feira, 8 de Agosto de 13