0

I have a collection, called Cities with city info in it. Each city document has an inner IpIntervals array ({StartNum, EndNum}) that contians IpIntervals for the city. Each interval boundary is calculated using formula 256 * 256 * 256 * a + 256 * 256 * b + 256 * c + d where "a.b.c.d" is ip address. To find location by ip address I'm using query:

{IpIntervals: $elemMatch: {"StartNum": {$lte: <<my_ip_num>>}, "EndNum": {$gte: <<my_ip_num>>}}}}

which works great but it takes about 270 ms, so I want to use some index with it. I've tried different indexes like:

{"IpIntervals.StartNum": 1, "IpIntervals.EndNum": 1}, {"IpIntervals.StartNum": -1, "IpIntervals.EndNum": 1}, {"IpIntervals.StartNum": 1, "IpIntervals.EndNum": -1}, {"IpIntervals.StartNum": 1}

But nothing seems to work: it is always BasicCursor and 270ms, which is not good. Any ideas about what index is appropriate in this situation? Thanx.

Sample data:

{ 
    "_id" : { "$oid" : "51015e8bd246e8e455ee027d" }, 
    "Name" : "SomeCity", 
    "Latitude" : 28.755787, 
    "Longitude" : 37.617634, 
    "IpIntervals" : [ 
         { "StartNum" : 2457360384, "EndNum" : 2457360639 }, 
         { "StartNum" : 2457361408, "EndNum" : 2457362431 }, 
         { "StartNum" : 2457364480, "EndNum" : 2457366527 }, 
         { "StartNum" : 2461648896, "EndNum" : 2461650943 }
     ] 
}
4

1 に答える 1

0

最後に、約 2 ミリ秒かかるクエリ
db.Cities.find({"IpIntervals.StartNum": {$lte: <<my_ip_num>>}}).limit(1).sort({"IpIntervals.StartNum": -1})とインデックスの回避策が見つかりました。{"IpIntervals.StartNum": 1}


IP 間隔が重複していないため、StartNums を注文して my_ip_num に最も近いものを取得できます。それでも、クエリに適したインデックスが見つかりません {IpIntervals: $elemMatch: {"StartNum": {$lte: <<my_ip_num>>}, "EndNum": {$gte: <<my_ip_num>>}}}}

于 2013-01-29T07:12:34.343 に答える