如何从内存中删除 Django 的 BinaryField?
How to remove Django`s BinaryField from memory?
我有一个 Django 模型 BinaryField
:
class MyData(models.Model):
someData = models.CharField()
data = models.BinaryField()
在我的应用程序中,我需要遍历一大组行。当然,我将二进制字段标记为延迟:
myDataList = MyData.objects().filter(...).defer('data')
for myData in myDataList:
doSmthWithData(myData.data)
...
初始请求的结果需要少量内存。但是当我遍历查询集时,我从数据库中获取二进制数据。由于二进制字段的大小,内存很快就会耗尽。
我们可以做一个释放内存的技巧 - 在循环结束时将 data
设置为 None
:myData = None
。但是在这种情况下修改和保存实体我需要从数据库中再次查询它,否则二进制数据将丢失。
我们还有其他方法可以从内存中删除二进制数据吗?类似于:set_defer(myData.data)
像这样使用 iterator。
myDataList = MyData.objects().filter(...).defer('data')
for myData in myDataList.iterator():
doSmthWithData(myData.data)
Evaluates the QuerySet (by performing the query) and returns an iterator (see PEP 234) over the results. A QuerySet typically caches its results internally so that repeated evaluations do not result in additional queries. In contrast, iterator() will read results directly, without doing any caching at the QuerySet level (internally, the default iterator calls iterator() and caches the return value). For a QuerySet which returns a large number of objects that you only need to access once, this can result in better performance and a significant reduction in memory.
我有一个 Django 模型 BinaryField
:
class MyData(models.Model):
someData = models.CharField()
data = models.BinaryField()
在我的应用程序中,我需要遍历一大组行。当然,我将二进制字段标记为延迟:
myDataList = MyData.objects().filter(...).defer('data')
for myData in myDataList:
doSmthWithData(myData.data)
...
初始请求的结果需要少量内存。但是当我遍历查询集时,我从数据库中获取二进制数据。由于二进制字段的大小,内存很快就会耗尽。
我们可以做一个释放内存的技巧 - 在循环结束时将 data
设置为 None
:myData = None
。但是在这种情况下修改和保存实体我需要从数据库中再次查询它,否则二进制数据将丢失。
我们还有其他方法可以从内存中删除二进制数据吗?类似于:set_defer(myData.data)
像这样使用 iterator。
myDataList = MyData.objects().filter(...).defer('data')
for myData in myDataList.iterator():
doSmthWithData(myData.data)
Evaluates the QuerySet (by performing the query) and returns an iterator (see PEP 234) over the results. A QuerySet typically caches its results internally so that repeated evaluations do not result in additional queries. In contrast, iterator() will read results directly, without doing any caching at the QuerySet level (internally, the default iterator calls iterator() and caches the return value). For a QuerySet which returns a large number of objects that you only need to access once, this can result in better performance and a significant reduction in memory.