The importance of indexes in mongo db

14
The Importance of Indexes in MongoDB How we increased the loading speed Profile Gliphs and Insights James Toyer, Lead Software Engineer at Glipho

description

 

Transcript of The importance of indexes in mongo db

Page 1: The importance of indexes in mongo db

The Importance of Indexes in MongoDB

How we increased the loading speed Profile Gliphs and Insights

James Toyer, Lead Software Engineer at Glipho

Page 2: The importance of indexes in mongo db

What is Glipho?

Social network for text based content

Aims to better engage writers and readers

Original content only

Not an aggregator

Automatically share to Facebook, LinkedIn and Twitter

Page 3: The importance of indexes in mongo db

Insights Page

Load up the gliphs a writer has

Iterate through, and sum, actions for each gliph

Load and sum actions for the writers profile

This can be over 100 calls to the database We know it’s inefficient

but it does the job for now

Page 4: The importance of indexes in mongo db

Insights Document Structure

Timestamp – when the action took place

EntityId – identifier of the original entity

ActionType – the type of the entity (probably should be entity type)

Action – the actual action that took place

Page 5: The importance of indexes in mongo db

Helpful Error Page…it’s all gone wrong!

Page 6: The importance of indexes in mongo db

Troubleshooting

CPU spiking? NO

Memory high? NO

Disk IO high? NO

Are there any actual regular hits happening? NO

Do you know anything? NO

Crack out the code performance tools…

Page 7: The importance of indexes in mongo db

Pre-Index performance

• 3 passes on each filter page

• Average time for each page to load = 3.9 seconds

• “ListAll” method calls the database

• “ListAll” is iterated over for each gliph in the database and the profile (in this case ~10 times)

• Average time in “ListAll” 256ms

Page 8: The importance of indexes in mongo db

More Troubleshooting

Is the code doing obviously stupid things? NO

Has Linq screwed you over again? NO

Do you trust the driver? PROBABLY

Check the database ~ 400,000 documents (now ~690,000)

No indexes

Page 9: The importance of indexes in mongo db

Know your query

GetMongoQuery code Output

Page 10: The importance of indexes in mongo db

Index analysis

Without action field Query structure

Query time before index: 334ms

Index

Query time after index: >1ms

With action field Query structure

Query time before index: 409ms

Index

Query time after index: >1ms

Page 11: The importance of indexes in mongo db

Post-Index performance

Pre-index performance Page load time:

3.9s

“ListAll” method execution time: 256ms

Post-index performance Page load time:

72ms

“ListAll” method execution time: >0.2ms

Page load time deceased by 98%

“ListAll” method execution time decreased by 99%

Page 12: The importance of indexes in mongo db

Gliph listings for Writers

Problems: Slow loading

Sometimes erroring out

Reasons: Indexes were no longer

accurate

Code had changed

Solution: New indexes

Remove old indexes

Page 13: The importance of indexes in mongo db

What did I learn?

Know exactly what queries are being run

Don’t do a “best guess” on an index. Test them out

Don’t “forget” to add indexes

Ensure your indexes evolve as your queries do

Page 14: The importance of indexes in mongo db

Any Questions?

[email protected]

glipho.com/james

@jamestoyer