我继承了一个 SQL Server 2008 R2 项目,其中包括从另一个表更新表:
Table1
(大约 150,000 行)有 3 个电话号码字段(Tel1
,Tel2
,Tel3
)Table2
(大约 20,000 行)有 3 个电话号码字段(Phone1
,Phone2
,Phone3
)
..当这些数字中的任何一个匹配时,Table1
应该更新。
当前代码如下所示:
UPDATE t1
SET surname = t2.surname, Address1=t2.Address1, DOB=t2.DOB, Tel1=t2.Phone1, Tel2=t2.Phone2, Tel3=t2.Phone3,
FROM Table1 t1
inner join Table2 t2
on
(t1.Tel1 = t2.Phone1 and t1.Tel1 is not null) or
(t1.Tel1 = t2.Phone2 and t1.Tel1 is not null) or
(t1.Tel1 = t2.Phone3 and t1.Tel1 is not null) or
(t1.Tel2 = t2.Phone1 and t1.Tel2 is not null) or
(t1.Tel2 = t2.Phone2 and t1.Tel2 is not null) or
(t1.Tel2 = t2.Phone3 and t1.Tel2 is not null) or
(t1.Tel3 = t2.Phone1 and t1.Tel3 is not null) or
(t1.Tel3 = t2.Phone2 and t1.Tel3 is not null) or
(t1.Tel3 = t2.Phone3 and t1.Tel3 is not null);
但是,此查询需要 30 多分钟才能运行。
执行计划表明主要瓶颈是Nested Loop
围绕聚集索引扫描Table1
。两个表的列都有聚集索引ID
。
由于我的 DBA 技能非常有限,任何人都可以提出提高此查询性能的最佳方法吗?Tel1
为,Tel2
和为每一列添加索引Tel3
是最好的选择,还是可以更改查询以提高性能?