我正在对一个表执行查询,在该表中我得到一个包含数亿行的列,这是因为我想将它们绘制在直方图中。问题是这几乎耗尽了我所有的内存(7.8 gb)和我所有的交换内存(8gb),然后程序在 cur.fetchall() 完成之前以退出代码 -9 退出。
我怎样才能防止这种情况发生?我应该先对我的列进行排序,然后对它的大块进行几次查询 - 还是有更好的方法来获取我的查询中的数据?cur.execute 本身几乎不需要时间。
#!/usr/bin/python
import sqlite3 as lite
import numpy as np
import sys
import os
import matplotlib.pyplot as plt
def getQuantity(databasepath):
con = lite.connect(databasepath)
binwidth = 1
start = time.time()
with con:
cur = con.cursor()
cur.execute('SELECT latitude FROM MessageType1')
con.commit()
latitudes = cur.fetchall() #Breakdown here
latitudes = [x[0] for x in latitudes]
plt.hist(latitudes, bins=range(int(min(latitudes)), int(max(latitudes)) + binwidth, binwidth))
plt.title("Bucket size: " + str(binwidth))
plt.ylabel("Number of message")
plt.savefig('latlongstats'+'t'+str(time.strftime("%H:%M:%S")), format='png')
if __name__ == "__main__":
getQuantity('database/database.db')