mysql - 连接多个数据库mysql

标签 mysql

我在两个不同的数据库中有大约 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/

相关文章:

php - 防止 $_SESSION 提交大量表单

mysql - 如何在不使用 between 或 >= 的情况下从给定的月份中进行选择?

php - SET 排序规则和字符 UTF8 - Mysql PDO

MySQL 查询求和 2 行

MySQL:按查询分组计数

php - mysql查询以不同的格式获取特定日期的数据

mysql - 如何将MySQL查询结果导出到多个 `txt`文件?

mysql - “GRANT ALL ON 数据库。* TO 命令中出现错误

php - 在mysql简化示例中查找重复行

mysql - 一一显示具有重复 StudentId 的验证消息