Skip to content

Commit f87ff24

Browse files
committed
Avoid unnecessary db query on index update
pks_seen is only needed if objects are removed from index, so only compute it if necessary. Improve pks_seen to not build an intermediary list.
1 parent 7263866 commit f87ff24

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

haystack/management/commands/update_index.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ def update_backend(self, label, using):
243243
if self.verbosity >= 1:
244244
print u"Indexing %d %s" % (total, force_unicode(model._meta.verbose_name_plural))
245245

246-
pks_seen = set([smart_str(pk) for pk in qs.values_list('pk', flat=True)])
247246
batch_size = self.batchsize or backend.batch_size
248247

249248
if self.workers > 0:
@@ -267,8 +266,10 @@ def update_backend(self, label, using):
267266
# They're using a reduced set, which may not incorporate
268267
# all pks. Rebuild the list with everything.
269268
qs = index.index_queryset().values_list('pk', flat=True)
270-
pks_seen = set([smart_str(pk) for pk in qs])
269+
pks_seen = set(smart_str(pk) for pk in qs)
271270
total = len(pks_seen)
271+
else:
272+
pks_seen = set(smart_str(pk) for pk in qs.values_list('pk', flat=True))
272273

273274
if self.workers > 0:
274275
ghetto_queue = []

0 commit comments

Comments
 (0)