简体   繁体   English

什么是处理几列更新的最佳方法

[英]What's the best way to deal with the updating of several columns

If I have a profile form with 20 fields a user can update on a page. 如果我有包含20个字段的个人资料表单,则用户可以在页面上更新。 It seems like it would be a waste to run an update statement that would update every single column if the user only changed two fields. 如果用户仅更改两个字段,则运行更新语句将更新每一列似乎是浪费的。

I was wondering what would be the best way to handle this for the best performance. 我想知道什么是处理此问题以获得最佳性能的最佳方法。

Since I only have 20 fields its not really an issue but I am just wondering for future development of where maybe it could be more. 由于我只有20个领域,这并不是一个真正的问题,但是我只是想知道将来可能会有更多的发展。

What does everyone else do, do you just update every column for that row? 其他人都在做什么,您只是更新该行的每一列吗?

Your average network packet is 1500 bytes long. 您的平均网络数据包长1500个字节。 That can probably contain your 20 fields with ease. 可能轻松包含您的20个字段。 So you would save no network load by updating less than 20 fields. 因此,通过更新少于20个字段,您将不会节省网络负载。

SQL Server stores data in pages of 8000 bytes. SQL Server将数据存储在8000字节的页面中。 If you update 1 bit, SQL Server will write it away as a fully changed page: 8000 bytes of data. 如果更新1位,则SQL Server会将其写为完整更改的页面:8000字节的数据。 So you save no disk activity by updating less than 20 fields. 因此,通过更新少于20个字段,您不会节省磁盘活动。

Optimizing for performance is often a mistake. 优化性能通常是一个错误。 Databases are very fast these days. 这些天数据库非常快。 Optimize for simple and clear code instead. 而是针对简单明了的代码进行优化。

Tools like Entity Framework (or other ORM tools) will handle this for you, for "free". 诸如Entity Framework之类的工具(或其他ORM工具)将为您免费处理此问题。

They track what has changed in your objects, and they will issue a tailor-made UPDATE statement that will only update those columns that have actually changed. 他们跟踪对象中发生了什么更改,并且将发出量身定制的UPDATE语句,该语句仅更新那些实际更改的列。

You need not worry about any of those details anymore. 您无需再担心任何这些细节。 All taken care of for you. 所有为您照顾。 Try it! 试试吧!

Resources: 资源:

I can only imagine fetching the current db record, compare field by field with the user input and then update only the changed fields. 我只能想象获取当前的数据库记录,逐字段与用户输入进行比较,然后仅更新更改的字段。

This will be more expensive than just updating all fields in a single update statement, so I'd go with single update choice. 这将比仅在单个update语句中更新所有字段更加昂贵,因此我会选择单个update。

CREATE PROC [dbo].[UpdateMe]
(
    @pkey int,
    @Col1 int = null,
    @Col2 int = null,
    ...,
    @Col20 int = null
)
AS BEGIN

    UPDATE [Table]
           SET Col1 = ISNULL(@Col1,Col1),
           Col2 = ISNULL(@Col2,Col2),
           ...
           Col20 = ISNULL(@Col20,Col20)
    WHERE pkey = @pkey

END

Keep track of what has changed on the client and send things accordingly. 跟踪客户端发生了什么变化,并相应地发送信息。 If you are worried about sending to much over the wire, use named parameters and only send what has changed. 如果您担心要通过网络发送太多信息,请使用命名参数,仅发送已更改的内容。 Since there are defaults on the procedure, it doesn't cost you anything. 由于该过程有默认设置,因此不会花费您任何费用。 The seek time in sql is where the cost is, so updating a column to itself should be pretty cheap. sql中的查找时间是开销所在,因此将列更新为其本身应该非常便宜。

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

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