2

The process inserting data from the dataframe into the oracle database is work very slow (more than one hour)

Advise me please, how I can to improve the process of inserting data into the database?

count column = 50, raw count = > 250000

I have a lot of such files

Connecting to the database in this way

dialect = 'oracle'
sql_driver = 'cx_oracle'

to_engine: str = dialect + '+' + sql_driver + '://' + user + ':' + password + '@' + host + ':' + str(port) + '/?service_name=' + service_name

connection = create_engine(to_engine)

to_sql method look like this

 df.to_sql(table_name_in_db, con=connection, schema='', if_exists='append', index=False, chunksize = 1000,  dtype=None)

Thanks a lot!

1
  • I'd use SQL Loader. It is extremely fast. Commented Mar 3, 2022 at 8:08

1 Answer 1

1

This trick help me

Speed up to_sql() when writing Pandas DataFrame to Oracle database using SqlAlchemy and cx_Oracle Conclusion: use the following trick in order to explicitly specify dtype for all DF columns of object dtype when saving DataFrames to Oracle DB. Otherwise it'll be saved as CLOB data type, which requires special treatment and makes it very slow

 dtyp = {c:types.VARCHAR(df[c].str.len().max())
        for c in df.columns[df.dtypes == 'object'].tolist()}

 df.to_sql(..., dtype=dtyp)here
Sign up to request clarification or add additional context in comments.

3 Comments

Further down on that other SO question, chunksize was mentioned. You may also want to tune this.
Thanks, "The ideal chunksize depends on your table dimensions. A table with a lot of columns needs a smaller chunk-size than a table that has only 3." I dont know, this is true or not I find this method here towardsdatascience.com/… I had a lot of such files where are different numbers of columns and rows
You can run some tests to tune chunksize for your data and network. But if you want best performance, then use cx_Oracle directly without the Pandas & SQLAlchemy layers. See the cx_Oracle doc on batch loading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.