diff --git a/arangod/Aql/Condition.cpp b/arangod/Aql/Condition.cpp index 18c93a3a3b..6ad89f5703 100644 --- a/arangod/Aql/Condition.cpp +++ b/arangod/Aql/Condition.cpp @@ -166,13 +166,18 @@ void Condition::andCombine (AstNode const* node) { /// @brief locate indices for each condition //////////////////////////////////////////////////////////////////////////////// -void Condition::findIndexes (EnumerateCollectionNode const* node) { +void Condition::findIndexes (EnumerateCollectionNode const* node, std::vector usedIndexes) { // We can only start after DNF transform TRI_ASSERT(_root->type == NODE_TYPE_OPERATOR_NARY_OR); Variable const* reference = node->outVariable(); for (size_t i = 0; i < _root->numMembers(); ++i) { - setIndexForAndNode(_root->getMember(i), reference, node); + setIndexForAndNode(_root->getMember(i), reference, node, usedIndexes)); + if (i != usedIndexes.size()) { + // We are not able to find an index for this AND block. Sorry we have to abort here + usedIndexes.clear(); + break; + } } } @@ -181,27 +186,21 @@ void Condition::setIndexForAndNode (AstNode const* node, Variable const* referen TRI_ASSERT(node->type == NODE_TYPE_OPERATOR_NARY_AND); std::vector indexes = colNode->collection()->getIndexes(); - std::vector matching; std::vector sortAttributes; // TODO has to be internal + double bestCost = -1.0; // All costs are > 0, so if we have found one we can use it. + Index* bestIndex; + for (auto& idx : indexes) { double estimatedCost; if (idx->canServeForConditionNode(node, reference, &sortAttributes, estimatedCost)) { - matching.emplace_back(idx); + if (bestCost < estimatedCost) { + bestIndex = idx; + } } } std::cout << "We can use indexes for var: " << reference->name << " in collection " << colNode->collection()->getName() << ":" << std::endl; - for (auto& idx : matching) { - std::cout << idx->toJson() << std::endl; - /* - std::cout << "Type" << idx->type; - if (idx->hasSelectivityEstimate()) { - std::cout << " Estimate: " << idx->selectivityEstimate(); - } - std::cout << std::endl; - */ - } - std::cout << std::endl; + std::cout << "We use: " << bestIndex->toJson() << std::endl; } //////////////////////////////////////////////////////////////////////////////// diff --git a/arangod/Aql/Condition.h b/arangod/Aql/Condition.h index 28580fc176..eef7f71229 100644 --- a/arangod/Aql/Condition.h +++ b/arangod/Aql/Condition.h @@ -172,7 +172,7 @@ namespace triagens { /// @brief locate indices which can be used for conditions //////////////////////////////////////////////////////////////////////////////// - void findIndexes (EnumerateCollectionNode const*); + void findIndexes (EnumerateCollectionNode const*, std::vector); //////////////////////////////////////////////////////////////////////////////// /// @brief dump the condition