[Python] Performance issues when using Django's Min('id') in big table

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have a model Table which gets a new entry every few seconds, so it's a quite big table.

    class Table(models.Model):
    id: int
    pk: int

    timestamp = models.PositiveBigIntegerField(db_index=True)


    Now what i am trying to do is get the first entry for the last 24h. And for some reason it takes way to much time. Like we are talking about seconds. But if i try to get last i get it instantly. I've tried following queries:

    from_ts = int(time.time()) - 24 * 60 * 60
    first_entry = Table.objects.filter(timestamp__gte=from_ts).first()
    first_entry = Table.objects.filter(timestamp__gte=from_ts).aggregate(Min('id'))


    They both take a few seconds to complete but if I try .aggregate(Min('timestamp')) it returns almost instantly. I tried .filter(timestamp__gte=from_ts)[0] which is quick and does work but i know its undefined behaviour because the entries aren't sorted and in some edge cases it might not work. Then i tried to just get the whole query in python and find min in python and it was almost instant.

    first_entry = min(list(Table.objects.filter(timestamp__gte=from_ts)))


    Can anyone explain what exactly is happening here and what is the cleanest/best solution?

    Continue reading...

Compartilhe esta Página