简体   繁体   English

从数据表填充SQLite表的最快方法是什么

[英]what is the fastest way to populate SQLite table from a datatable

What is the fastest way to populate a SQLite database from a DataTable in C# .net 2. 从C#.net 2中的DataTable填充SQLite数据库的最快方法是什么?

Currently I am building insert statments for each row in the table. 目前我正在为表中的每一行构建插入语句。 I have tried a dataadaptor but the speed didn't seem to be any faster. 我尝试过dataadaptor,但速度似乎没有更快。 It currently takes 5 min to loop through 20,000 rows and write them to the database. 目前需要5分钟才能遍历20,000行并将其写入数据库。 Any sugestions? 任何sugestions?

solution: 解:

I found that surounding blocks of insert statments with BEGIN...COMMIT worked for me with a remarkable speed improvement: 我发现使用BEGIN ... COMMIT的插入声明的区别块为我带来了显着的速度提升:

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

my insert statements were around 500 byte each, so I limited the number of statements to 100 per transaction. 我的insert语句大约每个500字节,因此我将每个事务的语句数量限制为100。

See this FAQ entry from the SQLite website: 请参阅SQLite网站上的此FAQ条目:

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

By default, each INSERT statement is its own transaction. 默认情况下,每个INSERT语句都是自己的事务。 But if you surround multiple INSERT statements with BEGIN...COMMIT then all the inserts are grouped into a single transaction. 但是如果用BEGIN ... COMMIT包围多个INSERT语句,则所有插入都被分组到一个事务中。 The time needed to commit the transaction is amortized over all the enclosed insert statements and so the time per insert statement is greatly reduced. 提交事务所需的时间在所有随附的insert语句中分摊,因此每个insert语句的时间大大减少。

See this thread . 看到这个帖子

The best way is to use ExecuteNonQuery() , which commits all the inserts at once, and doesn't have to keep allocating strings. 最好的方法是使用ExecuteNonQuery() ,它一次提交所有插入,而不必继续分配字符串。 20,000 rows should take much less than a minute. 20,000行应该花费不到一分钟。

Consider to use the SqLiteDataAdapter. 考虑使用SqLiteDataAdapter。 (I still have to use vb.net but following example should be ease to translate. Or have a look at the original source: http://stackoverflow.com/a/2671511 ) (我仍然需要使用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

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

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