Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_database() - Insert many rows with sql server using fast_executemany #16053

Closed
gregory-kral-shell opened this issue May 5, 2024 · 3 comments · Fixed by #16099
Closed
Assignees
Labels
A-io-database Area: reading/writing to databases accepted Ready for implementation enhancement New feature or an improvement of an existing feature

Comments

@gregory-kral-shell
Copy link

Description

I would like to be able to insert many rows quickly in an sql server database table with df.write_database()

I can currently achieve this with the fast_executemany option in pandas but I can’t find a way to do this with polars.write_database() which only accepts a connection string and not an sql alchemy engine. The pandas code is much faster than the polars equivalent without fast_executemany.

Example code with pandas:
engine = create_engine(
"mssql+pyodbc://{user}:{password}@{server}/{db_name}?driver=ODBC+Driver+17+for+SQL+Server",
fast_executemany=True,
)
ret = curve.to_pandas().to_sql(
name="curve",
con=engine,
if_exists="append",
index=False,
)

Example code with polars:
curve.write_database(
table_name="curve",
connection="mssql+pyodbc://{user}:{password}@{server}/{db_name}?driver=ODBC+Driver+17+for+SQL+Server",
if_table_exists="append",
engine="sqlalchemy",
)

@gregory-kral-shell gregory-kral-shell added the enhancement New feature or an improvement of an existing feature label May 5, 2024
@alexander-beedie alexander-beedie self-assigned this May 5, 2024
@alexander-beedie alexander-beedie added the accepted Ready for implementation label May 5, 2024
@alexander-beedie
Copy link
Collaborator

alexander-beedie commented May 5, 2024

Yup; allowing write_database to take a connection object is on my near-term "todo" list... I'll try to get it done this week. (Longer-term I have a total rewrite of this entire set of functionality in mind so we can drop the internal adbc/pandas redirects completely 😅)

@alexander-beedie
Copy link
Collaborator

Done; in the upcoming 0.20.26 you'll be able to pass instantiated connections to write_database, so you can use the same approach (create one with fast_executemany) with Polars too 👌

@gregory-kral-shell
Copy link
Author

Great thanks for that!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io-database Area: reading/writing to databases accepted Ready for implementation enhancement New feature or an improvement of an existing feature
Projects
Archived in project
2 participants