非常慢的MongoDB游标迭代

时间:2018-10-10 11:22:08

标签: c# mongodb iteration mongodb-.net-driver database-cursor

我面临的问题是游标在MongoDB上的集合上迭代速度非常慢。

设置:

MongoDB v4.0.1(1TB + zlib数据,WiredTiger)
MongoDB .NET驱动程序v2.7.0
C#应用程序和数据库都在同一台计算机上运行

C#代码

在代码中,我以标准方式使用 IAsyncCursor <TData> _mongoCursor

bool moved = await _mongoCursor.MoveNextAsync().ConfigureAwait(false);

我以这种方式测量了性能:

        Stopwatch sw = Stopwatch.StartNew();
        bool moved = await _mongoCursor.MoveNextAsync().ConfigureAwait(false);
        Console.WriteLine($"MoveNext took {sw.ElapsedMilliseconds}");

Mongo Log

2018-10-10T12:15:32.883+0200 D COMMAND  [conn16] run command MyDatabase.$cmd { getMore: 7026208311, collection: "MyCollection", batchSize: 10000, $db: "MyDatabase", lsid: { id: UUID("5b8c8a18-fbc9-4eb3-b447-8a46c0546878") } }
2018-10-10T12:15:33.023+0200 I COMMAND  [conn16] command MyDatabase.MyCollection command: getMore { getMore: 7026208311, collection: "MyCollection", batchSize: 10000, $db: "MyDatabase", lsid: { id: UUID("5b8c8a18-fbc9-4eb3-b447-8a46c0546878") } } originatingCommand: { find: "MyCollection", filter: { _id: { $gte: { DateTime: new Date(1377579604345), Ticks: 635131764043450000 }, $lte: { DateTime: new Date(253402300799999), Ticks: 3155378975999999999 } } }, batchSize: 10000, noCursorTimeout: true, readConcern: { level: "local" }, $db: "MyDatabase", $readPreference: { mode: "nearest" }, lsid: { id: UUID("5b8c8a18-fbc9-4eb3-b447-8a46c0546878") } } planSummary: IXSCAN { _id: 1 } cursorid:7026208311 keysExamined:2780 docsExamined:2780 numYields:21 nreturned:2780 reslen:16776285 locks:{ Global: { acquireCount: { r: 22 } }, Database: { acquireCount: { r: 22 } }, Collection: { acquireCount: { r: 22 } } } protocol:op_msg 139ms

问题

每次调用MoveNext()平均需要1500毫秒,即很多。从MongoDB日志中,您可以看到在数据库上返回新批处理平均需要140毫秒。

问题

为什么查询这么慢?是从BSON到类的缓慢反序列化/映射吗?
我怎样才能使其更快?有任何想法吗?

0 个答案:

没有答案