我需要从Oracle插入3600万行到MSSQL。下面的代码有效,但是即使以1k的大小进行分块(由于您一次只能在MSSQL中插入1k的行),它也不是很快。当前的估计大约需要100个小时才能完成:)
def method(self):
# get IDs and Dates from Oracle
ids_and_dates = self.get_ids_and_dates()
# get 2 each time
for chunk in chunks(ids_and_dates, 2):
# set up list for storing each where clause
where_clauses = []
for id, last_change_dt in chunk:
where_clauses.append(self.queries['where'] % {dict})
# set up final SELECT statement
details_query = self.queries['details'] % " OR ".join([wc for wc in where_clauses])
details_rows = [str(r).replace("None", "null") for r in self.src_adapter.fetchall(details_query)]
for tup in chunks(details_rows, 1000):
# tup in the form of ["(VALUES_QUERY)"], remove []""
insert_query = self.queries['insert'] % ', '.join(c for c in tup if c not in '[]{}""')
self.dest_adapter.execute(insert_query)
根据我一直在阅读的内容,我意识到fetchall
并不理想。我应该考虑实施其他方法吗?我应该尝试使用executemany
而不是使用execute
进行插入吗?
Oracle查询独立版确实很慢,因此我将其分解为几个查询: