解决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)

作者    :秋时

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

原文地址:https://www.cnblogs.com/Netsharp/p/8361648.html