0

我在存在多列比较的 SQL Server 连接方面存在性能问题。

SELECT T1.* FROM T1, T2
WHERE T1.LASTNAME = T2.LASTNAME AND
T1.FIRSTNAME = T2.FIRSTNAME AND
T1.MIDDLENAME = T2.MIDDLENAME AND
DAY(T1.DoB) = T2.DOBDay AND
MONTH(T1.DoB) = T2.DOBMonth

这些表中的每一个都超过 20,000 条记录,这确实导致了性能问题。我跑了它,它花了超过 6 分钟,所以我取消了它!

有没有办法提高该语句的性能?

4

2 回答 2

2

由于您只需要 T1 表中的数据,因此您可以使用左连接或存在子句。

  select T1.* 
  from T1
  where
      exists (
                  select 1
                  from T2
                  where
                     T1.LASTNAME = T2.LASTNAME
                     and T1.FIRSTNAME = T2.FIRSTNAME
                     and T1.MIDDLENAME = T2.MIDDLENAME
                     and DAY(T1.DoB) = T2.DOBDay
                     and MONTH(T1.DoB) = T2.DOBMonth
             )
于 2013-06-28T03:09:28.530 回答
0

您必须先在两个表上创建以下非聚集索引..

CREATE NONCLUSTERED INDEX IX_First_Middle_T2  ON T2 (FIRSTNAME,MIDDLENAME); 

现在运行以下查询

SELECT T1.*,DAY(T1.DOB) as DOBDay, MONTH(T1.DOB) as DOBMonth INTO #T1
CREATE NONCLUSTERED INDEX IX_First_Middle_T1  ON #T1 (FIRSTNAME,MIDDLENAME); 

SELECT T1.*
FROM #T1 T1
JOIN T2
ON T1.LASTNAME     = T2.LASTNAME
and T1.FIRSTNAME   = T2.FIRSTNAME
and T1.MIDDLENAME  = T2.MIDDLENAME
and T1.DOBDay      = T2.DOBDay
and T1.DOBMonth    = T2.DOBMonth

上面的查询为您提供了两个带有月份和日期的附加列,而不是 T1.* 将其替换为列名。

于 2013-06-28T05:01:08.417 回答