I have a collection in MongoDB with 20 million documents and an index created for the collection. I have confirmed that the find() query uses the index (col_1_col_2) (confirmed using the explain). However, I’ve noticed that the very first find() query in a day takes 5-10 sec, but all subsequent queries take less than 100 ms.
Updated DB stats:
db..stats()
ns .
count 20135438
size 5694.57m
avgObjSize 296.55146314672
storageSize 7109.57m
numExtents 35
nindexes 4
lastExtentSize 1188.86m
paddingFactor 1
flags 1
totalIndexSize 3044.69m
indexSizes {
“id“: 653319632,
“col_1_col_2”: 691730480,
“col2_1”: 848832320,
“col1_1”: 998706576
}
ok 1
{top:1}
total 18
readLock 16
Questions:
1 Why does the first query take longer time?
2 Is this to do with loading the index in RAM?
check the disk activity during this first request. It could be related to mounting data from disk to RAM.
If you use linux : iostat -m 1 will help you.