我需要使用 django reset_queries()
我正在使用 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)
我的问题是:我需要使用 reset_queries()
和或 connection.close()
这是内存使用量增加的原因吗?
i am using django 1.3 and i am running a script outside of a web context (from command line).
my code keep reading 10000 entries from the db each time.
i noticed that the memory usage of the process is getting bigger over time.
my code is:
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)
my question is: do i need to use reset_queries()
and or connection.close()
and is this is the reason for the increase in memory usage ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我首先使用 only 或 defer 查询中的方法。这两个用于仅检索您实际需要的字段,而不是所有字段。您的查询会稍微快一些并且消耗更少的内存,因为不会从数据库中获取不需要的字段。
I would start with using only or defer methods in your query. These two are used to retrieve only the fields that you actually need, instead of all fields. Your query will be slightly faster and consume less memory, because not needed fields will not be fetched from the database.