Skip to content

Commit 5750774

Browse files
craigmaloneyRoss Mechanic
authored andcommitted
Filter the records when given a minutes argument (#606)
* Fixes OOM issue when cleaning over 8M records * Adding name to Authors * Added changes to Changelog * Removing redundant .all() * Removing unnecessary typo. * Filter records if given a minutes argument. * Updated changelog * Adding black formatting to unrelated files * Removing commit, cleaning up changelog
1 parent 15be12e commit 5750774

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
Unreleased
5+
----------
6+
- Add simple filtering if provided a minutes argument in `clean_duplicate_history` (gh-606)
7+
48
2.8.0 (2019-12-02)
59
------------------
610
- Fixed `bulk_create_with_history support` for HistoryRecords with `relation_name` attribute (gh-591)

simple_history/management/commands/clean_duplicate_history.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,17 @@ def _process(self, to_process, date_back=None, dry_run=True):
6565
if not found:
6666
continue
6767

68-
# it would be great if we could just iterate over the instances that
69-
# have changes (in the given period) but
70-
# `m_qs.values(model._meta.pk.name).distinct()`
71-
# is actually slower than looping all and filtering in the code...
72-
for o in model.objects.iterator():
68+
# Break apart the query so we can add additional filtering
69+
model_query = model.objects.all()
70+
71+
# If we're provided a stop date take the initial hit of getting the
72+
# filtered records to iterate over
73+
if stop_date:
74+
model_query = model_query.filter(
75+
pk__in=(m_qs.values_list(model._meta.pk.name).distinct())
76+
)
77+
78+
for o in model_query.iterator():
7379
self._process_instance(o, model, stop_date=stop_date, dry_run=dry_run)
7480

7581
def _process_instance(self, instance, model, stop_date=None, dry_run=True):

0 commit comments

Comments
 (0)