﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29943	Slow admin chanelist query (because of adding `pk` to ordering)	Taha Jahangir	nobody	"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)."	Bug	new	contrib.admin	dev	Normal				Unreviewed	0	0	0	0	1	0
