解决mysqldb查询大量数据导致内存使用过高的问题

来源:http://blog.csdn.net/jianhong1990/article/details/41209493

------------------------------------------------------------------------

1.源代码

connection=MySQLdb.connect(
    host="thehost",user="theuser",
    passwd="thepassword",db="thedb")
cursor=connection.cursor()
cursor.execute(query)
for row in cursor.fetchall():
    print(row)

 

2.问题
普通的操作无论是fetchall()还是fetchone()都是先将数据载入到本地再进行计算,大量的数据会导致内存资源消耗光。解决办法是使用SSCurosr光标来处理。

3.优化后的代码  
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)

 

posted on 2018-01-26 21:16  Netsharp  阅读(144)  评论(0编辑  收藏  举报

导航