You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ja...@apache.org on 2014/11/25 06:26:55 UTC

phoenix git commit: PHOENIX-1346 the choice of index by phoneix is not correct where do querying

Repository: phoenix
Updated Branches:
  refs/heads/master 3f4579038 -> bff5ddae1


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/bff5ddae
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/bff5ddae
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/bff5ddae

Branch: refs/heads/master
Commit: bff5ddae11ba2ac47804bc6db6afecd5f1bbe76a
Parents: 3f45790
Author: James Taylor <jt...@salesforce.com>
Authored: Mon Nov 24 21:26:28 2014 -0800
Committer: James Taylor <jt...@salesforce.com>
Committed: Mon Nov 24 21:26:28 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/bff5ddae/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