Short answer is – benchmark. Test it in different situations and measure the load. It will be the best answer.
There can't be duplicates here. Is it really a problem to check for two situalions? Here's the hypotetic code: def average_score(self, user_id_list=None): qset = self.
Test_attempts.all() if user_id_list is not None: qset = qset. Filter(profile__id__in=user_id_list) scores = x.score() for x in qset # and compute the average I don't know what does score method do, but can't you compute the average at DB level? It will give you much more noticable perfomance boost.
And don't forget about caching.
From what I understand of the documentation, all queries are built before they are actually used. So, for instance, test_attempts.all() generates SQL code once and when you execute the query, actually get data by doing something like .count(), for t in test_attempts.all():, etc. , it runs the query on the database and returns a Queryset object or just an Object if you used get(). With that in mind, the number of calls to the database would be exactly the same, while the actual call would be different.
As you show in your edited post, the raw queries are different, but they are both generated in the same way, before the data is access by Django. From a Django perspective, they would both be created in the same fashion, and then executed on the database.In my opinion, it would be best not to test for an all() situation, as you would have to run TWO queries to determine that. I believe you should run with the code you have and skip checking for the all() scenario, which you describe as the most common case.
Most modern database engines run the queries in such a way that added joins do not hamper the performance metrics, as they process queries in optimal sequences, anyway.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.