简体   繁体   English

在Replicaset上批量插入MongoDB

[英]Bulk inserting to MongoDB on Replicaset

I am working on importing data form SQL to MongoDB 我正在努力将数据从SQL导入到MongoDB

I have a table in SQL with 3081583 records, I started inserting those to MongoDB in a foreach loop 我在SQL中有一个包含3081583条记录的表,我开始在foreach循环中将它们插入到MongoDB中

It started inserting at 05-06-2014 07:42:27 3081583 records and is still running. 它开始插入05-06-2014 07:42:27 3081583记录并仍在运行。

My connection to MongoDB using C# Driver 我使用C#Driver连接到MongoDB

private static MongoDatabase ConnectionOne(string dbName = "test")
{
    var connectionString = "mongodb://username$passwordx.x.x.x:27018"
        + "/admin?slaveOk=true";

    var client = new MongoClient(connectionString);

    var server = client.GetServer();

    var database = server.GetDatabase(dbName);

    return database;
}

Is there some thing I can do to improve the insert performance? 我有什么办法可以改善插入性能吗? Is this right way to do the bulk insert of 3081583 records or more? 这是批量插入3081583或更多记录的正确方法吗?

I am concerned about the performance, as this is a weekly process. 我担心性能,因为这是一个每周的过程。

  1. You could use bulk insert by passing array of objects to insert command. 您可以通过将对象数组传递给insert命令来使用批量插入。 You need configure ContinueOnError and define error handling strategy. 您需要配置ContinueOnError并定义错误处理策略。 But you should split whole set on chunks so that individual chuck doesn't exceed 16Mb. 但是你应该将整个组合分成块,以便单个吸盘不超过16Mb。

  2. Generally to improve insert performance you could try to limit amount of indexes on collection, use unacknowledged write concern {w:0, j:0} - ( you would have no confirmation about insert success in this case). 通常,为了提高插入性能,您可以尝试限制收集时的索引量,使用未确认的写入关注{w:0,j:0} - (在这种情况下,您将无法确认插入成功)。

Yes. 是。 You can insert in bulk: Bulk Inserts in MongoDB 您可以批量插入MongoDB中的批量插入

IEnumerable<WriteConcernResult> results = collection.InsertBatch(records)

That will cut on most of the round trips to the DB which should speed things up. 这将减少大部分到DB的往返行程,这应该加快速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM