What is a more efficient alternative to using the OR condition in the where clause?

Update X
set 
x.col1 = y.col1,
x.col2 = y.col2,
x.col3 = y.col3,
x.col4 = y.col4 
 from x 
 join y
on x.ID = y.ID
WHERE 
x.col1 <> y.col1 OR
x.col2 <> y.col2 OR
x.col3 <> y.col3 OR
x.col4 <> y.col4 
有帮助吗?

解决方案

As long as there are no other performance issues or business rules about table locking, etc., then this is the correct way to write this simple example. Anon lists an alternate which may or may not work better. Performance tuning requires knowledge of all the different moving components in the system, not just a simple statement, so for further assistance, we would need details about table schemas and indexing on those tables.

其他提示

An alternate option:

Update X
set 
  x.col1 = y.col1,
  x.col2 = y.col2,
  x.col3 = y.col3,
  x.col4 = y.col4 
from x 
INNER join (
  SELECT id,col1,col2,col3,col4 FROM y
  EXCEPT
  SELECT id,col1,col2,col3,col4 FROM x
) y ON y.id = x.id
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top