简体   繁体   中英

How to minimize time of execution

I have this loop:

start = datetime.now()
for entry in entries:
    related_entries = Related.objects.filter(entry_id=entry.entry_id, status='Related')
    lead_related_entries.append({'Entry': entry, 'related_entries': related_entries})
end = datetime.now()
print (end - start)

I have almost 7000+ entry with Execution time:

0:00:10.377000

Is there any tasks to do to make it faster ?

One thing that should make your query running faster is to use Lazy loading , so you only load the field you need initially. If you need more fields later, Django will be clever enough to fetch them for you when you invoke the field.

Reference: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.only

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM