2014-11-20 57 views
2

我有一個MongoDB的鳴叫與兩個集合,一個在匹茲堡(tweet_pgh),3M〜鳴叫,一個在克里夫蘭(tweet_cleveland),約160k鳴叫10分鐘。在每個城市,我一直試圖運行一個小的$ geoWithin查詢,如下所示:MongoDB的簡單geoWithin查詢需要在3M文件

db.tweet_cleveland.find({coordinates: {$geoWithin: {$geometry: 
    {type : "Polygon", coordinates : [ [ [ -81.6826, 41.5041 ], [ -81.6726, 41.5041 ], [ -81.6726, 41.4941 ], [ -81.6826, 41.5041 ] ] ] }} 
}}).explain() 

沒問題。在251ms結束,掃描13k文件,返回〜2k文件。

{ 
    "cursor" : "S2Cursor", 
    "isMultiKey" : true, 
    "n" : 2070, 
    "nscannedObjects" : 2070, 
    "nscanned" : 13874, 
    "nscannedObjectsAllPlans" : 2070, 
    "nscannedAllPlans" : 13874, 
    "scanAndOrder" : false, 
    "indexOnly" : false, 
    "nYields" : 4, 
    "nChunkSkips" : 0, 
    "millis" : 251, 
    "indexBounds" : { 

    }, 
    "nscanned" : 13874, 
    "matchTested" : NumberLong(11804), 
    "geoTested" : NumberLong(11804), 
    "cellsInCover" : NumberLong(2), 
    "server" : ... 
} 

所以我嘗試做相同的匹茲堡,其具有約20倍的鳴叫,並假設它會採取20倍〜長,所以5000毫秒。

db.tweet_pgh.find({coordinates: {$geoWithin: {$geometry: 
    {type : "Polygon", coordinates : [ [ [ -79.940, 40.466 ], [ -79.940, 40.465 ], [ -79.943, 40.465 ], [ -79.940, 40.466 ] ] ] }} 
}}).explain() 

但它大約需要10分鐘。 (2000倍長。)

{ 
    "cursor" : "S2Cursor", 
    "isMultiKey" : true, 
    "n" : 2129, 
    "nscannedObjects" : 2129, 
    "nscanned" : 284093, 
    "nscannedObjectsAllPlans" : 2129, 
    "nscannedAllPlans" : 284093, 
    "scanAndOrder" : false, 
    "indexOnly" : false, 
    "nYields" : 3, 
    "nChunkSkips" : 0, 
    "millis" : 586680, 
    "indexBounds" : { 

    }, 
    "nscanned" : 284093, 
    "matchTested" : NumberLong(281964), 
    "geoTested" : NumberLong(281964), 
    "cellsInCover" : NumberLong(1), 
    "server": ... 
} 

事情我已經檢查:指數(他們都有一個2dsphere指數,這是兩個被使用(光標說:「S2Cursor」)RAM(不是一個問題。 ,大約3GB免費)另一個類似的集合(tweet_sf,它有大約2M的推文,就像tweet_pgh一樣緩慢)同樣大小的查詢(大小和返回的記錄數量)

任何想法,爲什麼它很慢使用geoWithin或2dsphere索引時是否會出現二次或更糟的情況? 謝謝!

編輯:經過一段時間的愚弄後,我可以告訴是,這只是一個非常耗時的操作。沒有我顯然做錯了,只是這很難。我現在正試圖遷移到PostgreSQL + PostGIS,看起來它會更快,但還沒有真正的數字。

回答

0

所以,我回答我的問題,即使我仍然不知道爲什麼它很慢。但我知道我的真正的問題的答案,即「我現在該做什麼?」

答案是,遷移到Postgres/PostGIS。

做了一些基準測試:http://ilessendata.blogspot.com/2015/04/mongodb-vs-postgresql-for-geo-queries.html如果你想知道更多,但總之,它看起來像地理學「找到這個盒子裏的所有東西」查詢是Postgres的1-2個數量級更快(如果你索引和集羣座標)。

祝你好運,未來的StackOverflowers絆倒了這個無奈的金塊。

+0

需要注意的一件事:在您的查詢中沒有使用索引。 – 2015-04-10 23:19:34

+0

你的意思是上面的查詢?你確定?我認爲S2Cursor意味着它使用2dsphere索引。例如[本](http://stackoverflow.com/questions/19969405/why-i-make-a-2dsphere-index-but-when-i-query-it-shows-s2cursor)。 – 2015-04-12 16:19:38