473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pymssql - execute loads all results into memory!

Hi

I am trying to use pymssql, and have an issue where by the execute
(not the fetch) is appearing to load all records into memory.

if I execute

con = pymssql.connect(...)
cur = con.cursor()
cur.execute(sql)
rec = cur.fetchone()

if I put in a query which returns a lot of records into "sql" then the
execute never returns, pythons memory usage slowly ballons till the
machine cant give anymore. If I put a sql query returning only few
rows, then it works fine.

So I am not sure why an execute would feel the need to load all rows,
but its seriously crippling. Does anyone know if this is a bug or
something I can "turn off"

thanks,
Oct 20 '08 #1
7 6612
I am trying to use pymssql, and have an issue where by the execute
(not the fetch) is appearing to load all records into memory.

if I execute

con = pymssql.connect(...)
cur = con.cursor()
cur.execute(sql)
rec = cur.fetchone()

if I put in a query which returns a lot of records into "sql" then the
execute never returns, pythons memory usage slowly ballons till the
machine cant give anymore. If I put a sql query returning only few
rows, then it works fine.

So I am not sure why an execute would feel the need to load all rows,
but its seriously crippling. Does anyone know if this is a bug or
something I can "turn off"
I ran into this myself. After some digging I discovered that what you
are after is a server-side cursor that isn't implemented yet in
pymssql. There is one in MySQLdb, but it's not the default behavior.
Regardless of your usage (fetchone vs fetchmany), the result set is
held client side. AFAIK the only workaround is to keep your result
set small (enough). If you use fetchmany and iterate over it
directly, it may keep your memory usage down, I can't remember if
that worked. I definitely tried making a generator with it, that did
not help.

Eric
Oct 20 '08 #2
Eric Wertman wrote:
>I am trying to use pymssql, and have an issue where by the execute
(not the fetch) is appearing to load all records into memory.

if I execute

con = pymssql.connect(...)
cur = con.cursor()
cur.execute(sql)
rec = cur.fetchone()

if I put in a query which returns a lot of records into "sql" then the
execute never returns, pythons memory usage slowly ballons till the
machine cant give anymore. If I put a sql query returning only few
rows, then it works fine.

So I am not sure why an execute would feel the need to load all rows,
but its seriously crippling. Does anyone know if this is a bug or
something I can "turn off"

I ran into this myself. After some digging I discovered that what you
are after is a server-side cursor that isn't implemented yet in
pymssql. There is one in MySQLdb, but it's not the default behavior.
Regardless of your usage (fetchone vs fetchmany), the result set is
held client side. AFAIK the only workaround is to keep your result
set small (enough). If you use fetchmany and iterate over it
directly, it may keep your memory usage down, I can't remember if
that worked. I definitely tried making a generator with it, that did
not help.

.... or just switch to pyodbc, for example, which behaves
perfectly well with this snippet against a table of >24 million
rows:

<code>
import pyodbc

conn = [
"Driver={SQL Server}",
"Server=SVR17",
"Database=TDI",
"TrustedConnection=Yes"
]
db = pyodbc.connect (";".join (conn))
q = db.cursor ()
q.execute ("SELECT * FROM revenue") # 24 million rows
q.fetchone ()
q.close ()

</code>
TJG
Oct 20 '08 #3
On Oct 20, 3:38*pm, Tim Golden <m...@timgolden.me.ukwrote:
Eric Wertman wrote:
I am trying to use pymssql, and have an issue where by the execute
(not the fetch) is appearing to load all records into memory.
if I execute
con = pymssql.connect(...)
cur = con.cursor()
cur.execute(sql)
rec *= cur.fetchone()
if I put in a query which returns a lot of records into "sql" then the
execute never returns, pythons memory usage slowly ballons till the
machine cant give anymore. If I put a sql query returning only few
rows, then it works fine.
So I am not sure why an execute would feel the need to load all rows,
but its seriously crippling. Does anyone know if this is a bug or
something I can "turn off"
I ran into this myself. *After some digging I discovered that what you
are after is a server-side cursor that isn't implemented yet in
pymssql. *There is one in MySQLdb, but it's not the default behavior.
Regardless of your usage (fetchone vs fetchmany), the result set is
held client side. *AFAIK the only workaround is to keep your result
set small (enough). *If you use fetchmany and iterate over it
directly, it may keep your memory usage down, *I can't remember if
that worked. *I definitely tried making a generator with it, that did
not help.

