zoukankan      html  css  js  c++  java
  • 解决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)

    作者    :秋时

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

  • 相关阅读:
    uva299 Train Swapping
    uva 10106 Product
    uva 340 MasterMind Hints
    uva 10115 Automatic Editing
    uva748 Exponentiation
    uva152 Tree's a Crowd
    uva 10420 List of Conquests
    uva 644 Immediate Decodability
    要知其所以然的学习(转载)
    持有书籍统计
  • 原文地址:https://www.cnblogs.com/Netsharp/p/8361648.html
Copyright © 2011-2022 走看看