Skip to content

Commit d29b05f

Browse files
committed
Add ProfileEvents for index search algorithm
Update 03518_key_condition_binary_search stateless test to use the new ProfileEvents instead of relying on log traces.
1 parent 7d50e5d commit d29b05f

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

src/Common/ProfileEvents.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,8 @@ The server successfully detected this situation and will download merged part fr
10271027
M(FilterTransformPassedRows, "Number of rows that passed the filter in the query", ValueType::Number) \
10281028
M(FilterTransformPassedBytes, "Number of bytes that passed the filter in the query", ValueType::Bytes) \
10291029
M(QueryPreempted, "How many times tasks are paused and waiting due to 'priority' setting", ValueType::Number) \
1030+
M(IndexBinarySearchAlgorithm, "Number of times the binary search algorithm is used over the index marks", ValueType::Number) \
1031+
M(IndexGenericExclusionSearchAlgorithm, "Number of times the generic exclusion search algorithm is used over the index marks", ValueType::Number) \
10301032

10311033

10321034
#ifdef APPLY_FOR_EXTERNAL_EVENTS

src/Storages/MergeTree/MergeTreeDataSelectExecutor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include <Common/CurrentMetrics.h>
5656
#include <Common/ElapsedTimeProfileEventIncrement.h>
5757
#include <Common/FailPoint.h>
58+
#include <Common/ProfileEvents.h>
5859
#include <Common/quoteString.h>
5960

6061
#include <IO/WriteBufferFromOStream.h>
@@ -72,6 +73,8 @@ namespace ProfileEvents
7273
{
7374
extern const Event FilteringMarksWithPrimaryKeyMicroseconds;
7475
extern const Event FilteringMarksWithSecondaryKeysMicroseconds;
76+
extern const Event IndexBinarySearchAlgorithm;
77+
extern const Event IndexGenericExclusionSearchAlgorithm;
7578
}
7679

7780
namespace DB
@@ -1474,6 +1477,7 @@ MarkRanges MergeTreeDataSelectExecutor::markRangesFromPKRange(
14741477
}
14751478
}
14761479

1480+
ProfileEvents::increment(ProfileEvents::IndexGenericExclusionSearchAlgorithm);
14771481
LOG_TRACE(
14781482
log,
14791483
"Used generic exclusion search {}over index for part {} with {} steps",
@@ -1487,6 +1491,7 @@ MarkRanges MergeTreeDataSelectExecutor::markRangesFromPKRange(
14871491
/// we can use binary search algorithm to find the left and right endpoint key marks of such interval.
14881492
/// The returned value is the minimum range of marks, containing all keys for which KeyCondition holds
14891493

1494+
ProfileEvents::increment(ProfileEvents::IndexBinarySearchAlgorithm);
14901495
LOG_TRACE(log, "Running binary search on index range for part {} ({} marks)", part_name, marks_count);
14911496

14921497
size_t steps = 0;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
3
2-
1
1+
3 0
2+
0 1

tests/queries/0_stateless/03518_key_condition_binary_search.sh

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
44
# shellcheck source=../shell_config.sh
55
. "$CUR_DIR"/../shell_config.sh
66

7+
readonly query_prefix=$CLICKHOUSE_DATABASE
8+
79
$CLICKHOUSE_CLIENT -n -q "
810
DROP TABLE IF EXISTS t;
911
CREATE TABLE t
@@ -12,11 +14,11 @@ CREATE TABLE t
1214
)
1315
ENGINE = MergeTree
1416
ORDER BY c;
15-
INSERT INTO t values('One');
16-
SELECT * FROM t WHERE c = 1 settings send_logs_level='trace';
17-
SELECT * FROM t WHERE c = 'One' settings send_logs_level='trace';
18-
SELECT * FROM t WHERE c = 1 and 1 = 1 settings send_logs_level='trace';
19-
" 2>&1 | grep -c "binary search"
17+
INSERT INTO t values('One');"
18+
19+
$CLICKHOUSE_CLIENT -n -q "SELECT * FROM t WHERE c = 1 FORMAT Null;" --query_id="${query_prefix}_binary1"
20+
$CLICKHOUSE_CLIENT -n -q "SELECT * FROM t WHERE c = 'One' FORMAT Null;" --query_id="${query_prefix}_binary2"
21+
$CLICKHOUSE_CLIENT -n -q "SELECT * FROM t WHERE c = 1 and 1 = 1 FORMAT Null;" --query_id="${query_prefix}_binary3"
2022

2123
$CLICKHOUSE_CLIENT -n -q "
2224
DROP TABLE IF EXISTS t1;
@@ -26,6 +28,12 @@ CREATE TABLE t1
2628
)
2729
ENGINE = MergeTree
2830
ORDER BY timestamp;
29-
INSERT INTO t1 VALUES ('2025-05-21 00:00:00');
30-
SELECT * FROM t1 WHERE toDayOfMonth(timestamp) = 1 settings send_logs_level='trace';
31-
" 2>&1 | grep -c "generic exclusion search"
31+
INSERT INTO t1 VALUES ('2025-05-21 00:00:00');"
32+
33+
$CLICKHOUSE_CLIENT -n -q "SELECT * FROM t1 WHERE toDayOfMonth(timestamp) = 1 FORMAT Null;" --query-id="${query_prefix}_generic"
34+
$CLICKHOUSE_CLIENT -n -q "SYSTEM FLUSH LOGS;"
35+
36+
$CLICKHOUSE_CLIENT -n -q "SELECT sum(ProfileEvents['IndexBinarySearchAlgorithm']), sum(ProfileEvents['IndexGenericExclusionSearchAlgorithm']) FROM system.query_log
37+
WHERE type > 1 AND event_date >= yesterday() AND query_id ILIKE '${query_prefix}_binary%' AND current_database = currentDatabase()"
38+
$CLICKHOUSE_CLIENT -n -q "SELECT sum(ProfileEvents['IndexBinarySearchAlgorithm']), sum(ProfileEvents['IndexGenericExclusionSearchAlgorithm']) FROM system.query_log
39+
WHERE type > 1 AND event_date >= yesterday() AND query_id ILIKE '${query_prefix}_generic%' AND current_database = currentDatabase()"

0 commit comments

Comments
 (0)