-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
add indexes to timestamped models #1433
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
base: main
Are you sure you want to change the base?
add indexes to timestamped models #1433
Conversation
Could you please update |
update docs with the usage examples of the Meta class.
update docs with the usage examples of the Meta class.
a4a4017
to
e2e2781
Compare
I have added the docs. Thanks. |
django_extensions/db/models.py
Outdated
@@ -25,6 +25,7 @@ def save(self, **kwargs): | |||
class Meta: | |||
get_latest_by = 'modified' | |||
ordering = ('-modified', '-created',) | |||
indexes = [models.Index(fields=["created"]), models.Index(fields=["modified"])] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently these indexes are like adding db_index=True
to each of the fields (created
and modified
). There is no need for Meta.indexes
in this case.
If Meta.indexes
should be defined I thing that is better to add index which is for both fields in descending order (to match the Meta.ordering
). This way listing of the objects should be faster. Also individual indexes can also be in descending order (at least for modified
) for latest()
to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your points. So do you think it would be better if I modified the current version like this:
indexes = [
models.Index(fields=["-created", "-modified"]),
models.Index(fields=["-created"]), models.Index(fields=["-modified"])
]
Sorry guys, I was (mistakenly) under the impression that we already had indexes via This change would introduce a schema migration for people so we have to think a little more careful about this imho. Why not do a little differently... Add an class IndexedTimeStampedModel(TimeStampedModel):
class Meta(TimeStampedModel.Meta):
indexes = [
models.Index(fields=["-modified"])
models.Index(fields=["-created"]),
] (or This way we do not suddenly change the behavior of |
I was thinking of implementing this way, but then most people will have to modify their code if they want to use indexes. On the other hand if we go with adding indexes to Although, I am not in favor of going with latter option, we can totally avoid it by adding the document for
Yes
|
Cool lets do it that way and add index'd versions with some documentation. This would also be a very nice example of how easy it can be to customize these things :-) |
FWIW as a django-extensions user I think adding the index to my existing models, vs. having to make complex migrations to migrate my
This would be much easier for me than having to switch my existing @trbs I would think that for almost all use cases, having an index on For large projects this would (I believe) significantly speed up queries, and for small projects this would make a negligible difference from the current implementation. |
Unless you have Anyways so many potential cases and we really don't know what people are using it for. I can be convinced to set the indexes by default, specially since I think having a complex migration path for people (I assume the majority) that do want to have the indexes by default. So what I suggest to do is if there is consensus to make the indexes the default. Then we do so via:
(Please note that I will be traveling a lot this month, so appologies in advance for reviews and merging PR's to be a bit slow) |
@trbs thanks for your thoughtful response! One thing to add. I think the reason people would assume the indexes are in place is because Your suggestion for managing the change makes perfect sense. @anudeepsamaiya let me know if you want any help on implementing the changes? I have some time end of next week but not before then. |
Issue Ref: #1425
Added indexes in the
Meta
of theTimeStampedModel
.Usage:
or
or