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()