550万行/文档的MongoDB性能与PostgreSQL的对比


10

有人可以帮我比较这些查询,并解释为什么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

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

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}
   }
])

PostgreSQL表和MongoDB集合都在datetime:1和locomotive_id:1上建立索引。

这些查询正在具有2TB混合驱动器和16GB内存的iMac上进行测试。在具有8GB内存和256GB SSD的Windows 7计算机上,我收到了可比的结果。

谢谢!

**更新:我的问题发布后,我正在发布EXPLAIN(气泡,分析)结果

"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"

**更新: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
}

1
对于PostgreSQL查询,EXPLAIN (BUFFERS, ANALYZE)请显示输出。另外,PostgreSQL版本。(我已投票决定将其移至dba.SE)
Craig Ringer


2
尽管很难逃避NoSQL的炒作,但传统的RDBMS每天都有更好的,更成熟的聚合。NoSQL数据库针对主键索引和按键检索进行了优化,而不是针对此类查询。
Alexandros

我可能遗漏了一些细节。每个文档中有200多个字段。这是从PostgreSQL数据库直接导入的。许多字段值为空。我记得MongoDB并不是特别喜欢空值。我再次导入了相关数据的<20个字段,并且查询性能更好。我在具有8GB内存和较慢的HD的计算机上获得<3000ms。我将很快在功能更强大的计算机上开始新的测试。
Mike A

Mongodb索引的性能{datetime: 1, prevdatetime: 1}应优于当前索引,因为mongodb会过滤datetime和prevdatetime。这样可以减少需要扫描的文档数量。
rubish

Answers:


8

PostgreSQL在这里所做的就是对位图堆进行扫描bpkdmp_datetime_ix以查找可能包含匹配行的块,然后对这些块进行堆扫描以在中查找匹配的行bpkdmp。然后,使用分组键的哈希将行分组为哈希存储桶,对每个存储桶求和,并对结果进行排序。这是一个简单的基本查询计划-如果您对它进行大量测试,它可能会表现更好work_mem,但也不一定。

该查询中的任何地方都没有并行性。所有这些都将在一个内核上发生。

我只能假设MongoDB使用的是效率较低的方法,或者没有从适当的索引中受益。您需要显示explainMongoDB查询的,以获取有用的注释;见cursor.explain

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.