... or just switch to pyodbc, for example, which behaves
perfectly well with this snippet against a table of >24 million
rows:

<code>
import pyodbc

conn = [
* "Driver={SQL Server}",
* "Server=SVR17",
* "Database=TDI",
* "TrustedConnection=Yes"
]
db = pyodbc.connect (";".join (conn))
q = db.cursor ()
q.execute ("SELECT * FROM revenue") # 24 million rows
q.fetchone ()
q.close ()

</code>

TJG
Thanks for the responses, I am astounded any db api tool doesnt
support cursors! pymssql is mostly useless, I will switch to an odbc
interface.
Oct 21 '08 #4
On Tue, 21 Oct 2008 10:14:56 +0200, ChaosKCW <da********@gmail.comwrote:
On Oct 20, 3:38*pm, Tim Golden <m...@timgolden.me.ukwrote:
>Eric Wertman wrote:
>I am trying to use pymssql, and have an issue where by the execute
(not the fetch) is appearing to load all records into memory.
>if I execute
>con = pymssql.connect(...)
cur = con.cursor()
cur.execute(sql)
rec *= cur.fetchone()
>if I put in a query which returns a lot of records into "sql" then
the
>execute never returns, pythons memory usage slowly ballons till the
machine cant give anymore. If I put a sql query returning only few
rows, then it works fine.
>So I am not sure why an execute would feel the need to load all rows,
but its seriously crippling. Does anyone know if this is a bug or
something I can "turn off"
I ran into this myself. *After some digging I discovered that what you
are after is a server-side cursor that isn't implemented yet in
pymssql. *There is one in MySQLdb, but it's not the default behavior.
Regardless of your usage (fetchone vs fetchmany), the result set is
held client side. *AFAIK the only workaround is to keep your result
set small (enough). *If you use fetchmany and iterate over it
directly, it may keep your memory usage down, *I can't remember if
that worked. *I definitely tried making a generator with it, that did
not help.

... or just switch to pyodbc, for example, which behaves
perfectly well with this snippet against a table of >24 million
rows:

<code>
import pyodbc

conn = [
* "Driver={SQL Server}",
* "Server=SVR17",
* "Database=TDI",
* "TrustedConnection=Yes"
]
db = pyodbc.connect (";".join (conn))
q = db.cursor ()
q.execute ("SELECT * FROM revenue") # 24 million rows
q.fetchone ()
q.close ()

</code>

TJG

Thanks for the responses, I am astounded any db api tool doesnt
support cursors! pymssql is mostly useless, I will switch to an odbc
interface.
--
http://mail.python.org/mailman/listinfo/python-list
Yes this is true, unfortunately...

