Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102954

Re: fetchall is taking much longer time while getting data from Sybase module in Python

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: fetchall is taking much longer time while getting data from Sybase module in Python
Date Mon, 15 Feb 2016 10:25:46 +0100
Organization None
Lines 63
Message-ID <mailman.136.1455528359.22075.python-list@python.org> (permalink)
References <f27cefdf-ead3-4331-9a98-d3a5793a6fae@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de hoVAQyATH9BScwEVZn8yMAcMRxvxBXK7gkY5QPZwQJwg==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'finally:': 0.05; 'none:': 0.05; 'none)': 0.07; 'subject:getting': 0.07; 'cursor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:module': 0.09; 'subject:while': 0.09; 'python.': 0.11; 'def': 0.13; 'pdb': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'retrieving': 0.16; 'row': 0.16; 'true:': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'all,': 0.20; 'import': 0.24; 'module': 0.25; 'script': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'yield': 0.27; 'record': 0.29; 'guarantees': 0.29; 'print': 0.30; 'query': 0.30; 'probably': 0.31; 'run': 0.33; 'skip:- 10': 0.34; 'subject:time': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'turn': 0.37; 'received:org': 0.37; 'test': 0.39; 'data': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'complete': 0.63; 'overall': 0.72; 'transfer': 0.73; 'subject:much': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd81b4.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102954

Show key headers only | View raw


reetesh nigam wrote:

> Hi All,
> 
> I am retrieving data from Sybase database using Sybase module of Python.
> My query is not taking time however fecthall is taking longer time.
> 
> Below is the test script :
> 
> def run_query(db,query): ## Run query and resturn record result
>         t1 = datetime.now()
>         cursorObj = db.cursor()
>         t2 = datetime.now()
>         cursorObj.execute(query)
>         t3 = datetime.now()
>         import pdb
>         pdb.set_trace()
>         rowset = cursorObj.fetchall()
>         t4 = datetime.now()
>         cursorObj.close()
>         print "Time taken to make cursor --%s"%(t2-t1)
>         print "Time taken to execute query --%s"%(t3-t2)
>         print "Time taken for fetchall--%s"%(t4-t3)
>         return rowset
> 
> Output:
> Time taken to make cursor --0:00:00.000037
> Time taken to execute query --0:00:00.379443
> Time taken for fetchall--0:00:14.739064

fetchall() probably has to transfer a lot of rows. If you want to process 
them one at a time you can turn run_query into a generator

def run_query(db, query):
   ...
   while True:
       row = cursor.fetchone()
       if row is None: break
       yield row
   ...

A complete implementation that guarantees that the cursor is closed might 
look like this (untested):

@contextlib.contextmanager
def run_query(db, query):
    cursor = db.cursor()
    try:
        cursor.execute(query)
        yield iter(cursor.fetchone, None)
    finally:
        cursor.close()

# use it
with run_query(db, query) as rows:
    for row in rows:
        print row

This is likely to *increase* the overall time taken, but should drastically 
reduce the time you have to wait for the first record to be printed, i. e. 
the latency.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

fetchall is taking much longer time while getting data from Sybase module in Python reetesh nigam <nigamreetesh84@gmail.com> - 2016-02-14 22:07 -0800
  Re: fetchall is taking much longer time while getting data from Sybase module in Python Peter Otten <__peter__@web.de> - 2016-02-15 10:25 +0100

csiph-web