+4  A: 

filter itself doesn't execute a query, no query is executed until you explicitly fetch items from query (e.g. get), and list( query ) also executes it.

hasen j
Ok, so if this is true, it doesnt query the DB before the view get exectuted? So that when i actually queries the DB it is already filtered down? Because i am a little bit afraid of the overhead that comes with the Soknad.objects.all() at the beginning.
Espen Christensen
Yes, this is correct. Django querysets are lazy, and are not executed until you actually access a result row (by slicing or iterating). So the only potential performance issues with your code are with the SQL generated by the final (single) database query.
Carl Meyer
Aha, thanks for the info Carl, this is great, so as long as the final resulting query is good, wich this might not be, it is good. I was afraid that i first did a query and got all objects, then filtered, and did a new query and so on, and that wouldnt be to good :)
Espen Christensen
+2  A: 

You can see the query that will be generated by using:

soknad_list.query.as_sql()[0]

You can then put that into your database shell to see how long the query takes, or use EXPLAIN (if your database backend supports it) to see how expensive it is.

Aaron
A: 

As Aaron mentioned, you should get a hold of the query text that is going to be run against the database and use an EXPLAIN (or other some method) to view the query execution plan. Once you have a hold of the execution plan for the query you can see what is going on in the database itself. There are a lot of operations that see very expensive to run through procedural code that are very trivial for any database to run, especially if you provide indexes that the database can use for speeding up your query.

If I read your question correctly, you're retrieving a result set of all rows in the Soknad table. Once you have these results back you use the filter() method to trim down your results meet your criteria. From looking at the Django documentation, it looks like this will do an in-memory filter rather than re-query the database (of course, this really depends on which data access layer you're using and not on Django itself).

The most optimal solution would be to use a full-text search engine (Lucene, ferret, etc) to handle this for you. If that is not available or practical the next best option would be to to construct a query predicate (WHERE clause) before issuing your query to the database and let the database perform the filtering.

However, as with all things that involve the database, the real answer is 'it depends.' The best suggestion is to try out several different approaches using data that is close to production and benchmark them over at least 3 iterations before settling on a final solution to the problem. It may be just as fast, or even faster, to filter in memory rather than filter in the database.

Jeremiah Peschka
-1 just wrong. Django does not "retrieve a result set of all rows" then do an "in-memory filter." In the code presented by the OP, no query is ever performed against the database at all, a SQL query is just constructed step by step. The query will not be executed until results are accessed.
Carl Meyer
Guess I misunderstood objects.all() http://docs.djangoproject.com/en/dev/topics/db/queries/#retrieving-all-objects
Jeremiah Peschka