MongoDB takes long time to return geolocation query.

124 views
Skip to first unread message

aditya

unread,
Jan 6, 2013, 12:06:07 PM1/6/13
to mongod...@googlegroups.com
I have events collection in database on which I have applied following compound index

{ "coordinates":"2d","start_time":1 }

The coordinates are regular latitude-longitude and start_time is unix time. I have about 4,00,000 events in collection. Now I throw following command to find nearby events

db.events.find({ "coordinates": { "$near": [ 77.22445, 28.63576 ] }, "start_time": { "$gt": 1357491649 } });

This query sometimes returns result after 20 seconds!!!

Please help me how to optimize this query? 

aditya

unread,
Jan 6, 2013, 12:12:26 PM1/6/13
to mongod...@googlegroups.com
Even when I throw 

db.events.find({ "coordinates": { "$near": [ 77.22445, 28.63576 ] }});

query which is without start_time, it takes more than 1 second. 
Please help me. This is hindering my site speed.

Justin Case

unread,
Jan 6, 2013, 12:20:13 PM1/6/13
to mongod...@googlegroups.com
Could you post the .explain() output?

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

aditya

unread,
Jan 6, 2013, 12:28:13 PM1/6/13
to mongod...@googlegroups.com
My log shows this 

Sun Jan  6 22:50:17 [conn8] command cityevent.$cmd command: { count: "events", query: { start_time: { $gt: 1357492788 }, coordinates: { $nearSphere: [ 72.61667, 23.03333 ] } } } ntoreturn:1 keyUpdates:0 locks(micros) r:29019506 reslen:48 29019ms

explain output is 

{
"cursor" : "GeoSearchCursor",
"isMultiKey" : false,
"n" : 100,
"nscannedObjects" : 100,
"nscanned" : 100,
"nscannedObjectsAllPlans" : 100,
"nscannedAllPlans" : 100,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 1,
"indexBounds" : {
},
"server" : "aditya-laptop:27017"
}

Justin Case

unread,
Jan 6, 2013, 12:51:27 PM1/6/13
to mongod...@googlegroups.com
What is the query you ran explain on? According to the output it completed in 1ms.

aditya

unread,
Jan 6, 2013, 1:14:02 PM1/6/13
to mongod...@googlegroups.com
That is another problem. I don't know why it gives output 1ms when I can even count time in my wrist watch. You can also see the log of mongodb.log file for same query.

Sun Jan  6 22:57:15 [conn10] query cityevent.events query: { query: { start_time: { $gt: 1357492788.0 }, coordinates: { $nearSphere: [ 72.61667, 23.03333 ] } }, $explain: true } ntoreturn:0 ntoskip:0 nscanned:100 keyUpdates:0 locks(micros) r:46155752 nreturned:1 reslen:389 46155ms

Hari Khalsa

unread,
Feb 1, 2013, 3:11:29 PM2/1/13
to mongod...@googlegroups.com
If you don't need the points sorted by distance, consider bounding the query by doing a $center or $centerSphere, or if you do need them sorted, try adding a maxDistance to the near?  $near might have to look quite far away to find points.

If your points are meant to be latitude and longitude, there is new geospatial functionality coming out in 2.4 which should be much faster for these types of queries.

aditya

unread,
Feb 3, 2013, 1:17:47 PM2/3/13
to mongod...@googlegroups.com
I always use maxdistance there. Anyway, will wait till 2.4 is released. Thanks :-)
Reply all
Reply to author
Forward
0 new messages