[Answered ]-Bad Performance in my code

2👍

You can create a dictionary of all User and Provider objects, indexed by what you’re interested in, like this:

users = dict([(u.username, u) for u in User.objects.all()])
providers = dict([(p.name, p) for p in Provider.objects.all()])

(Make sure you do this outside the for call in result: for loop!) You can then change your slow queries to:

                    rate_cust = users[call[3]]
                    rate_prov = provided[call[5]]

I’m guessing there are considerably fewer users and providers than calls, which means that keeping them in a dictionary will be much faster to access than making one query for each call.

Leave a comment