Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从STDIN复制到从C中的内存数据集插入#_C#_.net_Postgresql_Bulkinsert_Npgsql - Fatal编程技术网

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

C# 从STDIN复制到从C中的内存数据集插入#,c#,.net,postgresql,bulkinsert,npgsql,C#,.net,Postgresql,Bulkinsert,Npgsql,我是Npgsql的新手。我正在尝试将其用于windows应用程序,我们的要求是减少在客户端的本地/嵌入式DB文件中插入200万到300万行所需的时间,目前,如果我们在每个事务中批量插入60000行,在SQLite中插入200万行需要3分钟。我曾使用NpgsqlDataAdapter(下面粘贴的代码片段)尝试过这一点,但插入一批60000行大约需要20秒,而作为SQLite插入同样的行需要2到3秒。有谁能帮助我使用COPY FROM STDIN从内存中的数据结构(而不是文本/csv文件)插入数据

我是Npgsql的新手。我正在尝试将其用于windows应用程序,我们的要求是减少在客户端的本地/嵌入式DB文件中插入200万到300万行所需的时间,目前,如果我们在每个事务中批量插入60000行,在SQLite中插入200万行需要3分钟。我曾使用NpgsqlDataAdapter(下面粘贴的代码片段)尝试过这一点,但插入一批60000行大约需要20秒,而作为SQLite插入同样的行需要2到3秒。有谁能帮助我使用COPY FROM STDIN从内存中的数据结构(而不是文本/csv文件)插入数据


我找到了一些解释如何使用NpgsqlCopyIn和NpgsqlCopySerializer实现大容量插入的链接。这肯定比使用NpgsqlDataAdapter要好。与使用事务插入相比,我所用的时间提高了100%


您是否为每次插入打开新交易和新交易?还是我误读了您的代码?我正在为每个数据块打开一个新事务并关闭它。datatable logEventsTable平均包含60000行。如果可能,请尝试保留连接。如果可能的话,还可以使用两个平行插入辅助线。我真的不希望SQLite和Pg之间有像你们在这里看到的那个样大的差异——我可以看到2倍的差异,但60倍?有点奇怪,谢谢克雷格。我使用了,通过使用序列化和并行插入,我可以看到30%的改进。
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();