import cx_Oracle # 连接Oracle数据库 conn = cx_Oracle.connect('HHZNWSC/hhznwsc@192.168.139.9:1521/orcl') cursor = conn.cursor() # 查询重复实例号从test2表 query = "select instance from test2 group by instance having count(*) >1" cursor.execute(query) haha = cursor.fetchall() # 循环遍历查询结果,构造insert语句并输出到test1表 i = 0 for row in haha: instance = row[0] print(instance) insert_query = "delete from test2 where instance = '{0}' and ROWID NOT IN (SELECT MAX(ROWID) FROM test2 GROUP BY instance HAVING COUNT(*) > 1) ".format(instance) print(insert_query) print(row[0],'ok') cursor.execute(insert_query) conn.commit() i = i+1 print(i) # 关闭数据库连接 cursor.close() conn.close() 改为多进程
时间: 2024-04-14 09:31:27 浏览: 165
import cx_Oracle
import multiprocessing
def delete_duplicates(instance):
# 连接Oracle数据库
conn = cx_Oracle.connect('HHZNWSC/hhznwsc@192.168.139.9:1521/orcl')
cursor = conn.cursor()
# 构造delete语句
delete_query = "delete from test2 where instance = '{0}' and ROWID NOT IN (SELECT MAX(ROWID) FROM test2 GROUP BY instance HAVING COUNT(*) > 1)".format(instance)
# 执行delete语句
cursor.execute(delete_query)
conn.commit()
# 关闭数据库连接
cursor.close()
conn.close()
if __name__ == '__main__':
# 连接Oracle数据库
conn = cx_Oracle.connect('HHZNWSC/hhznwsc@192.168.139.9:1521/orcl')
cursor = conn.cursor()
# 查询重复实例号从test2表
query = "select instance from test2 group by instance having count(*) > 1"
cursor.execute(query)
instances = cursor.fetchall()
# 关闭数据库连接
cursor.close()
conn.close()
# 创建多个进程来并行处理删除操作
processes = []
for instance in instances:
p = multiprocessing.Process(target=delete_duplicates, args=(instance[0],))
processes.append(p)
p.start()
# 等待所有进程结束
for process in processes:
process.join()
print("删除操作已完成")
阅读全文