Python 我需要使用django reset_查询()吗

Python 我需要使用django reset_查询()吗,python,django,orm,django-models,memory-leaks,Python,Django,Orm,Django Models,Memory Leaks,我使用的是django 1.3,在web上下文之外运行脚本(从命令行)。 我的代码每次从数据库中读取10000个条目。 我注意到,随着时间的推移,进程的内存使用量越来越大。 我的代码是: def getData(startIndex,chunkSize): dataList =Mydata.objects.filter(update_date__isnull = True)[startIndex:startIndex+chunkSize] return list(dataList

我使用的是django 1.3,在web上下文之外运行脚本(从命令行)。
我的代码每次从数据库中读取10000个条目。
我注意到,随着时间的推移,进程的内存使用量越来越大。
我的代码是:

def getData(startIndex,chunkSize):
    dataList =Mydata.objects.filter(update_date__isnull = True)[startIndex:startIndex+chunkSize]
    return list(dataList)

if __name__ == '__main__':
   chunkSize = 10000
   startIndex = 0
   dataSize = Mydata.objects.filter(update_date__isnull = True).count()
   while startIndex < dataSize:
       dataList = getData(startIndex,chunkSize)
       startIndex += chunkSize
       do_stuff(dataList)
def getData(startIndex,chunkSize):
dataList=Mydata.objects.filter(update\u date\u isnull=True)[startIndex:startIndex+chunkSize]
返回列表(数据列表)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
chunkSize=10000
startIndex=0
dataSize=Mydata.objects.filter(update\u date\u isnull=True).count()
当startIndex<数据大小:
dataList=getData(startIndex,chunkSize)
startIndex+=块大小
do_stuff(数据列表)
我的问题是:我是否需要使用
reset\u querys()
和/或
connection.close()


这就是内存使用量增加的原因吗

我将从在查询中使用或方法开始。这两个字段仅用于检索实际需要的字段,而不是检索所有字段。您的查询速度会稍快一些,占用的内存也会更少,因为不需要的字段不会从数据库中提取。

请检查:您是否有
DEBUG=False
?确实是python进程占用了更多内存吗?你能一步一步地看一下脚本,看看哪些行导致了内存的增加吗?是python进程占用了内存。但它可能不是查询部分,也可能是do_stuff部分,我只是想确保不需要重置_queries()。另外,试着使用
queryset.iterator()
而不是
list(queryset)
一次只显式读取一条记录。