与传统 SQL Insert 语句相比,LINQ Insertonsubmit 非常慢

发布于 2024-10-14 17:40:57 字数 316 浏览 3 评论 0原文

我有一个很大的插入工作要执行,比如说 300000 个插入。

如果我采用传统方式,我只需编写一个包含 100 个 Insert 语句块的 SQL 字符串,并对数据库执行一个executeCommand(每条记录 100 条)。

每 3 秒左右大约可以插入 100 次。

当然,现在插入的值中存在单引号和 CrLf 的问题。因此,我没有编写代码来加倍单引号等,因为我很懒,所以我尝试使用 Linq InsertOnSubmit 和一个 context.SublitChanges 彼此 100 行。

这比传统方式多花费 20 倍!

为什么?

I have a large inserting job to perform, say 300000 Inserts.

If I do it the legacy way, I just write a SQL string with blocks of 100 Insert statements, and perform an executeCommand against the DB (each 100 records).

That lends to some 100 inserts per 3 seconds or so.

Now of course there are issue with single quotes and CrLf's within the inserted values. So rather than writing code to double the single quotes and so on, since I'm lazy I have a go with Linq InsertOnSubmit and one context.SublitChanges each other 100 rows.

And that take some 20x more times than the legacy way!!!

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-10-21 17:40:57

您没有使用正确的工具来完成这项工作。 LINQ-to-SQL 和大多数其他 ORM(至少是 Entity Framework 和 NHibernate)适用于 OLTP 场景,它们不适用于批量数据操作,并且在用于批量数据操作时执行速度会很慢。

您应该使用 SqlBulkCopy< /a>.

You're not using the right tool for the job. LINQ-to-SQL and most other ORMs (at least Entity Framework and NHibernate) are meant for OLTP scenarios, they are not meant for bulk data operations and will perform slowly when used for bulk data operations.

You should be using SqlBulkCopy.

南城旧梦 2024-10-21 17:40:57

我也遇到了同样的问题,InsertOnSubmit() 花了很长时间。

但是,使用 DataTableHelper 类(可从下面的链接下载)并仅更改 1 或 2 行代码,您就可以轻松地使用批量插入。

批量插入

例如:

const int RECORDS_TO_INSERT = 5000;

List<Product> recordsToBeInserted = new List<Product>();
using (NorthwindDataContext dc = new NorthwindDataContext())
{
    for (int n = 0; n < RECORDS_TO_INSERT; n++)
    {
        Product newProduct = new Product()
        {
            ProductName = "Product " + n.ToString(),
            UnitPrice = 3999,
            UnitsInStock = 2,
            UnitsOnOrder = 0,
            Discontinued = false
        };
        recordsToBeInserted.Add(newProduct);
    }
    // Insert this List<> of records into the [Products] table in our database, using a Bulk Insert
    DataTableHelper.BulkCopyToDatabase(recordsToBeInserted, "Products", dc);
}

希望这会有所帮助。

I had the same issues, with InsertOnSubmit() taking a long time.

However, using the DataTableHelper class (downloadable from the link below), and changing just 1 or 2 lines of your code, you can easily use a Bulk Insert instead.

Bulk-inserts

For example:

const int RECORDS_TO_INSERT = 5000;

List<Product> recordsToBeInserted = new List<Product>();
using (NorthwindDataContext dc = new NorthwindDataContext())
{
    for (int n = 0; n < RECORDS_TO_INSERT; n++)
    {
        Product newProduct = new Product()
        {
            ProductName = "Product " + n.ToString(),
            UnitPrice = 3999,
            UnitsInStock = 2,
            UnitsOnOrder = 0,
            Discontinued = false
        };
        recordsToBeInserted.Add(newProduct);
    }
    // Insert this List<> of records into the [Products] table in our database, using a Bulk Insert
    DataTableHelper.BulkCopyToDatabase(recordsToBeInserted, "Products", dc);
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文