Try harder to choose index queries as a data source

AndSource should be choosing which child as the primary data source
based on its cost, in order to make the cheapest query.

This improves the chances of a partial-index query actually being
answered from the index, because the query builder adds at least one
top-level index predicate (sortkey) to any query. If we treat the
cost of an index query as 0, this means that of a top-level AndSource
it should choose that rather than a non-index predicate.

This also papers over an issue where the index rewriter fails to wrap
a non-indexable predicate (e.g. ownerin) in a ChangeDataSource.

Change-Id: I56d9781743fe31e5cf8c309d62cd93fe0d886219
This commit is contained in:
Dave Borowitz
2013-09-13 14:53:26 -04:00
parent 2bd1cb12d1
commit 8828458f53
2 changed files with 10 additions and 4 deletions

View File

@@ -126,7 +126,10 @@ public class IndexedChangeQuery extends Predicate<ChangeData>
@Override
public int getCost() {
return pred.getCost();
// Index queries are assumed to be cheaper than any other type of query, so
// so try to make sure they get picked. Note that pred's cost may be higher
// because it doesn't know whether it's being used in an index query or not.
return 0;
}
@Override

View File

@@ -162,12 +162,15 @@ public class AndSource extends AndPredicate<ChangeData>
}
private ChangeDataSource source() {
int minCost = Integer.MAX_VALUE;
Predicate<ChangeData> s = null;
for (Predicate<ChangeData> p : getChildren()) {
if (p instanceof ChangeDataSource) {
return (ChangeDataSource) p;
if (p instanceof ChangeDataSource && p.getCost() < minCost) {
s = p;
minCost = p.getCost();
}
}
return null;
return (ChangeDataSource) s;
}
@Override