MS Sql Server 性能更新单个列与所有列
Win Srvr 2003+ 上的 MS SQL Srvr 2005/2008
我一次仅更新 1 行,更新是为了响应 Web 表单上的用户更改。
我正在使用 PK 更新表中的几列。该表有 95 列。通常会更新 1 个 FK 列和 1 或 2 个其他列。该表有 6 个 FK。
动态生成仅在 UPDATE 的 SET 部分中更改列的 UPDATE 语句对我有好处吗?还是坚持使用对所有列进行参数化更新的当前存储过程?
目前,Web 表单中的数据不会立即更改,而是会发送回服务器并可供更新。我无法跳转到 AJAX 场景,此时仅将更改的数据从客户端浏览器发回服务器。
谢谢, G
MS SQL Srvr 2005/2008 on Win Srvr 2003+
I am only updating 1 row at a time, the UPDATE is in response to a user change on a web form.
I am updating a few columns in a table using the PK. The table has 95 columns. Typically 1 FK column and 1 or 2 other columns will be updated. The table has 6 FK's.
Is it of benefit for me to dynamically generate the UPDATE statement only having the columns being changed in the SET portion of the UPDATE, or stick with the current Stored Procedure using a parameterized update with all of the columns?
Currently, and not subject to immediate change, the data from the web form is posted back to the server and is available for the update. I can't jump to an AJAX scenario where only changed data is posted back to the server from the client browser at this point.
Thanx,
G
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就性能而言,在一次更新中更新多个列比在多次更新中更新单个列更好,一旦数据库锁定一行来更新,用于更改值的时间就不是性能问题,另一方面,锁定一行可能会导致性能问题,如果有多个连接尝试访问相同的信息,情况可能会变得更糟。我建议保持参数化存储过程的状态,而不是尝试更新单行列。
In terms of performance its better to update multiple columns in a single update than updating single columns in multiple updates, once the databases locks a row for updating the time used for changing the values is not a performance issue, on the other hand the time that takes to lock a row can cause performance issues and it can get worst if you have multiple connections trying to access the same information. I would recommend to stay as you are with the parameterized stored procedure rather than trying to update single row-columns.
SQL Server 读取和写入由 8kb 数据组成的“页”。通常,一页将包含一行或多行。
由于磁盘 I/O 是更新中最昂贵的部分,因此更新一半列和所有列的成本大致相同。它仍然会产生 8kb 磁盘 I/O。
还有另一个方面,通常不会发挥作用,因为 SQL Server 在 8kb 页面中写入。但想象一下您的行如下所示:
现在,如果您将
col1
更新为长 5 个字节,则col2
必须向前移动 5 个字节。因此,即使您不更新col2
,它仍然需要写入磁盘。SQL Server reads and writes "pages" that consist of 8kb of data. Typically, a page will contain one or more row.
Since disk I/O is the expensive part of an update, the cost of updating half the columns and all the columns is roughly the same. It will still result in 8kb disk I/O.
There's another aspect, that usually doesn't come into play because SQL Server writes in 8kb pages. But imagine your row looks like this:
Now if you update
col1
to be 5 bytes longer,col2
has to be moved forward by five bytes. So even if you don't updatecol2
, it will still have to be written to disk.