mongodb$near查询速度慢
一个mongodb集合mongodb$near查询速度慢,mongodb,pymongo,Mongodb,Pymongo,一个mongodb集合 { "_id" : ObjectId("574bbae4d009b5364abaebe5"), "cityid" : 406, "location" : { "type" : "Point", "coordinates" : [ 118.602355, 24.89083 ] }, "shopid" : "a" } 大约有50000行 和
{
"_id" : ObjectId("574bbae4d009b5364abaebe5"),
"cityid" : 406,
"location" : {
"type" : "Point",
"coordinates" : [
118.602355,
24.89083
]
},
"shopid" : "a"
}
大约有50000行
和索引:
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "pingan-test.shop_actinfo_collection_0530"
},
{
"v" : 1,
"key" : {
"location" : "2dsphere"
},
"name" : "location_2dsphere",
"ns" : "pingan-test.shop_actinfo_collection_0530",
"2dsphereIndexVersion" : 3
},
{
"v" : 1,
"key" : {
"shopid" : 1,
"cityid" : 1
},
"name" : "shopid_1_cityid_1",
"ns" : "pingan-test.shop_actinfo_collection_0530"
}
]
我对该集合的查询方式如下:
body = {'cityid': 2, 'location': {'$near': {'$geometry': {'type': 'Point', 'coordinates': [122.0, 31.0]}}}, 'shopid': {'$in': ['a','b']}}
results = collection.find(body, {'shopid': 1, '_id':0},).batch_size(20).limit(20)
shops = list(results)
问题是它大约运行400毫秒。但如果我们不在乎位置,只需要30分钟
为什么以及如何修复?请。您有一个关于shopid和cityid的索引,但您搜索的是cityid。由于索引首先按shopid排序,因此不能用于按cityid进行搜索。如果您将索引更改为cityid:1,shopid:1,那么您将看到性能改进,因为您的查询将能够使用该索引进行搜索。毕竟,我得到了它。 我只是创建了一个索引,指向
cityid:1,shopid:1,“location”:“2dsphere”
,然后是世界和平
再次感谢。是的,在我的查询中,“shopid”、“cityid”和“location”都是必需的。所以我索引到cityid:1,shopid:1。您可以在查询中看到“'shopid':{'$in':['a','b']}”。