我继承了一个 SQL Server 2008 R2 项目,其中包含从另一张表格进行表格更新的操作:
当前代码如下:
然而,这个查询需要超过30分钟才能运行。
执行计划显示主要瓶颈在于围绕
由于我的DBA技能非常有限,谁能建议最好的方法来提高这个查询的性能?将
Table1
(大约有150,000行)有三个电话号码字段(Tel1
、Tel2
、Tel3
)Table2
(大约有20,000行)也有三个电话号码字段(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分钟才能运行。
执行计划显示主要瓶颈在于围绕
Table1
的聚集索引扫描周围的一个Nested Loop
。这两个表都在它们的ID
列上有聚集索引。由于我的DBA技能非常有限,谁能建议最好的方法来提高这个查询的性能?将
Tel1
、Tel2
和Tel3
添加到每列中作为索引是否是最好的选择,还是可以改变查询以提高性能?
=
就不会返回 true - 你不需要所有这些and t1.Tel1 is not null
。此外,你正在更新查询的字段,这可能会导致一些数据丢失(如果Tel1 = Phone2
但Phone1
为空)。首先尝试规范化电话号码(即有一个链接表来保存电话号码)。 - KeithNULL
不会被评估,它们只是为了效果而存在 :P - KenD