I have a sql query that containers approximately 500k rows and 47 columns and I want this query to be dumped into a csv file , so I can import afterwards the file into a table onto a new database hosted into another server. My code does not use any fancy library that would cause some overhead into the process , but nonetheless the writing takes around 15 minutes to complete. I believe there is something wrong with my code but can't define what would speed things up. The connection uses cx_oracle driver in python.
import config
from pathlib import WindowsPath
import csv
con = cx_Oracle.connect(f'{config.USER_ODS}/{config.PASS_ODS}@{config.HOST_ODS}:{config.PORT_ODS}/{config.SERVICENAME_ODS}')
sql = 'SELECT * FROM ods.v_hsbc_ucmdb_eim'
cur = con.cursor()
output = WindowsPath('result.csv')
with output.open('w',encoding="utf-8") as f:
writer = csv.writer(f, lineterminator="\n")
cur.execute(sql)
col_names = [row[0] for row in cur.description]
writer.writerow(col_names)
for row in cur:
writer.writerow(row)
for row in cur:is the slowest way you can do this. I would look at the other fetch methods in particularfetchmany(). See also Batch procesing in particular Loading CSV which you can reverse to export CSV.