diff options
author | ssmike <ssmike@ydb.tech> | 2023-12-12 18:55:33 +0300 |
---|---|---|
committer | ssmike <ssmike@ydb.tech> | 2023-12-12 19:59:32 +0300 |
commit | f27466ce03991dc5c2292394e9187fa2bb532111 (patch) | |
tree | 04387af87c2fbf8520b2cd4d2fcc53beb56031c4 | |
parent | a3d7f0d2be0a2f1cf394428ffb4e08ea0388520f (diff) | |
download | ydb-f27466ce03991dc5c2292394e9187fa2bb532111.tar.gz |
Consider used columns in index chooser
KIKIMR-10864
-rw-r--r-- | ydb/core/kqp/opt/logical/kqp_opt_log_ranges_predext.cpp | 20 | ||||
-rw-r--r-- | ydb/core/kqp/ut/opt/kqp_ne_ut.cpp | 4 |
2 files changed, 17 insertions, 7 deletions
diff --git a/ydb/core/kqp/opt/logical/kqp_opt_log_ranges_predext.cpp b/ydb/core/kqp/opt/logical/kqp_opt_log_ranges_predext.cpp index a60308ebdd..b6a0a87f95 100644 --- a/ydb/core/kqp/opt/logical/kqp_opt_log_ranges_predext.cpp +++ b/ydb/core/kqp/opt/logical/kqp_opt_log_ranges_predext.cpp @@ -265,14 +265,23 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx if (!indexName.IsValid() && !readSettings.ForcePrimary && kqpCtx.Config->IndexAutoChooserMode != NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_DISABLED) { using TIndexComparisonKey = std::tuple<bool, size_t, bool, size_t, bool>; - auto calcKey = [&](NYql::IPredicateRangeExtractor::TBuildResult buildResult, size_t descriptionKeyColumns, bool needsJoin) -> TIndexComparisonKey { + auto calcNeedsJoin = [&] (const TKikimrTableMetadataPtr& keyTable) -> bool { + bool needsJoin = false; + for (auto&& column : read.Columns()) { + if (!keyTable->Columns.contains(column.Value())) { + needsJoin = true; + } + } + return needsJoin; + }; + auto calcKey = [&](NYql::IPredicateRangeExtractor::TBuildResult buildResult, size_t descriptionKeyColumns, bool needsJoin) -> TIndexComparisonKey { return std::make_tuple( buildResult.PointPrefixLen >= descriptionKeyColumns, buildResult.PointPrefixLen, buildResult.UsedPrefixLen >= descriptionKeyColumns, buildResult.UsedPrefixLen, - needsJoin); + !needsJoin); }; TMaybe<TString> chosenIndex; @@ -284,15 +293,16 @@ TExprBase KqpPushExtractedPredicateToReadTable(TExprBase node, TExprContext& ctx if (index.Type != TIndexDescription::EType::GlobalAsync) { auto& tableDesc = kqpCtx.Tables->ExistingTable(kqpCtx.Cluster, mainTableDesc.Metadata->GetIndexMetadata(TString(index.Name)).first->Name); auto buildResult = extractor->BuildComputeNode(tableDesc.Metadata->KeyColumnNames, ctx, typesCtx); + bool needsJoin = calcNeedsJoin(tableDesc.Metadata); - if (kqpCtx.Config->IndexAutoChooserMode == NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_ONLY_FULL_KEY && buildResult.PointPrefixLen < index.KeyColumns.size()) { + if (needsJoin && kqpCtx.Config->IndexAutoChooserMode == NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_ONLY_FULL_KEY && buildResult.PointPrefixLen < index.KeyColumns.size()) { continue; } - if (kqpCtx.Config->IndexAutoChooserMode == NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_ONLY_POINTS && buildResult.PointPrefixLen == 0) { + if (needsJoin && kqpCtx.Config->IndexAutoChooserMode == NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_ONLY_POINTS && buildResult.PointPrefixLen == 0) { continue; } - auto key = calcKey(buildResult, index.KeyColumns.size(), true); + auto key = calcKey(buildResult, index.KeyColumns.size(), needsJoin); if (key > maxKey) { maxKey = key; chosenIndex = index.Name; diff --git a/ydb/core/kqp/ut/opt/kqp_ne_ut.cpp b/ydb/core/kqp/ut/opt/kqp_ne_ut.cpp index 89b2f10a18..adc7addeac 100644 --- a/ydb/core/kqp/ut/opt/kqp_ne_ut.cpp +++ b/ydb/core/kqp/ut/opt/kqp_ne_ut.cpp @@ -3905,7 +3905,7 @@ Y_UNIT_TEST_SUITE(KqpNewEngine) { Y_UNIT_TEST(AutoChooseIndex) { TKikimrSettings settings; NKikimrConfig::TAppConfig appConfig; - appConfig.MutableTableServiceConfig()->SetIndexAutoChooseMode(NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_MAX_USED_PREFIX); + appConfig.MutableTableServiceConfig()->SetIndexAutoChooseMode(NKikimrConfig::TTableServiceConfig_EIndexAutoChooseMode_ONLY_POINTS); settings.SetAppConfig(appConfig); TKikimrRunner kikimr(settings); @@ -3919,7 +3919,7 @@ Y_UNIT_TEST_SUITE(KqpNewEngine) { auto result = session.ExecuteDataQuery(R"( --!syntax_v1 - SELECT * FROM `/Root/SecondaryKeys` WHERE Fk <= 1; + SELECT Fk, Key FROM `/Root/SecondaryKeys` WHERE Fk <= 1; )", TTxControl::BeginTx(TTxSettings::SerializableRW()), querySettings).GetValueSync(); AssertSuccessResult(result); AssertTableReads(result, "/Root/SecondaryKeys/Index/indexImplTable", 1); |