-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
sql: prefer order-matching index if there is a limit #5446
Conversation
commit message: s/and expensive/an expensive/ Reviewed 2 of 2 files at r1, 4 of 4 files at r2. sql/select.go, line 313 [r1] (raw file): sql/select.go, line 308 [r2] (raw file): sql/select.go, line 311 [r2] (raw file): sql/select.go, line 670 [r2] (raw file): sql/select.go, line 936 [r2] (raw file): sql/testdata/select_non_covering_index, line 89 [r2] (raw file): Comments from the review on Reviewable.io |
56b9366
to
9ece681
Compare
TFTR! Updated. Review status: 3 of 5 files reviewed at latest revision, 6 unresolved discussions. sql/testdata/select_non_covering_index, line 89 [r2] (raw file): Comments from the review on Reviewable.io |
Reviewed 2 of 2 files at r3. sql/limit.go, line 69 [r3] (raw file): sql/testdata/select_non_covering_index, line 89 [r2] (raw file): Comments from the review on Reviewable.io |
9ece681
to
aa372ba
Compare
Review status: 5 of 6 files reviewed at latest revision, 1 unresolved discussion. sql/limit.go, line 69 [r3] (raw file): Comments from the review on Reviewable.io |
In cockroachdb#4925, we observed ineffective planning for a query in the photos app. We prefer to use the primary index and sort rather than use a non-covering index which makes sense in general (non-covering indices require an expensive indexJoin) but in this case we also had a limit. In such a case using the index would require looking only at the first rows instead of getting all matching rows and sorting. In this change we tweak the index selection: if we have a reasonable limit, we give a "boost" to all indices that match the ordering exactly. The boost exactly offsets the non-covering index penalty. In addition to the new tests, I also verified the photo app query in cockroachdb#4925 now uses the index. Fixes cockroachdb#5246.
Reviewed 1 of 1 files at r4. Comments from the review on Reviewable.io |
sql: prefer order-matching index if there is a limit
In #4925, we observed ineffective planning for a query in the photos app. We
prefer to use the primary index and sort rather than use a non-covering index
which makes sense in general (non-covering indices require and expensive
indexJoin) but in this case we also had a limit. In such a case using the index
would require looking only at the first rows instead of getting all matching
rows and sorting.
In this change we tweak the index selection: if we have a reasonable limit, we
give a "boost" to all indices that match the ordering exactly. The boost exactly
offsets the non-covering index penalty.
In addition to the new tests, I also verified the photo app query in #4925 now
uses the index.
Fixes #5246.
This change is