如何使Django QuerySet批量删除()更有效率 [英] How to make Django QuerySet bulk delete() more efficient
问题描述
设置:
Django 1.1.2,MySQL 5.1
Setup:
Django 1.1.2, MySQL 5.1
问题:
Blob.objects.filter(foo = foo) \
.filter(status = Blob.PLEASE_DELETE) \
.delete()
此代码段导致ORM首先从xxx_blob生成 SELECT * ...
查询,然后从xxx_blob中执行 DELETE,其中id(BLAH);
其中BLAH是一个可笑的长列表的id。由于我删除了大量的blob,所以我和DB都很不开心。
This snippet results in the ORM first generating a SELECT * from xxx_blob where ...
query, then doing a DELETE from xxx_blob where id in (BLAH);
where BLAH is a ridiculously long list of id's. Since I'm deleting a large amount of blobs, this makes both me and the DB very unhappy.
是否有原因?我不明白为什么ORM无法将上述代码段转换为单个DELETE查询。有没有办法优化这个而不诉诸原始的SQL?
Is there a reason for this? I don't see why the ORM can't convert the above snippet into a single DELETE query. Is there a way to optimize this without resorting to raw SQL?
推荐答案
不是没有编写自己的自定义SQL或经理或某事;他们显然正在努力。
Not without writing your own custom SQL or managers or something; they are apparently working on it though.
http: //code.djangoproject.com/ticket/9519
这篇关于如何使Django QuerySet批量删除()更有效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!