From 6c893491948cbdb2ef88533c36ab7f982569fcea Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Wed, 6 Mar 2024 15:22:55 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=BC=9A=E8=AF=9D=20?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=AE=B0=E5=BD=95=20=E5=88=86=E7=89=87?= =?UTF-8?q?=E5=88=A0=E9=99=A4=20(#12763)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- apps/audits/tasks.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/audits/tasks.py b/apps/audits/tasks.py index 74332f9ca..f16a792a6 100644 --- a/apps/audits/tasks.py +++ b/apps/audits/tasks.py @@ -86,9 +86,12 @@ def clean_celery_tasks_period(): def batch_delete(queryset, batch_size=3000): + model = queryset.model + count = queryset.count() with transaction.atomic(): - for i in range(0, queryset.count(), batch_size): - queryset[i:i + batch_size].delete() + for i in range(0, count, batch_size): + pks = queryset[i:i + batch_size].values_list('id', flat=True) + model.objects.filter(id__in=list(pks)).delete() def clean_expired_session_period():