内连接的ON-Clause中的附加“Or-Criteria”

时间:2011-06-14 11:31:17

标签: sql-server-2005 tsql join

On-Clause-Criteria取决于两列中的任何一列时,(内部)连接两个表的正确方法是什么?因此,当两列中的任何一列等于Table2的一列时,Table1应该连接。

看看以下简化的T-SQL,看看我的意思,以及我是否以正确的方式完成了它:

SET TRANSACTION ISOLATION LEVEL READ COMMITTED
GO
BEGIN TRANSACTION;
GO
create table #RMA1(
    SSN_IN varchar(15),
    SSN_OUT varchar(15)
)
create table #RMA2(
    SSN_NUMBER varchar(15),
)
INSERT INTO #RMA1 VALUES('012590001827977', '351600035840752');
INSERT INTO #RMA1 VALUES('332653577440220', '351600035804105');
INSERT INTO #RMA1 VALUES('350302031074780', '350302031074780');
INSERT INTO #RMA1 VALUES('351649036778024', '351806033440194');

INSERT INTO #RMA2 VALUES('012590001827977');
INSERT INTO #RMA2 VALUES('332653577440220');
INSERT INTO #RMA2 VALUES('350302031074780');
INSERT INTO #RMA2 VALUES('351649036778024');
INSERT INTO #RMA2 VALUES('351600035840752');
INSERT INTO #RMA2 VALUES('351600035804106');

SELECT SSN_IN,SSN_OUT,SSN_NUMBER FROM #RMA1 INNER JOIN
#RMA2 ON (#RMA1.SSN_IN = #RMA2.SSN_NUMBER OR
          #RMA1.SSN_Out = #RMA2.SSN_NUMBER)

DROP TABLE #RMA1;
DROP TABLE #RMA2;
GO
COMMIT TRANSACTION;
GO

结果:

SSN_IN          SSN_OUT            SSN_NUMBER
012590001827977 351600035840752    012590001827977
012590001827977 351600035840752    351600035840752
332653577440220 351600035804105    332653577440220
350302031074780 350302031074780    350302031074780
351649036778024 351806033440194    351649036778024

问:因此,在OR - 内部联接的条款中使用ON加入表是否正确?

它确实正确返回5条记录并丢弃#RM2的最后一条记录,因为它既不在#RM1.SSN_IN也不在#RM1.SSN_OUT。但我感觉不好,这并不能总是给出正确的结果。实际上,真正的完整查询的表现非常糟糕。

1 个答案:

答案 0 :(得分:3)

结果应该是正确的,但你的观察是正确的,表现不好。数据库无法有效使用索引,因为它必须扫描索引并对每个项执行算术运算。

改为使两个左连接可以让数据库更好地利用索引:

select
  SSN_IN,
  SSN_OUT,
  coalesce(T1.SSN_NUMBER, T2.SSN_NUMBER) as SSN_NUMBER
from
  #RMA1
  left join #RMA2 as T1 on #RMA1.SSN_IN = T1.SSN_NUMBER
  left join #RMA2 as T2 on #RMA1.SSN_Out = T2.SSN_NUMBER
where
  T1.SSN_NUMBER is not null or T2.SSN_NUMBER is not null