You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@asterixdb.apache.org by AsterixDB Code Review <do...@asterix-gerrit.ics.uci.edu> on 2023/07/13 04:14:07 UTC

Change in asterixdb[master]: [ASTERIXDB-3209][COMP]: CBO Not Picking Up Index (for Equality Pred)

From Vijay Sarathy <vi...@couchbase.com>:

Vijay Sarathy has uploaded this change for review. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17639 )


Change subject: [ASTERIXDB-3209][COMP]: CBO Not Picking Up Index (for Equality Pred)
......................................................................

[ASTERIXDB-3209][COMP]: CBO Not Picking Up Index (for Equality Pred)

Change-Id: I27a00609b86ea1f53ba6f81788a74079e947731c
---
M asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/CostMethods.java
1 file changed, 29 insertions(+), 13 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/39/17639/1

diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/CostMethods.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/CostMethods.java
index 887cc94..7acce9b 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/CostMethods.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/cost/CostMethods.java
@@ -31,6 +31,7 @@
     protected long blockSize;
     protected long DOP;
     protected double maxMemorySize;
+    protected static double selectivityForSecondaryIndexSelection = 0.1;
 
     public CostMethods(IOptimizationContext context) {
         optCtx = context;
@@ -58,65 +59,71 @@
         return physOptConfig.getMaxFramesForJoin() * physOptConfig.getFrameSize();
     }
 
-    // These cost methods are very simple and rudimentary for now. These can be improved by asterixdb developers as needed.
+    // These cost methods are very simple and rudimentary for now. 
+    // They can be improved by asterixdb developers as needed.
     public Cost costFullScan(JoinNode jn) {
-        return new Cost(jn.computeJoinCardinality());
+        return new Cost(jn.getOrigCardinality());
     }
 
     public Cost costIndexScan(JoinNode jn, double indexSel) {
-        return new Cost(jn.computeJoinCardinality());
+        return new Cost(indexSel * jn.getOrigCardinality());
     }
 
     public Cost costIndexDataScan(JoinNode jn, double indexSel) {
-        return new Cost(jn.computeJoinCardinality());
+        if (indexSel < selectivityForSecondaryIndexSelection) {
+            return new Cost(indexSel * jn.getOrigCardinality());
+        }
+
+        // If index selectivity is not very selective, make index scan more expensive than full scan.
+        return new Cost(jn.getOrigCardinality());
     }
 
     public Cost costHashJoin(JoinNode jn) {
         JoinNode leftJn = jn.getLeftJn();
         JoinNode rightJn = jn.getRightJn();
-        return new Cost(leftJn.computeJoinCardinality() + rightJn.computeJoinCardinality());
+        return new Cost(leftJn.getCardinality() + rightJn.getCardinality());
     }
 
     public Cost computeHJProbeExchangeCost(JoinNode jn) {
         JoinNode leftJn = jn.getLeftJn();
-        return new Cost(leftJn.computeJoinCardinality());
+        return new Cost(leftJn.getCardinality());
     }
 
     public Cost computeHJBuildExchangeCost(JoinNode jn) {
         JoinNode rightJn = jn.getRightJn();
-        return new Cost(rightJn.computeJoinCardinality());
+        return new Cost(rightJn.getCardinality());
     }
 
     public Cost costBroadcastHashJoin(JoinNode jn) {
         JoinNode leftJn = jn.getLeftJn();
         JoinNode rightJn = jn.getRightJn();
-        return new Cost(leftJn.computeJoinCardinality() + DOP * rightJn.computeJoinCardinality());
+        return new Cost(leftJn.getCardinality() + DOP * rightJn.getCardinality());
     }
 
     public Cost computeBHJBuildExchangeCost(JoinNode jn) {
         JoinNode rightJn = jn.getRightJn();
-        return new Cost(DOP * rightJn.computeJoinCardinality());
+        return new Cost(DOP * rightJn.getCardinality());
     }
 
     public Cost costIndexNLJoin(JoinNode jn) {
         JoinNode leftJn = jn.getLeftJn();
         JoinNode rightJn = jn.getRightJn();
-        return new Cost(leftJn.computeJoinCardinality());
+        return new Cost(2 * leftJn.getCardinality());
     }
 
     public Cost computeNLJOuterExchangeCost(JoinNode jn) {
         JoinNode leftJn = jn.getLeftJn();
-        return new Cost(DOP * leftJn.computeJoinCardinality());
+        return new Cost(DOP * leftJn.getCardinality());
     }
 
     public Cost costCartesianProductJoin(JoinNode jn) {
         JoinNode leftJn = jn.getLeftJn();
         JoinNode rightJn = jn.getRightJn();
-        return new Cost(leftJn.computeJoinCardinality() * rightJn.computeJoinCardinality());
+        return new Cost(leftJn.getCardinality() * rightJn.getCardinality());
     }
 
     public Cost computeCPRightExchangeCost(JoinNode jn) {
         JoinNode rightJn = jn.getRightJn();
-        return new Cost(DOP * rightJn.computeJoinCardinality());
+        return new Cost(DOP * rightJn.getCardinality());
     }
 }

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17639
To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Change-Id: I27a00609b86ea1f53ba6f81788a74079e947731c
Gerrit-Change-Number: 17639
Gerrit-PatchSet: 1
Gerrit-Owner: Vijay Sarathy <vi...@couchbase.com>
Gerrit-MessageType: newchange