Why accessing Django QuerySet became very slow?

Viewed 1047

I have a model query in Django:

Query = Details.objects.filter(name__iexact=nameSelected)

I filter it later:

Query2 = Query .filter(title__iexact=title0)

Then I access it using:

...Query2[0][0]...

A few days ago it worked very fast. But now it became at least 20 times slower. I test it on other PC, it works very fast.

Update: filtering is not the reason of the delay, Query[0][0] is the reason. Besides that, it became super slow suddenly not over time.

What can make it so slow on my first PC?

2 Answers

Maybe you could try to make a list out of the Queryset when you create it so that you have a real list not only a lazy QS

Query2 = list(Query .filter(title__iexact=title0))

The best way is to avoid loop for filtering the query. What I did is to create a hashmap dictionary

dict0 = {}

Then I added list of items and data that corresponds to that item in the query:

dict0 = dict(zip(title0List, DataList))

Finally I use dict0 instead of query, It boosts the speed at least 10 times for me)

Related