[英]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:以下是条件:
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:可能的原因:
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.