简体   繁体   English

C#-SQL插入和更新速度

[英]C# - SQL Insert and Update speed

I have a CSV file of about 80,000 rows and I am trying to process it in a C# application and make necessary changes to my SQL database. 我有一个大约80,000行的CSV文件,我试图在C#应用程序中对其进行处理,并对我的SQL数据库进行必要的更改。 I read the CSV in to the application which doesn't take long then I process it from there. 我将CSV读取到应用程序中并不需要很长时间,然后从那里开始处理它。

For each of the records from the CSV I either insert a new record or update an existing one in the database based on a condition(from a SQL select statement). 对于来自CSV的每条记录,我都将基于条件(来自SQL select语句)插入新记录或更新数据库中的现有记录。 I am just using C# inline SQL commands to do my selects, updates, and inserts. 我只是使用C#内联SQL命令来进行选择,更新和插入。 This process is taking about 40 minutes to complete. 此过程大约需要40分钟才能完成。

I was wondering if there are any best practices for large SQL database updates and insertions that will be able to make the process quicker. 我想知道是否有针对大型SQL数据库更新和插入的最佳实践,可以使该过程更快。

Thanks in advance! 提前致谢!

Use begin transaction (before) and commit (after all the work). 使用begin transaction(之前)和commit(所有工作之后)。 If you don't explicitly start a transaction, for each of your commands, SQL will implicitly start a transaction and finish it, delaying the process (so 80k rows mean 80k separate transations, whereas all commands should run in one transaction.). 如果您没有显式启动事务,那么对于您的每个命令,SQL都会隐式启动事务并完成它,从而延迟该过程(因此80k行意味着80k个独立的转换,而所有命令应在一个事务中运行。)。

You should try to use backgroundWorker... It helped me when I had to make large queries... 您应该尝试使用backgroundWorker ...当我不得不进行大型查询时,它对我有所帮助...

How to use BackgroundWorker 如何使用BackgroundWorker

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

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