比较两个大表中的多行

时间:2017-02-16 06:44:38

标签: mysql

我有两个大表,每个表的记录超过10mil。我试图找出缺少其中一个表的值。该表具有相同的模式,看起来像这样

表1

| ColA       | ColB        | ColC         |
|:-----------|------------:|:------------:|
| John       | Steve       | Richardson   |
| Steve      | Richard     | Johnson      |
| Will       | Jack        | Jackson      |

表2

| ColA       | ColB        | ColC         |
|:-----------|------------:|:------------:|
| John       | Steve       | Richardson   |
| Will       | Jack        | Jackson      |

我正在寻找 Steve Richard Johnson 记录

我尝试了以下查询,但超时

SELECT *
FROM `Table1` AS a
WHERE NOT EXISTS 
(SELECT * FROM `Table2` AS b WHERE 
`a`.`ColA` = `b`.`ColA` AND `a`.`ColB` = `b`.`ColB` AND `a`.`ColC` = `b`.`ColC`)

1 个答案:

答案 0 :(得分:2)

从t1应用带有table2的左连接以获得非匹配行,如下所示

select t1.* 
from table1 t1 
left join Table2 t2 on t1.col1A = t2.colA and t1.colB = t2.colB and t1.colC = t2.colC
where t2.colA is null and t2.cOlB is null and t2.colC is null