这张桌子是一张大桌子,有上百万的记录。
表1有三列
ID、列X和列Y。目前我使用的语句" in“很慢,尤其是在大表中。我希望在不使用in语句的情况下提高此update语句的性能。任何帮助都是非常感谢的。
Update [Table1] set Column_X='Delete' where Column_Y in(
select distinct (Column_Y) from [Table1] where Column_X='Delete'
)
result before running script
Table 1
ID Column X Column Y
1 Delete CAT
2 x1 CAT
3 x1 CAT
4 x1 COW
5 x1 COW
6 x2 Moon
7 Delete Chicken
Intended result after running script
Table 1
ID Column X Column Y
1 Delete CAT
2 Delete CAT
3 Delete CAT
4 x1 COW
5 x1 COW
6 x2 Moon
7 Delete Chicken 发布于 2014-04-07 03:53:57
您可以在FROM子句中多次使用同一个表,因此如下所示:
update Table1
set Column_X = 'Delete'
from Table1 a, Table1 b
where a.Column_Y = b.Column_Y
and b.Column_X = 'Delete'更新会影响Table1的第一个实例,即"a“。
发布于 2014-04-07 03:49:39
您可以对此使用exists子句。
Update [Table1] set Column_X='Delete' from [Table1] t1where exists(
select t2.Column_Y
from [Table1] t2
where t2.Column_X = 'Delete'
and t2.Column_Y = t1.Column_Y)它应该比"in“子句的速度快一点。
请不要使用"in“,因为它会进行全表扫描。“存在”一语几乎适用于所有情况。
https://stackoverflow.com/questions/22903173
复制相似问题