However, pyODBC doesn't support return variables(parameters) in stored
procedures (at least with MS SQL). pymssql is the only db api for python
that I've found that can reliably do this. I've tried adodbapi, pyodbc and
one or two others (can't think of the names now... :-/).

Regards

Nicol

--
Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo
Oct 21 '08 #5
Aspersieman wrote:
However, pyODBC doesn't support return variables(parameters) in stored
procedures (at least with MS SQL). pymssql is the only db api for python
that I've found that can reliably do this. I've tried adodbapi, pyodbc
and one or two others (can't think of the names now... :-/).
That's a fair point (I'm assuming you're right; I've never tried). That
tends to be a shortcoming of SQL interfaces. However, pymssql is based
on the defunct ntwdblib.dll interface which is becoming harder and harder
to find as of SQL2005. Have you tried putting a feature request in to
the pyodbc team? I've not had anything to do with them for a bit, but
they did implement the .nextset method at my request a couple of years
ago. (That's assuming it's even possible with ODBC in general, which
I also haven't checked).

TJG
Oct 21 '08 #6
On Tue, 21 Oct 2008 13:06:37 +0200, Tim Golden <ma**@timgolden.me.uk>
wrote:
Aspersieman wrote:
>However, pyODBC doesn't support return variables(parameters) in stored
procedures (at least with MS SQL). pymssql is the only db api for
python that I've found that can reliably do this. I've tried adodbapi,
pyodbc and one or two others (can't think of the names now... :-/).

That's a fair point (I'm assuming you're right; I've never tried). That
tends to be a shortcoming of SQL interfaces. However, pymssql is based
on the defunct ntwdblib.dll interface which is becoming harder and harder
to find as of SQL2005. Have you tried putting a feature request in to
the pyodbc team? I've not had anything to do with them for a bit, but
they did implement the .nextset method at my request a couple of years
ago. (That's assuming it's even possible with ODBC in general, which
I also haven't checked).

TJG
--

Oh, trust me - I've tried a *LOT*. I aggree, submitting a feature request
is a good idea. I think I'll do that. :)

I did read somewhere (I think on the pyodbc site) that pyodbc isn't DB API
2.0 feature complete yet - so I'll have to probably check if they haven't
already planned this.

Obtaining output parameters from stored procedures is something I have to
do quite often, unfortunately - so maybe I can submit a patch even... :)

Alternatively - I have found the adodbapi quite useful (although it also
doesn't support output parameters), so I might ask them too.

Thanks for the suggestion.

Regards

Nicol

--
Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo
Oct 21 '08 #7
Aspersieman wrote:
[... re output params in pymssql / pyodbc ...]
Oh, trust me - I've tried a *LOT*. I aggree, submitting a feature
request is a good idea. I think I'll do that. :)
Looks like someone already has:

http://sourceforge.net/tracker/index...57&atid=824257

I did read somewhere (I think on the pyodbc site) that pyodbc isn't DB
API 2.0 feature complete yet - so I'll have to probably check if they
haven't already planned this.

Obtaining output parameters from stored procedures is something I have
to do quite often, unfortunately - so maybe I can submit a patch even... :)

I'm sure that will be more than welcome. Looks like it's a
one-man band (as I guess most projects are) and there aren't
always enough minutes in the day.

TJG
Oct 21 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Khawaja Shahzad Sadiq Butt | last post by:
Hi, I am trying to connect to sql server 2000 on win xp. I am using a python extension called. pymssql but it is giving me this errror: Traceback (most recent call last): File...
0
by: Josh Close | last post by:
I'm getting an error from pymssql when trying to execute a stored procedure: Operating-system Error - OpenClient I can't figure out why this is happening. It's probably a freetds problem (like...
0
by: saqib | last post by:
I have a query that is running on a mssql DB that takes about 15 minutes when run "directly"; IE: thru MS SQL Query Analyzer. I know, it sucks. I'm trying to fix it. But in the meantime, I have...
8
by: johnlichtenstein | last post by:
I am using cx_Oracle and MySQLdb to pull a lot of data from some tables and I find that the cursor.execute method uses a lot of memory that never gets garbage collected. Using fetchmany instead of...
1
by: m.biddiscombe | last post by:
Hi, I'm trying to use pymssql to execute a stored procedure. Currently, I have an Excel spreadsheet that uses VBA in this manner: Private Function CreateNewParrot(connDb As ADODB.Connection)...
3
by: rc | last post by:
How to insert NULL values in to int field using params. I'm trying to use pymssql.execute, passing the operation and list of params. One of the values in the params is a NULL value going to int...
4
by: gdyren | last post by:
the code is below: import pymssql conn = pymssql.connect(host = "121.198.126.233",user = "xxxx",password = "xxxx",database = "test") print "connecting success" cursor = conn.cursor()...
0
by: Aspersieman | last post by:
On Tue, 21 Oct 2008 20:44:24 +0200, Mike Hansen <projecktzero@yahoo.com> wrote: Hi there I hope you don't mind - I made this a new thread on the list, so others can maybe help out. ...
1
by: Darren Mansell | last post by:
Hi. I'm relatively new to python so please be gentle :) I'm trying to write a £ symbol to an MS SQL server using pymsssql . This works but when selecting the data back (e.g. using SQL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.