如何从datatable快速填充SQLite表格?

5

如何在C# .net 2中以最快的方式从DataTable填充SQLite数据库?

目前,我正在为表中的每一行构建插入语句。我尝试了一个数据适配器,但速度似乎并没有更快。目前需要5分钟循环遍历20000行并将其写入数据库。有什么建议吗?

解决方案:

我发现使用BEGIN...COMMIT包围插入语句的块对我非常有效,速度有显着提高:

BEGIN;
INSERT INTO friends (name1,name2) VALUES  ('john','smith');
INSERT INTO friends (name1,name2) VALUES  ('jane','doe');
COMMIT;

我的插入语句每个大约有500字节,因此我将每个事务的语句数量限制为100个。

3个回答

3

请参考SQLite网站上的常见问题解答:

http://www.sqlite.org/faq.html#q19

默认情况下,每个INSERT语句都是一个事务。但如果您将多个INSERT语句用BEGIN...COMMIT包围起来,则所有插入操作将被分组到单个事务中。提交事务所需的时间将分摊到所有封闭的插入语句中,因此每个插入语句的时间大大缩短。


有趣。我想知道要用 BEGIN ... COMMIT 块包围多少语句。 - fishhead
你可以使用7200 RPM硬盘每秒执行六十个事务。将你的写入速度(比如5MB/s)除以60,得到每个事务的字节数,然后再将其除以记录大小,得到每个事务的插入数量。 - Robert Harvey

1

请查看此线程

最好的方法是使用ExecuteNonQuery(),它会一次性提交所有插入操作,而且不需要不断分配字符串。20000行数据应该不到一分钟就能完成。


0

考虑使用SqLiteDataAdapter。(我仍然需要使用vb.net,但以下示例应该很容易翻译。或者查看原始来源:http://stackoverflow.com/a/2671511

Private Sub FillDatabaseTableWithDataTable(dataTable As DataTable)
            ' inspired by http://stackoverflow.com/a/2671511

            ' Query the destination database
            Dim query As String = $"SELECT * FROM `{dataTable.TableName}`"

            Using adapter As New SQLiteDataAdapter(query, Connection)

                Using commandBuilder = New SQLiteCommandBuilder(adapter)

                    Connection.Open()

                    commandBuilder.QuotePrefix = "["
                    commandBuilder.QuoteSuffix = "]"
                    commandBuilder.GetInsertCommand()

                    'Create an empty "destination" table for synchronization
                    ' with SqLite "source" database
                    Dim destinationTable As New DataTable()

                    'load data from SqLite "source" database to destination table, e.g.
                    'column headers
                    adapter.Fill(destinationTable)

                    'adapt "destination" table: fill data
                    For Each row As DataRow In dataTable.Rows
                        Dim destinationRow As DataRow = destinationTable.NewRow()
                        destinationRow.ItemArray = row.ItemArray
                        destinationTable.Rows.Add(destinationRow)
                    Next

                    'Update SqLite source table
                    'the Update has To be wrapped In a transaction
                    'Otherwise, SQLite would implicitly create a transaction
                    'for each line. That would slow down the writing process.
                    Using transaction = Connection.BeginTransaction()
                        adapter.Update(destinationTable)
                        transaction.Commit()
                    End Using

                    Connection.Close()
                End Using
            End Using
        End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接