Opened 6 years ago

Last modified 5 years ago

#29943 closed Cleanup/optimization

Slow admin chanelist query (because of adding `pk` to ordering) — at Initial Version

Reported by: Taha Jahangir Owned by: nobody
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider a simple model with this definition for model and admin:

class MyModel(models.Model):
    class Meta:
        ordering = ('-created',)

    created = models.DateTimeField(default=now, db_index=True)
    message = models.CharField(max_length=20)


@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    pass

We created a model with the indexed created field, and ordering field set to it . It should works nicely. But if the tables go large, the listing will be slow, because the generated query is like:

SELECT "myapp_mymodel"."id", "myapp_mymodel"."created", "myapp_mymodel"."message" FROM "myapp_mymodel" ORDER BY "myapp_mymodel"."created" DESC, "myapp_mymodel"."id" DESC LIMIT 100;

And the database (in my case, postgresql) WILL NOT use the created index and the query becomes very slow.

I treat this as a bug, because the default behavior of admin module is not sensible (and not documented), and will result in performance bug in normal setups , and it cannot be changed in a simple manner (without copying/monkey-patching of ChangeList.get_ordering method).

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top