Mik*_*e A 12 postgresql performance mongodb
有人可以帮我比较这些查询并解释为什么 PostgreSQL 查询的执行时间不到 2000 毫秒,而 MongoDB 聚合查询需要将近 9000 毫秒,有时甚至高达 130K 毫秒吗?
PostgreSQL 9.3.2 on x86_64-apple-darwin, compiled by i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00), 64-bit
Run Code Online (Sandbox Code Playgroud)
PostgreSQL 查询
SELECT locomotive_id,
SUM(date_trunc('second', datetime) - date_trunc('second', prevDatetime)) AS utilization_time
FROM bpkdmp
WHERE datetime >= '2013-7-26 00:00:00.0000'
AND datetime <= '2013-7-26 23:59:59.9999'
GROUP BY locomotive_id
order by locomotive_id
Run Code Online (Sandbox Code Playgroud)
MongoDB 查询
db.bpkdmp.aggregate([
{
$match : {
datetime : { $gte : new Date(2013,6,26, 0, 0, 0, 0), $lt : new Date(2013,6,26, 23, 59, 59, 9999) }
}
},
{
$project: {
locomotive_id : "$locomotive_id",
loco_time : { $subtract : ["$datetime", "$prevdatetime"] },
}
},
{
$group : {
_id : "$locomotive_id",
utilization_time : { $sum : "$loco_time" }
}
},
{
$sort : {_id : 1}
}
])
Run Code Online (Sandbox Code Playgroud)
PostgreSQL 表和 MongoDB 集合都在 datetime : 1 和 locomotive_id : 1 上建立索引
这些查询正在带有 2TB 混合驱动器和 16GB 内存的 iMac 上进行测试。我在具有 8GB 内存和 256GB SSD 的 Windows 7 机器上收到了类似的结果。
谢谢!
** 更新:我在发布我的问题后发布了 EXPLAIN (BUFFERS, ANALYZE) 结果
"Sort (cost=146036.84..146036.88 rows=19 width=24) (actual time=2182.443..2182.457 rows=152 loops=1)"
" Sort Key: locomotive_id"
" Sort Method: quicksort Memory: 36kB"
" Buffers: shared hit=13095"
" -> HashAggregate (cost=146036.24..146036.43 rows=19 width=24) (actual time=2182.144..2182.360 rows=152 loops=1)"
" Buffers: shared hit=13095"
" -> Bitmap Heap Scan on bpkdmp (cost=12393.84..138736.97 rows=583942 width=24) (actual time=130.409..241.087 rows=559529 loops=1)"
" Recheck Cond: ((datetime >= '2013-07-26 00:00:00'::timestamp without time zone) AND (datetime <= '2013-07-26 23:59:59.9999'::timestamp without time zone))"
" Buffers: shared hit=13095"
" -> Bitmap Index Scan on bpkdmp_datetime_ix (cost=0.00..12247.85 rows=583942 width=0) (actual time=127.707..127.707 rows=559529 loops=1)"
" Index Cond: ((datetime >= '2013-07-26 00:00:00'::timestamp without time zone) AND (datetime <= '2013-07-26 23:59:59.9999'::timestamp without time zone))"
" Buffers: shared hit=1531"
"Total runtime: 2182.620 ms"
Run Code Online (Sandbox Code Playgroud)
** 更新:Mongo 解释:
从MongoDB解释
{
"serverPipeline" : [
{
"query" : {
"datetime" : {
"$gte" : ISODate("2013-07-26T04:00:00Z"),
"$lt" : ISODate("2013-07-27T04:00:08.999Z")
}
},
"projection" : {
"datetime" : 1,
"locomotive_id" : 1,
"prevdatetime" : 1,
"_id" : 1
},
"cursor" : {
"cursor" : "BtreeCursor datetime_1",
"isMultiKey" : false,
"n" : 559572,
"nscannedObjects" : 559572,
"nscanned" : 559572,
"nscannedObjectsAllPlans" : 559572,
"nscannedAllPlans" : 559572,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 1,
"nChunkSkips" : 0,
"millis" : 988,
"indexBounds" : {
"datetime" : [
[
ISODate("2013-07-26T04:00:00Z"),
ISODate("2013-07-27T04:00:08.999Z")
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor datetime_1",
"n" : 559572,
"nscannedObjects" : 559572,
"nscanned" : 559572,
"indexBounds" : {
"datetime" : [
[
ISODate("2013-07-26T04:00:00Z"),
ISODate("2013-07-27T04:00:08.999Z")
]
]
}
}
],
"oldPlan" : {
"cursor" : "BtreeCursor datetime_1",
"indexBounds" : {
"datetime" : [
[
ISODate("2013-07-26T04:00:00Z"),
ISODate("2013-07-27T04:00:08.999Z")
]
]
}
},
"server" : "Michaels-iMac.local:27017"
}
},
{
"$project" : {
"locomotive_id" : "$locomotive_id",
"loco_time" : {
"$subtract" : [
"$datetime",
"$prevdatetime"
]
}
}
},
{
"$group" : {
"_id" : "$locomotive_id",
"utilization_time" : {
"$sum" : "$loco_time"
}
}
},
{
"$sort" : {
"sortKey" : {
"_id" : 1
}
}
}
],
"ok" : 1
}
Run Code Online (Sandbox Code Playgroud)
PostgreSQL 在这里所做的只是位图堆扫描bpkdmp_datetime_ix
以查找可能包含匹配行的块,然后对这些块进行堆扫描以查找bpkdmp
. 然后使用分组键的散列将行分组到散列桶中,对每个桶求和,并对结果进行排序。这是一个简单、基本的查询计划——如果你投入大量的work_mem
时间,它可能会表现得更好,但也可能不会。
该查询中的任何地方也没有并行性;这一切都将发生在一个核心上。
我只能假设 MongoDB 使用的是效率较低的方法,或者没有从适当的索引中受益。您需要explain
为 MongoDB 查询显示有用的注释,以便有可能;见cursor.explain
。