I want to retrieve the first 500 results from a large database that match a given filter query.
At the moment I'm using the following (terribly inefficient) method..
results = Entries.objects.filter(text__icontains="somequery")[0:500]
But I think this query loads the entire database in memory and then truncates the results. It's terribly slow.
Is there a more elegant way to do this? Thanks!
Use a subset of Python's array-slicing syntax to limit your QuerySet to a certain number of results. This is the equivalent of SQL's LIMIT and OFFSET clauses. Negative indexing (i.e. Entry.objects.all()[-1] ) is not supported.
The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.
Returns a new QuerySet containing objects that match the given lookup parameters. Basically use get() when you want to get a single unique object, and filter() when you want to get all objects that match your lookup parameters.
That's the way to do it.
The SQL generated uses LIMIT
so it's not loading the entire database into memory and being python sliced.
Note that you can see what SQL django is writing by using django.db.connection.queries
http://docs.djangoproject.com/en/dev/faq/models/#how-can-i-see-the-raw-sql-queries-django-is-running
But a lesser known trick is to print a queryset.query
or call sql = queryset.query.__str__()
>>> results = Entries.objects.filter(text__icontains="somequery")[0:500]
>>> print results.query
SELECT ... FROM ... WHERE ... LIKE ... LIMIT 500
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With