1

我成功查询了这个:

SELECT * FROM tableA
WHERE NOT (column1 IN 
(SELECT column1 FROM tableB) AND columnDate='9999-12-31');

WheretableA有大约 35000k(3500 万)条记录和tableB5k(5000)条记录。我在不到 5 分钟的时间内检索了所有记录tableAtableB

问题是当我尝试(对于另一个具有相同记录量的示例)使用 3 列(键)来实现相同的结果时:

SELECT * FROM tableA
WHERE NOT (column1 || column2 || column3 IN 
(SELECT column1 || column2 || column3 FROM tableB) AND columnDate='9999-12-31');

我检索了tableAtableB1 小时 40 分钟以外的所有记录......!

我可以更有效地查询它吗?

4

2 回答 2

2
SELECT * FROM tableA a
WHERE NOT EXISTS
(SELECT null FROM tableB where a.column1=column1 and a.column2=column2 and a.column3=column3)
AND columnDate<>'9999-12-31'
于 2012-10-31T15:05:59.173 回答
0

一种方法是过滤左连接:

select  * 
from    tableA a
left join
        tableB b
on      b.col1 = a.col1
        and b.col2 = a.col2
        and b.col3 = a.col3
where   a.columnDate <> '9999-12-31'
        and b.id is null -- No match found in B
于 2012-10-31T15:04:46.947 回答