Merge pull request #60675 from ClickHouse/use-minmax-indices-always-first

Always apply first minmax index among available skip indices
This commit is contained in:
Igor Nikonov 2024-03-06 21:25:49 +01:00 committed by GitHub
commit a3c22f9ace
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 35 additions and 11 deletions

View File

@ -43,6 +43,7 @@
#include <Processors/QueryPlan/IQueryPlanStep.h>
#include <Parsers/parseIdentifierOrStringLiteral.h>
#include <Parsers/ExpressionListParsers.h>
#include <Storages/MergeTree/MergeTreeIndexMinMax.h>
#include <algorithm>
#include <iterator>
@ -1421,6 +1422,20 @@ static void buildIndexes(
}
}
// move minmax indices to first positions, so they will be applied first as cheapest ones
std::stable_sort(begin(skip_indexes.useful_indices), end(skip_indexes.useful_indices), [](const auto & l, const auto & r)
{
const bool l_min_max = (typeid_cast<const MergeTreeIndexMinMax *>(l.index.get()));
const bool r_min_max = (typeid_cast<const MergeTreeIndexMinMax *>(r.index.get()));
if (l_min_max == r_min_max)
return false;
if (l_min_max)
return true; // left is min max but right is not
return false; // right is min max but left is not
});
indexes->skip_indexes = std::move(skip_indexes);
}

View File

@ -2,7 +2,6 @@
#include <base/types.h>
#include <memory>
#include <vector>
#include <Core/Field.h>
#include <Interpreters/ExpressionActions.h>

View File

@ -2,7 +2,6 @@
#include <Storages/MergeTree/MergeTreeIndices.h>
#include <Storages/MergeTree/MergeTreeData.h>
#include <memory>
namespace DB
{

View File

@ -4,8 +4,6 @@
#include <Storages/MergeTree/MergeTreeData.h>
#include <Storages/MergeTree/KeyCondition.h>
#include <memory>
namespace DB
{

View File

@ -5,9 +5,6 @@
#include <Interpreters/SetVariants.h>
#include <memory>
#include <set>
namespace DB
{

View File

@ -1,12 +1,9 @@
#pragma once
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <memory>
#include <utility>
#include <mutex>
#include <Core/Block.h>
#include <Storages/StorageInMemoryMetadata.h>
#include <Storages/MergeTree/GinIndexStore.h>

View File

@ -1,5 +1,4 @@
#pragma once
#include <tuple>
#include <Storages/MarkCache.h>
#include <Storages/MergeTree/MarkRange.h>
#include <Storages/MergeTree/MergeTreeData.h>

View File

@ -0,0 +1,2 @@
Name: v_mm
Name: v_set

View File

@ -0,0 +1,18 @@
DROP TABLE IF EXISTS skip_table;
CREATE TABLE skip_table
(
k UInt64,
v UInt64,
INDEX v_set v TYPE set(100) GRANULARITY 2, -- set index is declared before minmax intentionally
INDEX v_mm v TYPE minmax GRANULARITY 2
)
ENGINE = MergeTree
PRIMARY KEY k
SETTINGS index_granularity = 8192;
INSERT INTO skip_table SELECT number, intDiv(number, 4096) FROM numbers(100000);
SELECT trim(explain) FROM ( EXPLAIN indexes = 1 SELECT * FROM skip_table WHERE v = 125) WHERE explain like '%Name%';
DROP TABLE skip_table;