noobzie
noobzie

Reputation: 1139

Faster way to access the oldest database record in Django?

I have a batch job that processes records in order of the oldest record. In django, I have a model defined similar to this:

class mymodel(models.Model):
    name = models.CharField()
    ...
    last_processed = models.DateTimeField(blank=True, editable=False, null=True)

I need to get the oldest record by the DateTimeField last_processed. I understand the django way of doing this is:

mymodel.objects.order_by('last_processed')[:1].get()

However, is there a faster and more efficient way to execute that query?

I'm expecting the database to grows, so it possible to have up to 10 million records. Continually running that query doesn't seem very efficient....

System specification:

Upvotes: 1

Views: 1023

Answers (3)

Willian
Willian

Reputation: 2445

I think you must find out if it is inefficient. If it is, you could cache it (store it in the db, or use something like memcache)

Upvotes: 0

dMb
dMb

Reputation: 9337

Do you have an index on 'last_processed'? If so, the query will be efficient regardless of the size of the table.

Upvotes: 3

second
second

Reputation: 28637

if you are often searching based on the last_processed field, it may be worth indexing it

otherwise what you are doing looks fine

Upvotes: 2

Related Questions