使用 Linq to sql 从 csv 批量插入到数据库

发布于 2024-11-06 02:05:53 字数 257 浏览 6 评论 0原文

我已经实现了一个应用程序,它从 csv 文件获取数据并插入到 Sql DB 中,我正在使用 Linq to sql。我还要求跳过那些正在进行验证的记录, 为了实现这一点,我使用了一个循环,并在循环内部调用submitchnages()。

问题:此应用程序适用于较少数量的记录(<100),但实际上我会获取更多 3 - 4 个记录的 csv 文件。我只是简单地针对这些大文件运行我的应用程序,结果应用程序花费了很长时间(5 -6 小时)。

请建议任何更好的方法。

I have implemented an application which taking the data from csv file and inserting into Sql DB, i am using Linq to sql. also i have requirement of skiping those records whih are having some validation,
to achive this i used a loop and inside the loop, calling submitchnages().

problem: this app is working for less number of records(<100) but in reality ill be getting csv file of more 3 - 4 lacs of record. I just simply ran my app against these big files, in result the app is taking long time (5 -6 hrs).

Please suggest any better approach.

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

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

发布评论

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

评论(3

愛放△進行李 2024-11-13 02:05:54

Linq-to-SQL 非常适合从数据库中获取数据,或者用于验证和一次少量插入/更新。但是对于您正在做的事情(ETL),听起来您需要查看 SqlBulkCopy 对象。继续使用 L2S 对象进行验证,但无需提交更改,只需将对象映射到良好的老式 ADO.NET 数据表中,然后每 1000 条左右的记录批量插入它们。

Linq-to-SQL is great for getting data OUT of the database, or for validation and a small handful of inserts/updates at once. But for what you're doing (ETL), it sounds like you need to look into the SqlBulkCopy object. Go ahead and use your L2S objects to do the validation, but then instead of submitting the changes, just map the objects into a good old fashioned ADO.NET DataTable and the every 1000 records or so, bulk insert them.

以可爱出名 2024-11-13 02:05:54

如果性能是一个大问题,那么 LINQ to SQL 可能不是适合该工作的工具。但是,在将 LINQ to SQL 扔出解决方案之前,您可能会考虑以下事项:

  • 尝试在一定数量的记录之后创建一个新的 DataContext。 DataContext 缓存所有实体,以便发送到数据库和从数据库检索,这将导致大量内存占用,并最终......内存不足。
  • 使用 SQL 事件探查器查看 LINQ to SQL 发送到数据库的查询。 LINQ to SQL 可能还会查询数据库以查找您创建的每个实体。
  • 尝试在插入时调整数据库。这可能很困难,但您可以尝试将这些记录写入中间表(依赖性较少),并使用存储过程将数据移动到最终目的地。

批量插入是 O/RM 不擅长的,因此您可能需要采取不同的方法。

If performance is a big concern, LINQ to SQL might not be the tool for the job. However, before tossing LINQ to SQL out the door for your solution, you might consider the following:

  • Try creating a new DataContext after a certain number of records. The DataContext caches all entities so send to and retrieve from the database, which will lead to a large memory footprint and eventually... out of memory.
  • Use the SQL Profiler to see what queries LINQ to SQL sends to the database. Possibly, LINQ to SQL also queries the database for each entity you create.
  • Try to tune the database on inserts. This might be difficult, but you can try writing those records to an intermediate table (with less dependencies) and use a stored procedure to move the data to it's final destination.

Bulk inserts is something O/RMs are not good at, so you might need to take a different approach.

谈情不如逗狗 2024-11-13 02:05:54

如果必须使用 Linq2Sql 进行插入,您可能需要进行间歇性提交。像这样的东西 -

    public void LoadLargeDataUsingLinqToSql(string pathToCSV){
        DataTable dt = LoadMyCSVToDataTable(pathToCSV);
        int myPerformanceCounter = 0;
        foreach(DataRow dr in dt.Rows()){
            MyLinqClass m = ConvertDRToMyLinqClass(dr);
            if(m.IsValidAndReadyToBeSaved()){
                MyDataContext.MyLinqClassRef.InsertOnSubmit(m);
                myPerformanceCounter++;
            }

            if(myPerformaceCounter>25000){
                //Commit to clear cache.
                MyDataContext.SubmitChanges();
                myPerformanceCounter=0;
            }
        }
        //Commit leftovers
        MyDataContext.SubmitChanges();
    }

If you have to do the inserts using Linq2Sql, you may want to do intermittent commits. Something like this -

    public void LoadLargeDataUsingLinqToSql(string pathToCSV){
        DataTable dt = LoadMyCSVToDataTable(pathToCSV);
        int myPerformanceCounter = 0;
        foreach(DataRow dr in dt.Rows()){
            MyLinqClass m = ConvertDRToMyLinqClass(dr);
            if(m.IsValidAndReadyToBeSaved()){
                MyDataContext.MyLinqClassRef.InsertOnSubmit(m);
                myPerformanceCounter++;
            }

            if(myPerformaceCounter>25000){
                //Commit to clear cache.
                MyDataContext.SubmitChanges();
                myPerformanceCounter=0;
            }
        }
        //Commit leftovers
        MyDataContext.SubmitChanges();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文