Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add index for CRUDEvent.datetime #250

Open
GigiusB opened this issue Mar 27, 2023 · 1 comment
Open

Add index for CRUDEvent.datetime #250

GigiusB opened this issue Mar 27, 2023 · 1 comment

Comments

@GigiusB
Copy link

GigiusB commented Mar 27, 2023

In our scenario we have 1.7M records in the table (postgres DB)

By indexing the field CRUDEvent.datetime the following query performed in the admin goes from 38seconds down to <0.5sec

SELECT "easyaudit_crudevent"."id",
       "easyaudit_crudevent"."event_type",
       "easyaudit_crudevent"."object_id",
       "easyaudit_crudevent"."content_type_id",
       "easyaudit_crudevent"."object_repr",
       "easyaudit_crudevent"."object_json_repr",
       "easyaudit_crudevent"."changed_fields",
       "easyaudit_crudevent"."user_id",
       "easyaudit_crudevent"."user_pk_as_string",
       "easyaudit_crudevent"."datetime"
  FROM "easyaudit_crudevent"
 ORDER BY "easyaudit_crudevent"."datetime" DESC, "easyaudit_crudevent"."id" DESC
 LIMIT 100
GigiusB added a commit to GigiusB/django-easy-audit that referenced this issue Mar 27, 2023
@jheld
Copy link
Collaborator

jheld commented Apr 6, 2023

I do understand this scenario. I'm not going to say it's unimportant. I'm very well acquainted with it and in some cases have been able to get around it.

The problem as I see it is that it appears to be only come about through access via the django admin.

Perhaps if we removed the default ordering of -datetime people would be less likely to be initially hit by this.

I wonder if potentially encouraging people to use the ModelAdmin's configurability via https://docs.djangoproject.com/en/4.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.ordering would encourage other solutions.

So for instance, when you're doing a search on django admin, what are you likely to be looking for? Is it really just for items descending order by time, or more likely items in a certain object_type/object_id instead (and ordered by time)?

In the end we may have to allow this change (the devil's advocate was that I felt a bit cautious with regards to the incorrect index being chosen on certain queries, and thus scenarios where this would be slowdowns that you would not expect, given the additional index being in place).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants