Skip to content Skip to sidebar Skip to footer

Django Queryset Runtime - Get Nth Entry In Constant Time

I'm using multiple ways to get data from db via different django querysets, but I would like to know the runtime for each queryset and if possible a better way (to maybe get data i

Solution 1:

From the docs:

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.

Generally, slicing a QuerySet returns a new QuerySet – it doesn’t evaluate the query. An exception is if you use the “step” parameter of Python slice syntax.

To retrieve a single object rather than a list (e.g. SELECT foo FROM bar LIMIT 1), use a simple index instead of a slice.

https://docs.djangoproject.com/en/dev/topics/db/queries/#limiting-querysets

The part about not evaluating the queryset as you slice it is the important part.

Post a Comment for "Django Queryset Runtime - Get Nth Entry In Constant Time"