多行的高效SQL UPDATE命令 [英] Efficient SQL UPDATE command for many rows

查看:247
本文介绍了多行的高效SQL UPDATE命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题不是能够之一,而是能够快速

My problem is not one of "Able to", but "able to quickly"

我有一个表,表中的行数为静态,每行有多个项目该行需要经常更新。我正在使用.Net和SQL Server进行此操作。

I have a table with a static number of rows for which multiple items per row need to be updated frequently. I'm doing this with .Net and SQL Server.

为了进行测试,该表具有约5000行,20列,并且我将每行更新3个字段/列每5秒排一次。

For testing, the table has around 5000 rows, 20 columns, and I'm updating 3 fields/columns per row every 5 seconds.

我已经研究了一些解决方案,但是无法将更新时间更新到我想要的位置(<1秒):

I've investigated some solutions but cannot get my update time to where I want (< 1 second):


  1. 执行5000个SQL UPDATE命令:大约需要4秒

  2. 使用DataAdapter,更新DataTable,然后执行DataAdapter.Update:大约4秒(它只是执行多个UPDATE命令,如1。)

  3. 使用大的单个UPDATE TABLE表名col1 = CASE ... WHEN .... WHEN ...:给出错误查询处理器耗尽了堆栈空间。...

目标是获取内存中的一堆数据并将其持久化到表,以便其他应用程序或用户可以访问此数据。我假设有做这件事的有效方法,但我没有发现。

The goal is to take a bunch of data which is in memory and to persist it to a table so other applications or users can access this data. I assume there are effecient means to do this that I have not stumbled upon.

一个限制是我可能无法访问用户SQL Server,所以我想就像使用UPDATE命令或类似命令在.Net代码中以编程方式执行此操作。

One restrictions is that I may not have access to the users SQL Server, so I'd like to do this programmatically in the .Net code using an UPDATE command or similar.

推荐答案

让您的代码调用System.Data .SqlClient.SqlBulkCopy API批量插入将更改数据插入登台表,然后使用UPDATE命令读取登台数据并使用它更新目标表。

Have your code call the System.Data.SqlClient.SqlBulkCopy API to Bulk Insert your change data into a staging table, then use an UPDATE command to read the staging data and update your target table with it.

采用这种方法的原因是,上述所有方法都存在相同的基本性能瓶颈:首先将数据导入SQL Server。一旦进入该目录,有很多方法可以将静态数据转换为UPDATE,并且都具有非常好的性能。

The reason for this approach is that all of the methods above suffer from the same basic performance bottleneck: getting the data into SQL Server in the first place. Once it's in there, there are many ways to turn that static data into an UPDATE, all with very good performance.

这篇关于多行的高效SQL UPDATE命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