Last active
November 14, 2024 08:39
-
-
Save victorono/cd9d14b013487b8b22975512225c5b4c to your computer and use it in GitHub Desktop.
Django - remove duplicate objects where there is more than one field to compare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models import Count, Max | |
unique_fields = ['field_1', 'field_2'] | |
duplicates = ( | |
MyModel.objects.values(*unique_fields) | |
.order_by() | |
.annotate(max_id=Max('id'), count_id=Count('id')) | |
.filter(count_id__gt=1) | |
) | |
for duplicate in duplicates: | |
( | |
MyModel.objects | |
.filter(**{x: duplicate[x] for x in unique_fields}) | |
.exclude(id=duplicate['max_id']) | |
.delete() | |
) |
Thanks a lot! Excellent dimensioning to improve execution performance
Great starting point. Tried to reduce the number of queries without using raw SQL.
def remove_duplicates_from_table(model, lookup_fields):
duplicates = (
model.objects.values(*lookup_fields)
.order_by()
.annotate(min_id=Min('id'), count_id=Count('id'))
.filter(count_id__gt=1)
)
fields_lookup = Q()
duplicate_fields_values = duplicates.values(*lookup_fields)
for val in duplicate_fields_values:
fields_lookup |= Q(**val)
min_ids_list = duplicates.values_list('min_id', flat=True)
if fields_lookup:
model.objects.filter(fields_lookup).exclude(id__in=min_ids_list).delete()
Ended up using Q object to avoid making select query in each iteration while looping over the duplicates
list.
Thank you so much! This is EXACTLY what I needed to fix an issue with one of my migrations taking forever.
@ahmed-zubair-1998 thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When you have lots of duplicates this becomes very slow. Imagine a table where every row is duplicated, so that you have
table_size/2
items in theduplicates
QuerySet after the first query and then need to do the delete for each of those one by one.It gave a really good starting point though. This is what I ended up with, does it all in one query. Running time went from hours to minutes on a large table.