Try to search your question here, if you can't find : Ask Any Question Now ?

How to reduce the number of database hits while querying in Django

HomeCategory: stackoverflowHow to reduce the number of database hits while querying in Django
kundan asked 2 weeks ago

I have three tables

1. User, 2. Device and 3. Log

I want filter the logs based on devices and logs. I’m using the following querying which iterates over the users and devices in order to get the logs. I feel this will become a performance hit. Can anyone suggest me a way to reduce the number of database hits.

for user_obj in User.objects.all():
    device_qs = Device.objects.filter(user=user_obj)
    if device_qs.exists():
        for device_obj in device_qs:
            log_count = Log.objects.filter(user=user_obj, device=device_obj, created_at__range(from_date, to_date)).count()
1 Answers
Best Answer
Fernando answered 2 weeks ago
Your Answer

8 + 15 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com