Files
greptimedb/tests/cases/standalone/common/select/prune.sql
T
dennis zhuang b26b87f914 perf(mito2): split seq scan ranges of files with disjoint primary keys (#10561)
* perf(mito2): split seq scan ranges of files with disjoint primary keys

Seq scan merged every SST whose time range overlapped another into one
partition range. Compaction outputs split by max_output_file_size cover
the same time range with disjoint primary keys, so a compacted window was
read and deduplicated by a single partition.

Ranges are now merged only when both their time ranges and their
primary key ranges overlap. Memtables and files without key statistics
still overlap every key.

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

* perf(mito2): index open seq scan groups by primary key

Grouping compared each range with every open group, so n files with
disjoint keys in one time window took n(n-1) checks. Open groups now
sit in a BTreeMap keyed by their min primary key; their key ranges are
disjoint, so one range lookup finds every group to merge. Groups that
ended before the current range are closed when a lookup reaches them.

Per-series scans keep grouping ranges by time only.

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

* perf(mito2): merge the smaller seq scan range into the larger one

A range that joined many groups, such as a memtable overlapping many
files with disjoint keys, was appended to each group in turn and copied
its growing index lists every time. Merges now append the smaller range
to the larger one, so each index is copied O(log n) times.

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

* docs(mito2): clarify the seq scan grouping invariant and lookup cutoff

State that grouping keeps every version of a (primary key, timestamp)
pair together, instead of "ranges in different groups never share a
key", which read as if one key could not appear in two groups. Explain
why the reverse BTreeMap walk can stop early: disjoint key ranges order
their upper bounds the same as their lower bounds.

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

* fix(mito2): split seq scan ranges by primary key only without a distribution

TimeWindowed scans advertise (ts, primary key) ordering. Key-disjoint
ranges that partition assignment reorders inside one partition would not
keep it, so scans with any distribution keep grouping ranges by time.

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

* docs(mito2): state that SeqScan does not keep the TimeWindowed ordering today

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

* fix(mito2): locate the primary key column by its top-level path

A nested field such as a JSON child named __primary_key has the same leaf name and comes first, so the bounds and the primary key projection could read the wrong column.

Closes #10582

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

* refactor(mito2): rename files_time_range_pruned to files_manifest_pruned

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

---------

Signed-off-by: Dennis Zhuang <killme2008@gmail.com>
2026-11-23 12:48:21 +00:00

117 lines
3.9 KiB
SQL

create table demo(ts timestamp time index, `value` double, host string,idc string, collector string, primary key(host, idc, collector));
insert into demo values(1,2,'test1', 'idc1', 'disk') ,(2,3,'test2', 'idc1', 'disk'), (3,4,'test3', 'idc2','memory');
select * from demo where host='test1';
select * from demo where host='test2';
select * from demo where host='test3';
select * from demo where host='test2' and idc='idc1';
select * from demo where host='test2' and idc='idc1' and collector='disk';
select * from demo where host='test2' and idc='idc2';
select * from demo where host='test3' and idc>'idc1';
select * from demo where idc='idc1' order by ts;
select * from demo where collector='disk' order by ts;
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
-- SQLNESS REPLACE \\?"(dyn_filters|time_range|file_summary|max_part_range)\\?":.* "$1":REDACTED
explain analyze select * from demo where idc='idc1';
SELECT * FROM demo where host in ('test1');
-- SQLNESS REPLACE (metrics.*) REDACTED
-- SQLNESS REPLACE (RoundRobinBatch.*) REDACTED
-- SQLNESS REPLACE (-+) -
-- SQLNESS REPLACE (\s\s+) _
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE region=\d+\(\d+,\s+\d+\) region=REDACTED
-- SQLNESS REPLACE \\?"(dyn_filters|time_range|file_summary|max_part_range)\\?":.* "$1":REDACTED
explain analyze SELECT * FROM demo where host in ('test1');
drop table demo;
CREATE TABLE test_time_filter(
host STRING,
greptime_timestamp TIMESTAMP,
TIME INDEX(greptime_timestamp)
)
WITH
(
'append_mode' = 'true',
'sst_format' = 'flat'
);
INSERT INTO test_time_filter(host, greptime_timestamp) VALUES ('hello', '2026-01-07T00:00:00'), ('world', '2026-01-07T00:00:01'), ('test', '2026-01-07T00:00:00'), ('go', '2026-01-07T00:00:01');
SELECT host FROM test_time_filter WHERE greptime_timestamp > '2026-01-07 00:00:00';
SELECT host, greptime_timestamp FROM test_time_filter WHERE greptime_timestamp > '2026-01-07 00:00:00';
ADMIN flush_table('test_time_filter');
SELECT host FROM test_time_filter WHERE greptime_timestamp > '2026-01-07 00:00:00';
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_manifest_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_manifest_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;