我在两个不同的数据库中有大约 200 万条记录,每个数据库有 100 万条。我想将两个表相互连接以找出差异,但由于数据大小,每次执行该操作时 mysql 都会抛出超时错误。这是我的查询:
SELECT id FROM db1.table1 AS a INNER JOIN db2.table1 AS b ON ( b.Id != a.Id )
如有任何帮助,我们将不胜感激!
最佳答案
您正在将每个 db1 记录与具有另一个 ID 的所有 db2 记录连接起来。因此生成了大约一万亿行。
请尝试以下操作:
select * from db1.table1 where id not in (select id from db2.table1);
和
select * from db2.table1 where id not in (select id from db1.table1);
或组合:
select 'db1' as db, * from db1.table1 where id not in (select id from db2.table1)
union all
select 'db2' as db, * from db2.table1 where id not in (select id from db1.table1);
关于mysql - 连接多个数据库mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44677331/