简体   繁体   English

SQL 查询更新性能

[英]SQL query update performance

I'm trying to understand why an sql update with case have lower performance than two separated update with where clause.我试图理解为什么带有 case 的 sql 更新的性能低于带有 where 子句的两个单独的更新。

Here are the conditions:以下是条件:

  • if column1 in (1, 2, 3) then set column3 = 'A'如果 column1 in (1, 2, 3) 然后设置 column3 = 'A'
  • else set column3 = 'B'否则设置 column3 = 'B'

I have two options.我有两个选择。

Option #1:选项1:

UPDATE tableA
SET Column3 = (CASE WHEN column1 IN (1,2,3) THEN 'A' ELSE 'B' END)

Option #2:选项#2:

UPDATE tableA
SET column3 = 'A'
WHERE column1 IN (1,2,3)

UPDATE tableA
SET column3 = 'B'
WHERE column1 NOT IN (1, 2, 3)

The option #2 has the better performance than the first one.选项 #2 的性能优于第一个。

I would appreciate if any have reason to this.如果有任何理由,我将不胜感激。

I thought the option 1 have better performance due to one query with no where clause.我认为选项 1 具有更好的性能,因为一个查询没有 where 子句。

You didn't show us your table and index definitions, or your query plans, so this is a guess.您没有向我们展示您的表和索引定义,或者您的查询计划,所以这是一个猜测。

Your first query basically means: update every row of the table setting the column to either 'A' or 'B' .您的第一个查询基本上意味着:更新表的每一行,将列设置为'A''B' It has no WHERE clause so it must scan the whole table and change every row.它没有 WHERE 子句,因此它必须扫描整个表并更改每一行。

Your second choice, with two queries, has the possibility of using an index to find the rows it must update.您的第二个选择,有两个查询,有可能使用索引来查找它必须更新的行。 Between the two queries you still update all the rows.在这两个查询之间,您仍然更新所有行。 But it seems your system still save time.不过看来你的系统还是省时间的。 Possible reasons:可能的原因:

  1. SQL Server finds the rows faster with WHERE clauses. SQL 服务器使用 WHERE 子句更快地查找行。
  2. The number of rows updated is smaller in each query.每个查询中更新的行数较小。 A lot of the cost of this kind of bulk update is committing the transaction.这种批量更新的很多成本是提交事务。 (SQL Server bundles up all the changes and commits them to the table so they appear simultaneous to other users of the table.) It's possible the transaction of your first example was so large it involved lots of IO. (SQL Server 捆绑所有更改并将它们提交到表中,以便它们同时显示给表的其他用户。)您的第一个示例的事务可能非常大,涉及很多 IO。

If you want to do this sort of many-row update, you would be wise to do something like this.如果你想做这种多行更新,你会明智地做这样的事情。

UPDATE tableA
SET column3 = 'A'
WHERE column1 IN (1,2,3)
AND column3 <> 'A'

This will skip the updating of rows that don't need to change.这将跳过不需要更改的行的更新。

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

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