perf(mito2): prune files by their primary key range before reading footers (#10564)

* perf(mito2): prune files by their primary key range before reading footers

File-level pruning used only the manifest time range, so a scan read the footer of every file in the time range even when the file's primary key range excludes the predicate. The file-level pruning statistics now include the min and max of the first primary key column, decoded from FileMeta's primary key range, so such files are skipped without a footer read. For metric engine physical regions the first column is the table id.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test(mito2): cover file pruning by primary key range end to end

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* perf(mito2): decode the file primary key range only when pruning uses it

Predicates on the time index or fields never ask for the first primary
key column, so they no longer build a codec and two arrays per file.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* fix(mito2): prune files by primary key ranges loaded from footers

Region open can fill a file handle's key range from the SST footer when
the manifest has none. Let the handle decide whether bounds are known,
so those files are pruned too.

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

* test: check file pruning by primary key range with EXPLAIN ANALYZE

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
This commit is contained in:
dennis zhuang
2026-11-23 08:04:54 +00:00
committed by GitHub
parent 0ce4799e48
commit 2effff48d5
5 changed files with 335 additions and 15 deletions
@@ -197,3 +197,138 @@ DROP TABLE test_time_filter;
Affected Rows: 0
-- Files whose primary key range excludes the predicate are pruned before reading them.
CREATE TABLE pk_range_prune(ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, idc STRING, PRIMARY KEY(host, idc));
Affected Rows: 0
INSERT INTO pk_range_prune VALUES (1, 1, 'a', 'idc1'), (2, 2, 'b', 'idc2');
Affected Rows: 2
ADMIN flush_table('pk_range_prune');
+-------------------------------------+
| ADMIN flush_table('pk_range_prune') |
+-------------------------------------+
| 0 |
+-------------------------------------+
INSERT INTO pk_range_prune VALUES (1, 3, 'c', 'idc1'), (2, 4, 'd', 'idc2');
Affected Rows: 2
ADMIN flush_table('pk_range_prune');
+-------------------------------------+
| ADMIN flush_table('pk_range_prune') |
+-------------------------------------+
| 0 |
+-------------------------------------+
-- SQLNESS REPLACE (?s)^.*("files_time_range_pruned":\d+).*$ $1
EXPLAIN ANALYZE VERBOSE SELECT * FROM pk_range_prune WHERE host = 'c';
"files_time_range_pruned":1
SELECT * FROM pk_range_prune WHERE host = 'c';
+-------------------------+-----+------+------+
| ts | val | host | idc |
+-------------------------+-----+------+------+
| 1970-01-01T00:00:00.001 | 3.0 | c | idc1 |
+-------------------------+-----+------+------+
SELECT * FROM pk_range_prune WHERE host >= 'b' ORDER BY host;
+-------------------------+-----+------+------+
| ts | val | host | idc |
+-------------------------+-----+------+------+
| 1970-01-01T00:00:00.002 | 2.0 | b | idc2 |
| 1970-01-01T00:00:00.001 | 3.0 | c | idc1 |
| 1970-01-01T00:00:00.002 | 4.0 | d | idc2 |
+-------------------------+-----+------+------+
SELECT * FROM pk_range_prune WHERE host < 'b';
+-------------------------+-----+------+------+
| ts | val | host | idc |
+-------------------------+-----+------+------+
| 1970-01-01T00:00:00.001 | 1.0 | a | idc1 |
+-------------------------+-----+------+------+
DROP TABLE pk_range_prune;
Affected Rows: 0
CREATE TABLE pk_range_phy (ts TIMESTAMP TIME INDEX, val DOUBLE) ENGINE = metric WITH ("physical_metric_table" = "");
Affected Rows: 0
CREATE TABLE pk_range_t1 (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING PRIMARY KEY) ENGINE = metric WITH ("on_physical_table" = "pk_range_phy");
Affected Rows: 0
CREATE TABLE pk_range_t2 (ts TIMESTAMP TIME INDEX, val DOUBLE, job STRING PRIMARY KEY) ENGINE = metric WITH ("on_physical_table" = "pk_range_phy");
Affected Rows: 0
INSERT INTO pk_range_t1 VALUES ('host1', 0, 1), ('host2', 1, 2);
Affected Rows: 2
ADMIN flush_table('pk_range_phy');
+-----------------------------------+
| ADMIN flush_table('pk_range_phy') |
+-----------------------------------+
| 0 |
+-----------------------------------+
INSERT INTO pk_range_t2 VALUES ('job1', 0, 3), ('job2', 1, 4);
Affected Rows: 2
ADMIN flush_table('pk_range_phy');
+-----------------------------------+
| ADMIN flush_table('pk_range_phy') |
+-----------------------------------+
| 0 |
+-----------------------------------+
-- SQLNESS REPLACE (?s)^.*("files_time_range_pruned":\d+).*$ $1
EXPLAIN ANALYZE VERBOSE SELECT * FROM pk_range_t1;
"files_time_range_pruned":1
SELECT * FROM pk_range_t1 ORDER BY host;
+-------+-------------------------+-----+
| host | ts | val |
+-------+-------------------------+-----+
| host1 | 1970-01-01T00:00:00 | 1.0 |
| host2 | 1970-01-01T00:00:00.001 | 2.0 |
+-------+-------------------------+-----+
SELECT * FROM pk_range_t2 ORDER BY job;
+------+-------------------------+-----+
| job | ts | val |
+------+-------------------------+-----+
| job1 | 1970-01-01T00:00:00 | 3.0 |
| job2 | 1970-01-01T00:00:00.001 | 4.0 |
+------+-------------------------+-----+
DROP TABLE pk_range_t1;
Affected Rows: 0
DROP TABLE pk_range_t2;
Affected Rows: 0
DROP TABLE pk_range_phy;
Affected Rows: 0
@@ -65,3 +65,52 @@ SELECT host FROM test_time_filter WHERE greptime_timestamp > '2026-01-07 00:00:0
SELECT host, greptime_timestamp FROM test_time_filter WHERE greptime_timestamp > '2026-01-07 00:00:00';
DROP TABLE test_time_filter;
-- Files whose primary key range excludes the predicate are pruned before reading them.
CREATE TABLE pk_range_prune(ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING, idc STRING, PRIMARY KEY(host, idc));
INSERT INTO pk_range_prune VALUES (1, 1, 'a', 'idc1'), (2, 2, 'b', 'idc2');
ADMIN flush_table('pk_range_prune');
INSERT INTO pk_range_prune VALUES (1, 3, 'c', 'idc1'), (2, 4, 'd', 'idc2');
ADMIN flush_table('pk_range_prune');
-- SQLNESS REPLACE (?s)^.*("files_time_range_pruned":\d+).*$ $1
EXPLAIN ANALYZE VERBOSE SELECT * FROM pk_range_prune WHERE host = 'c';
SELECT * FROM pk_range_prune WHERE host = 'c';
SELECT * FROM pk_range_prune WHERE host >= 'b' ORDER BY host;
SELECT * FROM pk_range_prune WHERE host < 'b';
DROP TABLE pk_range_prune;
CREATE TABLE pk_range_phy (ts TIMESTAMP TIME INDEX, val DOUBLE) ENGINE = metric WITH ("physical_metric_table" = "");
CREATE TABLE pk_range_t1 (ts TIMESTAMP TIME INDEX, val DOUBLE, host STRING PRIMARY KEY) ENGINE = metric WITH ("on_physical_table" = "pk_range_phy");
CREATE TABLE pk_range_t2 (ts TIMESTAMP TIME INDEX, val DOUBLE, job STRING PRIMARY KEY) ENGINE = metric WITH ("on_physical_table" = "pk_range_phy");
INSERT INTO pk_range_t1 VALUES ('host1', 0, 1), ('host2', 1, 2);
ADMIN flush_table('pk_range_phy');
INSERT INTO pk_range_t2 VALUES ('job1', 0, 3), ('job2', 1, 4);
ADMIN flush_table('pk_range_phy');
-- SQLNESS REPLACE (?s)^.*("files_time_range_pruned":\d+).*$ $1
EXPLAIN ANALYZE VERBOSE SELECT * FROM pk_range_t1;
SELECT * FROM pk_range_t1 ORDER BY host;
SELECT * FROM pk_range_t2 ORDER BY job;
DROP TABLE pk_range_t1;
DROP TABLE pk_range_t2;
DROP TABLE pk_range_phy;