简体   繁体   English

从STDIN复制以从C#中的内存数据集中插入

[英]COPY FROM STDIN to insert from a in memory dataset in C#

I am new to Npgsql. 我是Npgsql的新手。 I am trying to make use of this for a windows app, and our requirement is to reduce the time taken to insert 2 to 3 million rows into a local/embedded DB file on the client, currently it takes 3 minutes to insert 2 million rows in SQLite if we do it in batches of 60000 per transaction. 我正在尝试将其用于Windows应用程序,我们的要求是减少将2到3百万行插入到客户端上的本地/嵌入式DB文件中所花费的时间,当前插入2百万行需要3分钟在SQLite中,如果我们每笔交易分批处理6万次。 I have tried this using NpgsqlDataAdapter (code snippet pasted below), but it takes around 20 seconds to insert a batch of 60000 rows, where as SQLite takes 2 to 3 seconds for the same. 我已经使用NpgsqlDataAdapter(下面粘贴的代码段)进行了尝试,但是插入一批60000行大约需要20秒,而SQLite则需要2到3秒。 Can anyone help me to use COPY FROM STDIN to insert data from an in memory data structure (not from a text/csv file). 谁能帮助我使用COPY FROM STDIN插入内存数据结构中的数据(而不是text / csv文件中的数据)。

var connection = new NpgsqlConnection(connStr);
connection.Open();

NpgsqlCommand cmd = new NpgsqlCommand() { CommandType = "INSERT INTO Logs (c1,c2,c3,c4) VALUES (@c1,@c2,@c3,@c4)" };

NpgsqlDataAdapter da = new NpgsqlDataAdapter { InsertCommand = cmd };
if (da.InsertCommand != null)
   da.InsertCommand.Connection = connection;


NpgsqlParameter c1= da.InsertCommand.Parameters.Add("@c1", NpgsqlTypes.NpgsqlDbType.Integer);
c1.SourceColumn = "c1";
c1.SourceVersion = DataRowVersion.Current;

NpgsqlParameter c2= da.InsertCommand.Parameters.Add("@c2", NpgsqlTypes.NpgsqlDbType.Integer);
c2.SourceColumn = "c2";
c2.SourceVersion = DataRowVersion.Current;

NpgsqlParameter c3= da.InsertCommand.Parameters.Add("@c3", NpgsqlTypes.NpgsqlDbType.Integer);
c3.SourceColumn = "c3";
c3.SourceVersion = DataRowVersion.Current;

NpgsqlParameter c4= da.InsertCommand.Parameters.Add("@c4", NpgsqlTypes.NpgsqlDbType.Date);
c4.SourceColumn = "c4";
c4.SourceVersion = DataRowVersion.Current;

da.InsertCommand.UpdatedRowSource = UpdateRowSource.None;
da.InsertCommand.Prepare();

NpgsqlTransaction trans = connection.BeginTransaction();

DataTable logEventsTable =
   this.ConvertToLogEventsDataTable(logEvents, areNewLogs);
logEventsTable.Locale = CultureInfo.CurrentCulture;

insertCount = da.Update(logEventsTable);

trans.Commit();

trans.Dispose();
da.Dispose();
logEventsTable.Clear();
connection.Close();

I found some links which explains on how to implement bulk insertion using NpgsqlCopyIn and NpgsqlCopySerializer. 我找到了一些链接,这些链接解释了如何使用NpgsqlCopyIn和NpgsqlCopySerializer实现批量插入。 This definitely is better than using NpgsqlDataAdapter. 这绝对比使用NpgsqlDataAdapter好。 I got a 100% improvement in the time taken as compared to insertion using transactions. 与使用事务插入相比,我的时间节省了100%。

http://devcrackers.blogspot.in/2013/06/npgsqlcopyin-and-npgsqlcopyserializer.html http://devcrackers.blogspot.in/2013/06/npgsqlcopyin-and-npgsqlcopyserializer.html

https://gist.github.com/smoothdeveloper/0c5c0f6bbdad8b3b18af https://gist.github.com/smoothdeveloper/0c5c0f6bbdad8b3b18af

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

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