Can someone help me compare these queries and explain why the PostgreSQL
query executes in just under 2000ms and the MongoDB aggregate query takes
almost 9000ms and sometimes as high as 130K ms?
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
*PostgreSQL query*
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
*MongoDB Query*
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}
])
Both the PostgreSQL table and the MongoDB collection are indexed on
datetime : 1 and locomotive_id : 1
These queries are being testing on an iMac with a 2TB hybrid drive and 16GB
of memory. I have received comparable results on a Windows 7 machine with
8GB of memory and a 256GB SSD.
Thanks!
*Pogresql EXPLAIN (ANALYZE, BUFFERS)*
"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"
query executes in just under 2000ms and the MongoDB aggregate query takes
almost 9000ms and sometimes as high as 130K ms?
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
*PostgreSQL query*
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
*MongoDB Query*
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}
])
Both the PostgreSQL table and the MongoDB collection are indexed on
datetime : 1 and locomotive_id : 1
These queries are being testing on an iMac with a 2TB hybrid drive and 16GB
of memory. I have received comparable results on a Windows 7 machine with
8GB of memory and a 256GB SSD.
Thanks!
*Pogresql EXPLAIN (ANALYZE, BUFFERS)*
"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"