read_sql for SQL Server
I have a SQL Server db and would like to use read_sql to get the result of a query. I looked around and could find only examples for SQLite
Pseudo code of what I'm doing
import pymssql
import pandas as pd
with pymssql.connect(DB_SERVER, DB_UID, DB_PWD, DB_NAME) as conn:
for chunk in pd.read_sql("select from dbo.table where date = <date> and id = <id>", conn, chunksize = 10000):
chunk.to_csv(CSV_PATH, sep=',', mode='a')
This is taking about 15-20 seconds to process 1,000,000 rows with 50 columns
I was wondering if you can guide me on how I can accomplish this using modin.read_sql
Thanks! Karthik
Hi @karthiksurendra, thanks for the question!
Is it possible to use SQLAlchemy instead of pymssql? It is not possible to reuse connection objects in Python from multiple workers because the state they contain cannot be shared or pickled. Creating and passing a connection object to Modin will force the data ingestion to be serial because of this limitation.
It should be possible to create a Modin shared connection module that does not suffer from the same limitations, but that would require changing more than just the one line of code. I can imagine a design that allows you to use whatever connection library you prefer. Something like this:
import modin.pandas as pd
import pymssql
from modin.distributed.connection import DistrubtedDBConn
conn = DistrubtedDBConn.from_args(pymssql.connect, DB_SERVER, DB_UID, DB_PWD, DB_NAME)
df = pd.read_sql(QUERY_STR, conn)
Would an addition like this solve your problem @karthiksurendra?
@devin-petersohn : yes, that would be perfect! To answer your earlier question, yes I can use SQLAlchemy instead of pymssql.
Appreciate it Karthik
Duplicate of #3400