-
Notifications
You must be signed in to change notification settings - Fork 37
Add DB indexes #155
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 DB indexes #155
Conversation
@kakulukia @c-soft @jonatron @eviljeff interested in your thoughts on this patch, given you've experienced these issues first-hand. |
ebd4572
to
26ee0f1
Compare
I currently have the following indices set So i looks like we have a match on pk and priority. I figured as you are using DBTaskResult.objects.ready() that the status index should also be valuable. But i dont have the time to further test this during the coming weeks. The other two are probably irrelevant. |
If you try running with the indexes in this PR, do you get the same performance improvements you saw? |
i will try to find some time for testing this in the next days - lets see |
Seems ok for postgres, after run_after order is updated #154 . return self.filter(
status=ResultStatus.NEW
).filter(
models.Q(run_after=None),
).union(
self.filter(
status=ResultStatus.NEW,
).filter(
models.Q(run_after__lte=timezone.now()),
),
all=True,
) Although this doesn't work for sqlite. As mentioned in #113 (comment) , another workaround is make run_after not null. |
The |
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've not checked out and tested locally but lgtm
# HACK: SQLite doesn't support NULLS LAST in indexes | ||
models.Case(models.When(run_after=None, then=True), default=False), |
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.
great hack! I was playing around with Coalesce
but this is cleaner
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.
Yeah I got some strange SQL syntax errors when I tried that. I need to try and distil them down to see if it's a bug in what I did or in the ORM itself.
F("priority").desc(), | ||
# HACK: SQLite doesn't support NULLS LAST in indexes | ||
models.Case(models.When(run_after=None, then=True), default=False), | ||
F("run_after").desc(), |
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.
this needs updating to .asc()
after #154
This is so far the only way I've found to make the index be used on all of mysql, postgres and sqlite: master...jonatron:django-tasks:db-indexes-2 . |
Yeah the addition of a |
Checking if @adamchainz , the django mysql guy wants to advise |
26ee0f1
to
5be1aab
Compare
@jonatron I decided to just pull in your changes, since they're basically exactly what I ended up with 😆. Nice work! ❤️ I don't think adding |
5be1aab
to
8dc8ebe
Compare
Closes #113
Fixes #140
Fixes #15
Add some indexes to improve performance. From various testing in the linked PRs, it should make quite a difference.
The ordering index seems to be the one which makes the most difference. The other indexes are mostly there to make other bits of filtering better.