-
-
Notifications
You must be signed in to change notification settings - Fork 481
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
as_of with Postgres generates a Sequential Scan with composite index enabled #1310
Comments
I seem to be running into this as well, regardless of whether a composite index is being used. By adding a |
@ddabble any concern with removing that code block? I think this is an easy win. |
Sure, as long as removing it doesn't severely affect performance in some cases (which I assumed was the reason it was added in the first place, but perhaps not..?), I'm all for both improving performance and reducing the amount of DB backend-specific code 🙂 Would be great having some tests analyzing/measuring the query performance, though, but I'm not familiar with any method of doing that easily 🤔 |
Describe the bug
When using
print(MyModel.history.as_of(as_of).only('id').explain(analyze=True))
, it generates a Sequential Scan fromt his code here:django-simple-history/simple_history/manager.py
Lines 76 to 82 in 273a4ef
To Reproduce
Steps to reproduce the behavior:
print(MyModel.history.as_of(as_of).only('id').explain(analyze=True))
Seq Scan on
in the report.Expected behavior
Sequential scans are the worst type of operation since it means every single record is being checked. This should not be the case, especially considering the basic nature of the query.
If we use the sqlite version of
latest_of_each
which uses a Subquery, we end up hitting an index scan which is significantly better:Environment (please complete the following information):
Suggested fix
I'd suggest dropping the postgresql logic branch in the
latest_of_each
method in favor of the Subquery.The text was updated successfully, but these errors were encountered: