Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SERVER-77899 remove repeat contain index from the relevant candidate indexes, this can avoid some useless calculations. #1552

Open
wants to merge 3 commits into
base: v5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/mongo/db/query/planner_ixselect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "mongo/db/query/planner_wildcard_helpers.h"
#include "mongo/db/query/query_planner_common.h"
#include "mongo/logv2/log.h"
#include "mongo/bson/simple_bsonelement_comparator.h"

namespace mongo {

Expand Down Expand Up @@ -309,6 +310,103 @@ std::vector<IndexEntry> QueryPlannerIXSelect::findIndexesByHint(
return out;
}

// remove repeat contain index from the relevant candidate indexes, this can avoid some useless calculations.
// for example:
// {a:1, b:1} contain {a:1}, so we can remove index {a:1} from the candidates
// {a:"hashed", b:1} contain {a:"hashed"}, so we can remove index {a:"hahsed"} from the candidates
//
// {a:1, b:1} is better than {a:1},because both cases are satisfied for db.collection.find({a:xx})
// and db.collection.find({a:xx,b:xx})
//
void QueryPlannerIXSelect::removeRepeatContainIndexes(std::vector<IndexEntry>& allIndices) {
if (allIndices.size() <= 1)
return;

auto isSpecialIndex = [](auto& it) {
if (it->type != INDEX_BTREE && it->type != INDEX_HASHED) {
return true;
}

if (it->sparse || it->unique) {
return true;
}

if (it->collator) {
return true;
}

if (it->filterExpr) {
return true;
}

BSONElement e = it->infoObj[IndexDescriptor::kExpireAfterSecondsFieldName];
if (e.isNumber() == false) {
return false;
}

auto expireTime = e.number();
if (expireTime >= 0) {
return true;
} else {
return false;
}
};

auto dealKeyPattern = [](BSONObj& indexKeyPattern) {
BSONObjBuilder build;
BSONObjIterator kpIt(indexKeyPattern);
while (kpIt.more()) {
BSONElement elt = kpIt.next();
if (elt.type() == mongo::String) {
build.append(elt.fieldName(), elt.String());
continue;
}

// The canonical check as to whether a key pattern element is "ascending" or "descending" is
// (elt.number() >= 0). This is defined by the Ordering class.
invariant(elt.isNumber());
int sortOrder = (elt.number() >= 0) ? 1 : -1;
build.append(elt.fieldName(), sortOrder);
}

return build.obj();
};

for (auto iterator1 = allIndices.begin(); iterator1 != allIndices.end();) {
if (isSpecialIndex(iterator1) == true) {
++iterator1;
continue;
}

BSONObj keyPattern1 = dealKeyPattern((*iterator1).keyPattern);

bool eraseIterator = false;
auto iterator2 = iterator1;
++iterator2;
while (iterator2 != allIndices.end()) {
if (isSpecialIndex(iterator2) == true) {
++iterator2;
continue;
}

BSONObj keyPattern2 = dealKeyPattern((*iterator2).keyPattern);

if (keyPattern2.isPrefixOf(keyPattern1, SimpleBSONElementComparator::kInstance)) {
iterator2 = allIndices.erase(iterator2);
} else if (keyPattern1.isPrefixOf(keyPattern2, SimpleBSONElementComparator::kInstance)) {
iterator1 = allIndices.erase(iterator1);
eraseIterator = true;
break;
} else {
++iterator2;
}
}

if (eraseIterator == false)
iterator1++;
}
}

// static
std::vector<IndexEntry> QueryPlannerIXSelect::findRelevantIndices(
const RelevantFieldIndexMap& fields, const std::vector<IndexEntry>& allIndices) {
Expand Down
12 changes: 12 additions & 0 deletions src/mongo/db/query/planner_ixselect.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ class QueryPlannerIXSelect {
static std::vector<IndexEntry> findIndexesByHint(const BSONObj& hintedIndex,
const std::vector<IndexEntry>& allIndices);

/**
* remove repeat contain index from the relevant candidate indexes, this can avoid some useless calculations.
* for example:
* {a:1, b:1} contain {a:1}, so we can remove index {a:1} from the candidates
* {a:"hashed", b:1} contain {a:"hashed"}, so we can remove index {a:"hahsed"} from the candidates
*
* {a:1, b:1} is better than {a:1},because both cases are satisfied for db.collection.find({a:xx})
* and db.collection.find({a:xx,b:xx})
*/
static void removeRepeatContainIndexes(std::vector<IndexEntry>& indexes);


/**
* Finds all indices prefixed by fields we have predicates over. Only these indices are
* useful in answering the query.
Expand Down
11 changes: 11 additions & 0 deletions src/mongo/db/query/query_planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,17 @@ StatusWith<std::vector<std::unique_ptr<QuerySolution>>> QueryPlanner::plan(
"index"_attr = relevantIndices[i].toString());
}

if (!hintedIndexEntry) {
QueryPlannerIXSelect::removeRepeatContainIndexes(relevantIndices);
for (size_t i = 0; i < relevantIndices.size(); ++i) {
LOGV2_DEBUG(20971,
2,
"After Remove Repeat Contain Btree And Hashed Index, Relevant index",
"indexNumber"_attr = i,
"index"_attr = relevantIndices[i].toString());
}
}

// Figure out how useful each index is to each predicate.
QueryPlannerIXSelect::rateIndices(query.root(), "", relevantIndices, query.getCollator());
QueryPlannerIXSelect::stripInvalidAssignments(query.root(), relevantIndices);
Expand Down