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

sql: prefer order-matching index when there is a limit #5246

Closed
RaduBerinde opened this issue Mar 14, 2016 · 5 comments
Closed

sql: prefer order-matching index when there is a limit #5246

RaduBerinde opened this issue Mar 14, 2016 · 5 comments
Assignees
Labels
C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)
Milestone

Comments

@RaduBerinde
Copy link
Member

There is a tradeoff between selecting a non-covering index that has the desired ordering vs using a covering index that doesn't. However, when we have a limit the latter has the potential of reading much more data so we should prefer the order-matching index.

See #4925 (comment) for an example.

@RaduBerinde RaduBerinde self-assigned this Mar 14, 2016
@RaduBerinde RaduBerinde added this to the Beta milestone Mar 14, 2016
@tbg
Copy link
Member

tbg commented Mar 14, 2016

to govern that "tradeoff", maybe taking into account the "average" (or otherwise characteristic) size of result sets which need to be handled for both possible options. Something like an "adaptive query planner". That must be a thing already in literature.

@petermattis
Copy link
Collaborator

"Cost based query optimization".

@tbg
Copy link
Member

tbg commented Mar 14, 2016

I was more asking about particular techniques/impls in distributed systems. I know Postgres uses all sorts of stats on the data size to guess how much it's going to get back, but we won't have a lot of those luxuries. Are you aware of anything on that subsubject in particular?

@RaduBerinde
Copy link
Member Author

Absolutely, you can be smarter if you have some kind of statistics available. We will definitely need to build some infrastructure around that.

Until then we can make a simple change to give a "bonus" that offsets the non-covering index penalty when an index satisfies the desired ordering completely AND there is a limit.

@petermattis
Copy link
Collaborator

I think we can do a decent job with table and index size statistics by using the range meta keys to determine how many ranges a particular range of keys spans.

@RaduBerinde RaduBerinde added the C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) label Mar 17, 2016
RaduBerinde added a commit to RaduBerinde/cockroach that referenced this issue Mar 21, 2016
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 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 cockroachdb#4925 now
uses the index.

Fixes cockroachdb#5246.
RaduBerinde added a commit to RaduBerinde/cockroach that referenced this issue Mar 22, 2016
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.
RaduBerinde added a commit to RaduBerinde/cockroach that referenced this issue Mar 22, 2016
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception)
Projects
None yet
Development

No branches or pull requests

3 participants