更新一行还是删除它并插入新行更快? [英] Is it faster to UPDATE a row, or to DELETE it and INSERT a new one?

查看:74
本文介绍了更新一行还是删除它并插入新行更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于 SQL Server 2008/2005 上的两个场景 -1 表有 5 行2 表有 100 万行

Given two scenarios on SQL Server 2008/2005 - 1 Table has 5 rows 2 Table has 1 million rows

如果我们需要更新几行,什么是有效的,为什么?1)更新所需的列2) 删除该行并插入带有更新信息的新行

If we need to update a few rows, what is is efficient and why? 1) UPDATE the required columns 2) DELETE the row and INSERT new row with the updated information

推荐答案

你不应该问这个问题.您在问以某种模糊的‘更快’概念的名义,是采用正确的方式还是错误的方式更好?"

You should not be asking this question. You are asking "Is it better to do it the right way, or the wrong way, in the name of some nebulous idea of 'faster'?"

您的应用程序是否太慢了?您是否出于某种原因认为问题是因为您的 UPDATE 花费的时间太长?您是否对数据库交互的性能进行了任何测量和基准测试?

Do you have an application that is somehow too slow? Do you for some reason think that the problem is because your UPDATEs are taking too long? Have you done any measurement and benchmarking of the performance of your database interactions?

您正在做的是最糟糕的过早优化,这样做会损害您的应用程序.您正在胡乱猜测如何加快代码速度,但完全没有任何依据.

What you are doing is premature optimization of the worst kind, and you are doing your application a disservice by doing so. You are making wild guesses about how to speed up your code, with absolutely nothing to base it on.

正确编写代码.然后尝试找出性能问题所在.你是否有性能问题,或者你问这个问题只是因为你认为这是你应该问的问题?你不应该.

Write your code right. Then try to find where you have a performance problem. Do you even HAVE a performance problem, or are you asking this question simply because you think it's something you should be asking about? You shouldn't.

即使您的 UPDATE 太慢,我们也无法回答X 是否比 Y 快"的问题,因为您没有提供给我们几乎足够的信息,例如:

Even if you specifically DID have a problem with your UPDATEs being too slow, we can't answer the question of "Is X faster than Y" because you have not given us nearly enough information, such as:

  • 您使用的是什么数据库
  • 表格布局
  • 数据库中有哪些索引
  • 您如何与数据库交互

请正确编写您的代码,然后再详细说明太慢的地方,而不是猜测微优化.

Please, write your code correctly, and then come back with specifics about what is too slow, rather than guessing at micro-optimizations.

这篇关于更新一行还是删除它并插入新行更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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