优化 SELECT 从 Oracle 到 INSERT 到 SQLite(在 Python3 中)

Optimizing a SELECT from Oracle to INSERT into SQLite (in Python3)

我有一个 Oracle 数据库,我正在将数据的一个子集提取到本地 SQLite 数据库中。我的代码基本上如下:

print(datetime.datetime.now())

#Oracle portion of the script
sql = '''select [columns] from [table] where [condition]''' 
oracle_cursor.execute(sql)

print(datetime.datetime.now())

#SQLite portion of the script
sqlite_conn.executeany('''INSERT into [table] 
                          ([columns]) 
                          values (?,?,?...etc.)''', 
                          oracle_cursor.fetchall()
                       )
sqlite_conn.commit()
sqlite_conn.close()

它做了需要它做的事情,但它花费的时间比我想要的要长。 Oracle 部分的执行实际上快得惊人,大约 3 分钟。但是插入需要更长的时间。我玩过 SQLite 设置,如缓冲区设置等。似乎没有什么能打破 50 行/秒。前三分钟网络 activity 出现峰值,但一旦它从上面打印第二个日期时间,就没有网络 activity,这让我相信瓶颈是我编写的代码。我的代码在插入时效率低下吗?如果是这样,是否有更好的方法来获得我想要的东西?

fetchall() 将所有数据加载到内存中。这不是必需的,因为 executemany 可以与任何迭代器一起使用;将 oracle_cursor.fetchall() 替换为 oracle_cursor.

还要确保您使用的是单笔交易。 (如果你启用了自动提交模式,你应该显式地启动一个事务。)