Skip to content

Commit

Permalink
MB-52919 Do not use sequential scan if another index is avaiable with…
Browse files Browse the repository at this point in the history
… CBO

Change-Id: Iec8b9a27cdf01458f051ea70ce274b9d999c7740
Reviewed-on: https://review.couchbase.org/c/query/+/180082
Reviewed-by: Sitaram Vemulapalli <sitaram.vemulapalli@couchbase.com>
Tested-by: Bingjie Miao <bingjie.miao@couchbase.com>
  • Loading branch information
miaobingjie committed Sep 16, 2022
1 parent 6fd8bd7 commit a9ae850
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions planner/build_scan_secondary.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,14 @@ func (this *builder) minimalIndexes(sargables map[datastore.Index]*indexEntry, s
}

if useCBO && shortest {
seCost := se.scanCost()
teCost := te.scanCost()
if matchedLeadingKeys(se, te, predFc) &&
(seCost < teCost || (seCost == teCost && se.cardinality < te.cardinality)) {
if t.Type() == datastore.SEQ_SCAN && te.nSargKeys == 0 {
delete(sargables, t)
} else if matchedLeadingKeys(se, te, predFc) {
seCost := se.scanCost()
teCost := te.scanCost()
if seCost < teCost || (seCost == teCost && se.cardinality < te.cardinality) {
delete(sargables, t)
}
}
} else {
if narrowerOrEquivalent(se, te, shortest, predFc) {
Expand Down Expand Up @@ -740,6 +743,11 @@ outer:

// for CBO, prune indexes that has similar leading index keys
func matchedLeadingKeys(se, te *indexEntry, predFc map[string]value.Value) bool {
if se.nSargKeys == 0 && te.nSargKeys == 0 &&
se.HasFlag(IE_LEADINGMISSING) && te.HasFlag(IE_LEADINGMISSING) {
return true
}

nkeys := 0
ncond := 0
for i, tk := range te.sargKeys {
Expand Down

0 comments on commit a9ae850

Please sign in to comment.