Sergey Nizhny Novgorod2017-03-24 16:49:17
Sergey Nizhny Novgorod, 2017-03-24 16:49:17

How to get the last element as fast as possible in Django?

Hello everyone
There is a model of about 1000 records, new elements are constantly added to the tail. The task is to get the id-number of the last element as quickly as possible.

test = Test.objects.all().order_by('-id')[0].id

test = Test.objects.all().last().id

They don't work as fast as I would like. Is there a faster way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Peter, 2017-03-24

In both cases, a query c must be generated
. speed uvas here depends only on the database.
And for acceleration in a database - the index on this field is necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question