解决mysqldb查询大量数据导致内存使用过高的问题
connection=MySQLdb.connect(
host="thehost",user="theuser",
passwd="thepassword",db="thedb")
cursor=connection.cursor()
cursor.execute(query)
for row in cursor.fetchall():
print(row)
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)
- BaseCursor
- The base class for Cursor objects. This does not raise Warnings.
- CursorStoreResultMixIn
- Causes the Cursor to use the mysql_store_result() function to get the query result. The entire result set is stored on the client side.
- CursorUseResultMixIn
- Causes the cursor to use the mysql_use_result() function to get the query result. The result set is stored on the server side and is transferred
row by row using fetch operations. - CursorTupleRowsMixIn
- Causes the cursor to return rows as a tuple of the column values.
CursorDictRowsMixIn
Causes the cursor to return rows as a dictionary, where the keys are column names and the values are column values. Note that if the column names are not unique, i.e., you are selecting from two tables that share column
names, some of them will be rewritten as table.column. This can be avoided by using the SQL ASkeyword.
(This is yet-another reason not to use * in SQL queries, particularly where JOIN is
involved.)
- Cursor
- The default cursor class. This class is composed of CursorWarningMixIn, CursorStoreResultMixIn, CursorTupleRowsMixIn, and BaseCursor,
i.e. it raises Warning, usesmysql_store_result(), and returns rows as tuples. - DictCursor
- Like Cursor except it returns rows as dictionaries.
- SSCursor
- A "server-side" cursor. Like Cursor but uses CursorUseResultMixIn.
Use only if you are dealing with potentially large result sets. - SSDictCursor
- Like SSCursor except it returns rows as dictionaries.
解决mysqldb查询大量数据导致内存使用过高的问题的更多相关文章
- POI读写大数据量excel,解决超过几万行而导致内存溢出的问题
1. Excel2003与Excel2007 两个版本的最大行数和列数不同,2003版最大行数是65536行,最大列数是256列,2007版及以后的版本最大行数是1048576行,最大列数是16384 ...
- NPOI解决由于excel删除数据导致空行读取问题
1.解决问题思路一:申明判断是否空行变量用于判断是否空行,声明变量数组用于临时非空行数据,最后存于datatable中. /// <summary>读取excel, /// 默认第一行为表 ...
- centos7 未启用swap导致内存使用率过高。
情况描述: 朋友在阿里云上有一台系统为CentOS7的VPS,内存为2GB,用于平时开发自己的项目时测试使用: 他在上面运行了5个docker实例,运行java程序:还有一个mysql服务: 上述5个 ...
- php查询mysql返回大量数据结果集导致内存溢出的解决方法
web开发中如果遇到php查询mysql返回大量数据导致内存溢出.或者内存不够用的情况那就需要看下MySQL C API的关联,那么究竟是什么导致php查询mysql返回大量数据时内存不够用情况? 答 ...
- 查询执行成本高(查询访问表数据行数多)而导致实例 CPU 使用率高是 MySQL 非常常见的问题
MySQL CPU 使用率高的原因和解决方法_产品性能_常见问题_云数据库 RDS 版-阿里云 https://help.aliyun.com/knowledge_detail/51587.html ...
- es实战之查询大量数据
背景 项目中已提供海量日志数据的多维实时查询,客户提出新需求:将数据导出. 将数据导出分两步: 查询大量数据 将数据生成文件并下载 本文主要探讨第一步,在es中查询大量数据或者说查询大数据集. es支 ...
- Windows Server 2008 R2服务器内存使用率过高,但与任务管理器中进程占用内存和不一致
系统环境: Windows Server 2008 R2 + Sql Server 2008 R2 问题描述: Windows Server 2008 R2系统内存占用率过大,而在任务管理器中各进 ...
- WPF循环加载图片导致内存溢出的解决办法
程序场景:一系列的图片,从第一张到最后一张依次加载图片,形成“动画”. 生成BitmapImage的方法有多种: 1. var source=new BitmapImage(new Uri(" ...
- XCode编译文件过多导致内存吃紧解决方法
XCode编译文件过多导致内存吃紧解决方法 /Users/~~/Library/Developer/Xcode/DerivedData 1) 然后 找到编译文件 删除 就好了哦 快去试试看吧
随机推荐
- 更改Oracle实例的字符集
(1).数据库服务器字符集select * from nls_database_parameters 来源于props$,是表示数据库的字符集. (2).服务端字符集环境select * from n ...
- AntiXSS - 支持Html同时防止XSS攻击
AntiXSS - 支持Html同时防止XSS攻击 跨站脚本攻击(XSS)已经不是什么新鲜的话题了,甚至很多大公司也为此吃尽苦头.最简单直接的防范方法,就是不允许任何html标签输入,对用户输入进行编 ...
- 【转】Acm之java速成
这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入:格式为:Scanner cin = new Sca ...
- iOS_25彩票_幸运转盘
终于效果图: 各个view的关系图: 背景圆盘(须要扣图处理)LuckyBaseBackground.png 盖在背景圆盘上面的转盘 LuckyRotateWheel.png watermark/2/ ...
- iis 隐藏 banner
去微软官网下载这个补丁 http://www.microsoft.com/en-us/search/DownloadResults.aspx?q=urlscan+3.1 2. 安装urlscan_ ...
- HTML4和HTML5之间10主要差异
HTML5恐怕要让部分网页工作者抓狂了,HTML5将採用标准的XML语法格式,这对代码的规范要求很高. HTML5是最新的HTML标准.尽管还在制定.但或迟或早,全部的web程序猿都会发现须要使用到这 ...
- Javascript Base64编码与解码
原文:[转]Javascript Base64编码与解码 <html> <head> <META HTTP-EQUIV="MSThemeCompatible&q ...
- C#之再议数组和集合
1.数组 1.1简单数组 1.2 多维数组 1.3锯齿数组 1.4Array数组 1.5作为参数的数组 1.6枚举 1.7结构 以上部分可参考 http://www.cnblogs.com/ztb12 ...
- net Mvc模块化开发
Asp.net Mvc模块化开发之“部分版本部分模块更新(上线)” 项目开发从来就不是一个简单的问题.更难的问题是维护其他人开发的项目,并且要修改bug.如果原系统有重大问题还需要重构. 怎么重构系统 ...
- 《Effective C++ 》学习笔记——规定10
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...