diff options
author | Cheahuychou Mao <mao.cheahuychou@gmail.com> | 2023-03-14 19:15:37 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-03-14 23:44:16 +0000 |
commit | 1b7c76d3e66c123bbc64b41ae9a0ded6ba3de6a9 (patch) | |
tree | 156e05509722cec77bc51a2ed40b533c33b1dd88 /src/mongo/db | |
parent | e0d2c7c1e007b93c7d9dfe6e4912ff37498b64e2 (diff) | |
download | mongo-1b7c76d3e66c123bbc64b41ae9a0ded6ba3de6a9.tar.gz |
SERVER-74773 Make analyzeShardKey command use the most compatible index to calculate cardinality and frequency metrics
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/s/analyze_shard_key_cmd_util.cpp | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/mongo/db/s/analyze_shard_key_cmd_util.cpp b/src/mongo/db/s/analyze_shard_key_cmd_util.cpp index d8152331156..2b2a418ea76 100644 --- a/src/mongo/db/s/analyze_shard_key_cmd_util.cpp +++ b/src/mongo/db/s/analyze_shard_key_cmd_util.cpp @@ -250,11 +250,15 @@ boost::optional<IndexSpec> findCompatiblePrefixedIndex(OperationContext* opCtx, } } + // Go through the indexes in the index catalog to find the most compatible index. + boost::optional<IndexSpec> compatibleIndexSpec; + auto indexIterator = indexCatalog->getIndexIterator(opCtx, IndexCatalog::InclusionPolicy::kReady); while (indexIterator->more()) { auto indexEntry = indexIterator->next(); auto indexDesc = indexEntry->descriptor(); + auto indexKey = indexDesc->keyPattern(); if (indexDesc->getIndexType() != IndexType::INDEX_BTREE && indexDesc->getIndexType() != IndexType::INDEX_HASHED) { @@ -270,13 +274,19 @@ boost::optional<IndexSpec> findCompatiblePrefixedIndex(OperationContext* opCtx, continue; } - auto indexKey = indexDesc->keyPattern(); - if (shardKey.isFieldNamePrefixOf(indexKey)) { - return IndexSpec{indexKey, indexDesc->unique()}; + if (!shardKey.isFieldNamePrefixOf(indexKey)) { + continue; + } + if (!compatibleIndexSpec.has_value() || + compatibleIndexSpec->keyPattern.nFields() > indexKey.nFields() || + (!compatibleIndexSpec->isUnique && indexDesc->unique())) { + // Give preference to indexes with fewer fields and unique indexes since they can help + // us infer if the shard key is unique. + compatibleIndexSpec = IndexSpec{indexKey, indexDesc->unique()}; } } - return boost::none; + return compatibleIndexSpec; } struct CardinalityFrequencyMetrics { |