Repository: phoenix Updated Branches: refs/heads/4.0 b8e7da27f -> 0fdf78e7c
PHOENIX-1346 the choice of index by phoneix is not correct where do querying Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/0fdf78e7 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/0fdf78e7 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/0fdf78e7 Branch: refs/heads/4.0 Commit: 0fdf78e7c4ea8ba6fdb29670dfddb287eea8b8cf Parents: b8e7da2 Author: James Taylor <[email protected]> Authored: Mon Nov 24 21:26:28 2014 -0800 Committer: James Taylor <[email protected]> Committed: Mon Nov 24 21:30:12 2014 -0800 ---------------------------------------------------------------------- .../apache/phoenix/optimize/QueryOptimizer.java | 26 ++++++-------------- 1 file changed, 8 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/0fdf78e7/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java b/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java index 333b102..a478656 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java @@ -317,21 +317,11 @@ public class QueryOptimizer { public int compare(QueryPlan plan1, QueryPlan plan2) { PTable table1 = plan1.getTableRef().getTable(); PTable table2 = plan2.getTableRef().getTable(); - int c = plan2.getContext().getScanRanges().getRanges().size() - plan1.getContext().getScanRanges().getRanges().size(); - boolean bothLocalIndexes = table1.getIndexType() == IndexType.LOCAL && table2.getIndexType() == IndexType.LOCAL; - // Account for potential view constants which are always bound - if (plan1 == dataPlan) { // plan2 is index plan. Ignore the viewIndexId if present - c += boundRanges - (table2.getViewIndexId() == null || bothLocalIndexes ? 0 : 1); - // if table2 is local index table and query doesn't have any condition on the - // indexed columns then give first priority to the local index. - if(table2.getIndexType()==IndexType.LOCAL && plan2.getContext().getScanRanges().getRanges().size()==0) c++; - } else { // plan1 is index plan. Ignore the viewIndexId if present - c -= boundRanges - (table1.getViewIndexId() == null || bothLocalIndexes ? 0 : 1); - // if table1 is local index table and query doesn't have any condition on the - // indexed columns then give first priority to the local index. - if (!bothLocalIndexes && table1.getIndexType() == IndexType.LOCAL - && plan1.getContext().getScanRanges().getRanges().isEmpty()) c--; - } + // For shared indexes (i.e. indexes on views and local indexes), + // a) add back any view constants as these won't be in the index, and + // b) ignore the viewIndexId which will be part of the row key columns. + int c = (plan2.getContext().getScanRanges().getRanges().size() + (table2.getViewIndexId() == null ? 0 : (boundRanges - 1))) - + (plan1.getContext().getScanRanges().getRanges().size() + (table1.getViewIndexId() == null ? 0 : (boundRanges - 1))); if (c != 0) return c; if (plan1.getGroupBy()!=null && plan2.getGroupBy()!=null) { if (plan1.getGroupBy().isOrderPreserving() != plan2.getGroupBy().isOrderPreserving()) { @@ -343,12 +333,12 @@ public class QueryOptimizer { if (c != 0) return c; // If all things are equal, don't choose local index as it forces scan - // on every region. + // on every region (unless there's no start/stop key) if (table1.getIndexType() == IndexType.LOCAL) { - return 1; + return plan1.getContext().getScanRanges().getRanges().isEmpty() ? -1 : 1; } if (table2.getIndexType() == IndexType.LOCAL) { - return -1; + return plan2.getContext().getScanRanges().getRanges().isEmpty() ? 1 : -1; } // All things being equal, just use the table based on the Hint.USE_DATA_OVER_INDEX_TABLE
