【问题标题】:Best way of bulk inserting a list of records in DuckDB using DuckDB.NET使用 DuckDB.NET 在 DuckDB 中批量插入记录列表的最佳方法
【发布时间】:2022-08-23 02:33:13
【问题描述】:

语境

我正在使用很棒的 DuckDB.NET 库和 C# for 这个例子。我专门与 ADO.NET 提供程序合作。

问题

我的数据库包含下表:

CREATE TABLE tbl01 (
    Id INTEGER,
    TextListTest TEXT[],
    DateTest DATE
);

在程序中,每条记录都由一个类封装:

class Record
{
    public int Id { get; set; }
    public List<string> TextListTest { get; set; };
    public DateTime TextListTest { get; set; };
}

并附加到List&lt;Record&gt;。这个列表得到 非常大,所以我想避免 INSERT 语句中的每行开销 环形。 documentation 说如果我绝对必须使用 以这种方式插入,我还应该将它们包装在BEGIN TRANSACTIONCOMMIT 的调用中。我 真的不想在这里错过插入性能。我可以使用另一种方法吗 我正在使用的图书馆?

我在DuckDB.NET sample 中注意到 LowLevelBindingsSample() 方法,我可以使用准备好的语句,但我不确定这是否会带来任何性能优势。

有没有我错过的方法 - 也许是appender?如果有人可以提供使用 3 个特定数据的示例 上表中的类型将不胜感激(我无法弄清楚 LIST 列)。

using DuckDB.NET.Data;

namespace DuckTest;

class Record
{
    public int Id { get; set; }
    public List<string> TextListTest { get; set; }
    public DateTime DateTest { get; set; }
}

class Program
{
    public static void Main(string[] args)
    {
        // pretend this is a really big list!
        List<Record> recordList = new List<Record>
        {
            new Record { Id = 1, TextListTest = new List<string> { \"Ball\", \"Horse\" }, DateTest = new DateTime(1994, 12, 3) },
            new Record { Id = 2, TextListTest = new List<string> { \"Matthew\", \"Giorgi is cool!\" }, DateTest = new DateTime(1998, 11, 28) },
            new Record { Id = 3, TextListTest = new List<string> { \"Red\", \"Black\", \"Purple\" }, DateTest = new DateTime(1999, 9, 13) },
            new Record { Id = 4, TextListTest = new List<string> { \"Cat\" }, DateTest = new DateTime(1990, 2, 5) },
        };

        using (var duckDBConnection = new DuckDBConnection(\"Data Source=db01.duckdb\"))
        {
            duckDBConnection.Open();

            var command = duckDBConnection.CreateCommand();

            command.CommandText = \"CREATE TABLE tbl01 ( Id INTEGER, TextListTest TEXT[], DateTest DATE );\";
            var executeNonQuery = command.ExecuteNonQuery();

            // I could do this in a loop but there\'s probably a better way...
            command.CommandText = \"INSERT INTO tbl01 VALUES (1, [\'Ball\', \'Horse\'], \'1994-12-03\');\";
            executeNonQuery = command.ExecuteNonQuery();
        }
    }
}

如果需要,我愿意使用低级绑定库。

    标签: c# .net duckdb


    【解决方案1】:

    该库有support for appenders,因此这应该是导入数据的最快方式。

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 2023-03-26
      • 1970-01-01
      • 2015-05-05
      • 2016-05-19
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多