mongodb - Searching for ranges in mongo -


what's efficient way find data in mongo, when input data single value, , collection data contains min/max ranges? e.g:

record = { min: number, max: number, payload } 

need locate record number falls within min/max range of record. ranges never intersect. there no predictability size of ranges.

the collection has ~6m records in it. if unpack ranges (have records each value in range), looking @ 4b records instead.

i've created compound index of {min:1,max:1}, attempt search using:

db.block.find({min:{$lte:value},max:{$gte:value}) 

... takes anywhere few tens of seconds. below output of explain() , getindexes(). there trick can apply make search execute faster?

njmongo:primary> db.block.getindexes() [     {             "v" : 1,             "key" : {                     "_id" : 1             },             "ns" : "mispot.block",             "name" : "_id_"     },     {             "v" : 1,             "key" : {                     "min" : 1,                     "max" : 1             },             "ns" : "mispot.block",             "name" : "min_1_max_1"     } ]    njmongo:primary> db.block.find({max:{$gte:1135194602},min:{$lte:1135194602}}).explain() {     "cursor" : "btreecursor min_1_max_1",     "ismultikey" : false,     "n" : 1,     "nscannedobjects" : 1,     "nscanned" : 1199049,     "nscannedobjectsallplans" : 1199050,     "nscannedallplans" : 2398098,     "scanandorder" : false,     "indexonly" : false,     "nyields" : 7534,     "nchunkskips" : 0,     "millis" : 5060,     "indexbounds" : {             "min" : [                     [                             -1.7976931348623157e+308,                             1135194602                     ]             ],             "max" : [                     [                             1135194602,                             1.7976931348623157e+308                     ]             ]     },     "server" : "ccc:27017" } 

if ranges of block records never overlap, can accomplish faster with:

db.block.find({min:{$lte:value}}).sort({min:-1}).limit(1) 

this query return instantly since can find record simple lookup in index.

the query running slow because 2 clauses each match on millions of records must merged. in fact, think query run faster (maybe faster) separate indexes on min , max since max part of compound index can used given min -- not search documents specific max.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -