How to efficiently use MySQLDB SScursor?

后端 未结 3 616
再見小時候
再見小時候 2020-11-28 07:44

I have to deal with a large result set (could be hundreds thousands of rows, sometimes more).
They unfortunately need to be retrieved all at once (on start up).

相关标签:
3条回答
  • 2020-11-28 08:00

    I am in agreement with Otto Allmendinger's answer, but to make explicit Denis Otkidach's comment, here is how you can iterate over the results without using Otto's fetch() function:

    import MySQLdb.cursors
    connection=MySQLdb.connect(
        host="thehost",user="theuser",
        passwd="thepassword",db="thedb",
        cursorclass = MySQLdb.cursors.SSCursor)
    cursor=connection.cursor()
    cursor.execute(query)
    for row in cursor:
        print(row)
    
    0 讨论(0)
  • 2020-11-28 08:10

    Definitely use the SSCursor when fetching big result sets. It made a huge difference for me when I had a similar problem. You can use it like this:

    import MySQLdb
    import MySQLdb.cursors
    
    connection = MySQLdb.connect(
            host=host, port=port, user=username, passwd=password, db=database, 
            cursorclass=MySQLdb.cursors.SSCursor) # put the cursorclass here
    cursor = connection.cursor()
    

    Now you can execute your query with cursor.execute() and use the cursor as an iterator.

    Edit: removed unnecessary homegrown iterator, thanks Denis!

    0 讨论(0)
  • 2020-11-28 08:13

    Alternatively, you can use SSCursor outside the connection object (it is pretty important when you already define connection and dont want all the connection use SSCursor as a cursorclass).

    import MySQLdb
    from MySQLdb.cursors import SSCursor # or you can use SSDictCursor
    
    connection = MySQLdb.connect(
            host=host, port=port, user=username, passwd=password, db=database)
    cursor = SSCursor(connection)
    cursor.execute(query)
    for row in cursor:
        print(row)   
    
    0 讨论(0)
提交回复
热议问题