我正在使用MongoDB来实现一个POC(从SAP HANA迁移到MongoDB),因为我需要在查询中支持join,range和group by。这是我在RDBMS中的查询,我试图在MongoDB中复制它:
SELECT *
FROM BUSINESSPOINTS_USA A
INNER JOIN (
SELECT max(PKEY) AS PKEY
FROM BUSINESSPOINTS_USA
WHERE longitude BETWEEN % LL_LONG %
AND % UR_LONG %
AND latitude BETWEEN % LL_LAT %
AND % UR_LAT %
AND % WHERE_CLAUSE %
GROUP BY LEFT(QUADKEY, % QUAD_TREE_LEVEL %)
) AS B ON A.PKEY = B.PKEY
但是group by在MongoDB中响应非常慢,这里是来自group by查询的部分:
db.getCollection('businesspoints').aggregate([{
$match: {position : {$within: {$box: [[-73,-73],[41,41]]}}}},
{$group:{_id: {$substr:["$quadkey",0,5]}, mapinfoid:{$max: "$_id"}
}
}]);
这是执行计划:
{
"waitedMS" : NumberLong(0),
"stages" : [
{
"$cursor" : {
"query" : {
"position" : {
"$within" : {
"$box" : [
[
-73.0,
-73.0
],
[
41.0,
41.0
]
]
}
}
},
"fields" : {
"quadkey" : 1,
"_id" : 1
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "spatialDB.businesspoints",
"indexFilterSet" : false,
"parsedQuery" : {
"position" : {
"$within" : {
"$box" : [
[
-73.0,
-73.0
],
[
41.0,
41.0
]
]
}
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"position" : {
"$within" : {
"$box" : [
[
-73.0,
-73.0
],
[
41.0,
41.0
]
]
}
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"position" : "2d"
},
"indexName" : "position_2d",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"position" : [
"[BinData(128, 30F0000000000000), BinData(128, 30FFFFFFFFFFFFFF)]",
"[BinData(128, 3180000000000000), BinData(128, 31BFFFFFFFFFFFFF)]",
"[BinData(128, 31C0000000000000), BinData(128, 31FFFFFFFFFFFFFF)]",
"[BinData(128, 3200000000000000), BinData(128, 32FFFFFFFFFFFFFF)]",
"[BinData(128, 3300000000000000), BinData(128, 33FFFFFFFFFFFFFF)]",
"[BinData(128, 3400000000000000), BinData(128, 37FFFFFFFFFFFFFF)]",
"[BinData(128, 3800000000000000), BinData(128, 3BFFFFFFFFFFFFFF)]",
"[BinData(128, 3C00000000000000), BinData(128, 3FFFFFFFFFFFFFFF)]",
"[BinData(128, 6000000000000000), BinData(128, 63FFFFFFFFFFFFFF)]",
"[BinData(128, 6800000000000000), BinData(128, 6BFFFFFFFFFFFFFF)]",
"[BinData(128, 9000000000000000), BinData(128, 93FFFFFFFFFFFFFF)]",
"[BinData(128, 9400000000000000), BinData(128, 97FFFFFFFFFFFFFF)]",
"[BinData(128, C000000000000000), BinData(128, C0FFFFFFFFFFFFFF)]",
"[BinData(128, C100000000000000), BinData(128, C1FFFFFFFFFFFFFF)]",
"[BinData(128, C200000000000000), BinData(128, C2FFFFFFFFFFFFFF)]",
"[BinData(128, C300000000000000), BinData(128, C3FFFFFFFFFFFFFF)]"
]
}
}
},
"rejectedPlans" : []
}
}
},
{
"$group" : {
"_id" : {
"$substr" : [
"$quadkey",
{
"$const" : 0.0
},
{
"$const" : 5.0
}
]
},
"mapinfoid" : {
"$max" : "$_id"
}
}
}
],
"ok" : 1.0
}
有没有办法将我的表分成多个集合来加速分组。