查询带有小表的巨大表时如何进行有效的联接

时间:2019-03-28 16:10:19

标签: sql sql-server tsql

我有2个表,分别考虑表A和表B。表A有500万条记录,表B有2万条记录。我正在将大表与小表连接在一起,在其中检查表A记录和表B记录,每个记录各一个。

DECLARE @Large TABLE (INN BIGINT,OUTT BIGINT )
INSERT INTO @Large (INN,OUTT)values (11356686,2101189)
INSERT INTO @Large (INN,OUTT)values (11369336,2101301)
INSERT INTO @Large (INN,OUTT)values (11358687,2101487)
INSERT INTO @Large (INN,OUTT)values (11369337,2101609)

DECLARE @small TABLE (INN BIGINT,OUTT BIGINT )
INSERT INTO @small (INN,OUTT)values (11356686,2101189)
INSERT INTO @small (INN,OUTT)values (11369337,2101609)

大:

INN          OUTT
11356686    2101189
11369336    2101301
11358687    2101487
11369337    2101609

小:

INN          OUTT
11356686    2101189
11369337    2101609

我的查询:

Select CASE WHEN T.INN IS NULL THEN O ELSE 1 END from  @LARGE T
LEFT JOIN @SMALLTT
ON T.INN = TT.INN AND T.OUTT = TT.OUTT

进入表后,它们会被很好地索引,甚至在执行计划中也没有任何提示来添加更多索引。 谁能建议我在查询级别该怎么做才能更快地获得结果,例如CROSS APPLY,HASH JOIN等;

1 个答案:

答案 0 :(得分:0)

如果目标是简单地识别在表之间共享的记录,并且您可以修改@Large表,则可以考虑保留@Large上的标记以匹配项进行更新:

DECLARE @Large TABLE (INN BIGINT, OUTT BIGINT, HasMatch BIT DEFAULT(0))
INSERT INTO @Large (INN,OUTT)values (11356686,2101189)
INSERT INTO @Large (INN,OUTT)values (11369336,2101301)
INSERT INTO @Large (INN,OUTT)values (11358687,2101487)
INSERT INTO @Large (INN,OUTT)values (11369337,2101609)

DECLARE @small TABLE (INN BIGINT,OUTT BIGINT )
INSERT INTO @small (INN,OUTT)values (11356686,2101189)
INSERT INTO @small (INN,OUTT)values (11369337,2101609)

UPDATE @Large SET HasMatch = 0

UPDATE l
   SET l.HasMatch = 1
  FROM @Large l INNER JOIN @Small s ON l.INN = s.INN AND l.OUTT = s.OUTT

SELECT INN, OUTT, HasMatch FROM @Large

同样,这假设您对表结构/数据有一定的控制权,并且可以执行UPDATE语句来填充HasMatch列。

好处-您可以利用INNER JOIN及其所有固有优点(有固有优点,对吧?)。