Home > Mobile >  How do I optimize ORM queries for faster results?
How do I optimize ORM queries for faster results?

Time:04-09

I have 80k Entries in table Calendar. When I run either of the following 2 methods, the filter or get is taking too long to execute and due to which server is getting crashed after sometime and no new entries are getting added. I want to know if there are any more methods from which I can solve this issue.

Method 1:

date_list = []
d1 = date(2022, 5, 1)
d2 = date(2022, 6, 30)
delta = d2 - d1
for i in range(delta.days   1):
    date_list.append(d1   timedelta(days=i))
profiles = Profile.objects.all()
for j in date_list:
    for i in profiles:
        try:
            Calendar.objects.get(date=j,emp_id = i.emp_id)
        except Calendar.DoesNotExist:
            e = Calander()
            e.team = i.emp_process
            e.date = j
            e.emp_name = i.emp_name
            e.emp_id = i.emp_id
            e.emp_desi = i.emp_desi
            e.att_actual = "Unmarked"
            e.save()

Method 2:

date_list = []
d1 = date(2022, 5, 1)
d2 = date(2022, 6, 30)
delta = d2 - d1
for i in range(delta.days   1):
    date_list.append(d1   timedelta(days=i))
profiles = Profile.objects.all()

for j in date_list:
    for i in profiles:
        cal = Calander.objects.filter(date=j,emp_id = i.emp_id).count()
        if cal < 1:
            e = Calander()
            e.team = i.emp_process
            e.date = j
            e.emp_name = i.emp_name
            e.emp_id = i.emp_id
            e.emp_desi = i.emp_desi
            e.att_actual = "Unmarked"
            e.save()

CodePudding user response:

Try this:

date_list = []
d1 = date(2022, 5, 1)
d2 = date(2022, 6, 30)
delta = d2 - d1
for i in range(delta.days   1):
    date_list.append(d1   timedelta(days=i))

for j in date_list:
    profiles = Profile.objects.exclude(emp_id__in=Calendar.objects.filter(date=j).values('emp_id'))
    calendars = []
    for i in profiles:
        e = Calendar()
        e.team = i.emp_process
        e.date = j
        e.emp_name = i.emp_name
        e.emp_id = i.emp_id
        e.emp_desi = i.emp_desi
        e.att_actual = "Unmarked"
        calendars.append(e)
    Calendar.objects.bulk_create(calendars)
  • Related