6

I would like to query a MSSQL database using Python's SQLAlchemy. There could be tens of millions of matched rows. In order to use less memory at the server side, I consider using server-side cursor (SSCursor) to slice the matched rows. However, I cannot find examples or resources about SSCursor with SQLAlchemy.

Is it possible to use SSCursor with SQLAlchemy? If this is do-able, can someone show me examples or point out references? If not, any suggested workarounds?

Thanks!

1
  • There currently isn't any MSSQL connector that supports server-side cursors. Commented Dec 6, 2022 at 20:39

1 Answer 1

1

Yes. You just specify the 'cursorclass' option in the connect_args argument. Here is an example with mysql. You'll need to use an MSSQL connector that implements server side cursors like MySQLdb does for mysql as shown below.

from sqlalchemy import create_engine, MetaData
import MySQLdb.cursors
engine = create_engine('mysql://your:details@go/here', connect_args={'cursorclass': MySQLdb.cursors.SSCursor})
Sign up to request clarification or add additional context in comments.

3 Comments

I didn't accept the answer because I didn't find SSCursor for MSSQL. I googled for a while but got no luck :-(. However, I up-voted your answer since you point out the connect_args argument.
There may be a connector that lazily fetches rows for MSSQL. oursql is supposed to do that for mysql.
SSCursor class isn't a cursor which supports server side cursors, it just reads the results in unbuffered mode.

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.