Closed bbelderbos closed 8 months ago
ok I will definitely watch the n+1 video as it seems to keep coming up--
Interestingly no matter how many search terms I give the debug toolbar says each search is only 3 queries
Because search terms is a limited amount?
for term in search_terms[1:]:
term_query = tunes.filter(...
That's then not that bad actually, but ideally we try to make one query give you can &
several Q
s.
I am working on this, but a couple further points:
Though this looks like three database hits, in fact it hits the database only once, at the last line (print(q)). In general, the results of a QuerySet aren’t fetched from the database until you “ask” for them. When you do, the QuerySet is evaluated by accessing the database. For more details on exactly when evaluation takes place, see When QuerySets are evaluated.
Haven't read that last link yet but will look once I've gotten things cleaned up a bit
Okay, so the three queries I mentioned (learned this from your n+1 video) are actually just one tune query and two django/allauth queries.
Even when you pass four search terms to the basic search it still runs it in only one query.
I think I'm going to hold off on changing this search tonight and we can just talk about it tomorrow. Definitely in terms of readability and elegance the code needs a rework but it seems that functionally it's already pretty efficient? Thanks to lazy query sets!
Also worth mentioning that I didn't know I was taking advantage of lazy query sets at the time, I thought I was writing the inefficient thing of making three separate queries and then combining them
Good find and sorry for not checking/thinking about the standard auth_user + django_session query that happen so your code indeed was just making one query - great!
So yes you did it in an efficient manner and it's nice that Django has us do the right thing intuitively :)
Only refactoring left is removing the if / else in favor of a single query code block, but that's less urgent right now (I updated the title of this issue).
Q
queries into single query using&
_Originally posted by @bbelderbos in https://github.com/jwjacobson/jazz_repertoire/pull/101#discussion_r1432887170_