首页 > 解决方案 > 性能调整:使用集合或数据框比较两个表:获取最终消息为“Killed”

问题描述

我的表很少,表将在不同的数据库中,下面是我正在尝试的示例比较

EmplTbl = cur.execute("select A , B , C from EmployeeTable where EmplName in ('A','B')") 
emp_entries = set(cur)

DeptTbl = cur.execute("select A , B , C from DeptTable") 
dept_entries = set(cur) 

print(emp_entries.difference(dept_entries))

在这个例子中,我只提供了 3 列进行比较。但就我而言,我有 30-40 列。当我尝试在集合之间进行区分或使用“for”循环或数据帧连接比较时——脚本运行速度非常慢,我收到的最终消息是“已杀死”

在下面的代码中,我尝试进行内部连接以获得完全匹配

EmplTbl = cur.execute("select A , B , C from EmployeeTable where EmplName in ('A','B')") 
emp_entries = set(cur)

DeptTbl = cur.execute("select A , B , C from DeptTable") 

for DeptTbl in cur:
    if emp_entries in DeptTbl:
        print(emp_entries)

记录量:我可能有多达 1000 万条记录

有什么办法可以提高我的性能使它运行得更快。我有 4 个节点配置的 linux 服务器。请建议

标签: pythonpandasdataframeset

解决方案


您可以直接使用查询差异:

Select col1, col2, col3 from table 1
Minus
Select col1, col2, col3 from table2;

- 或者 -

Select col1, col2, col3 from table1 t1
Where exists 
(Select 1 from table2 t2
Where t1.col1 = t2.col1
And t1.col2 = t2.col2
And t1.col3 = t2.col3)

干杯!!


推荐阅读