ClickHouse/dbms/tests/queries/0_stateless/00926_adaptive_index_granularity.sql
2019-03-19 13:19:40 +03:00

151 lines
5.9 KiB
SQL

----- Group of very similar simple tests ------
DROP TABLE IF EXISTS test.zero_rows_per_granule;
CREATE TABLE test.zero_rows_per_granule (
p Date,
k UInt64,
v1 UInt64,
v2 Int64
) ENGINE MergeTree() PARTITION BY toYYYYMM(p) ORDER BY k SETTINGS index_granularity_bytes = 20;
INSERT INTO test.zero_rows_per_granule (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 2, 3000, 4000), ('2018-05-17', 3, 5000, 6000), ('2018-05-18', 4, 7000, 8000);
SELECT COUNT(*) FROM test.zero_rows_per_granule;
SELECT distinct(marks) from system.parts WHERE table = 'zero_rows_per_granule' and database='test';
OPTIMIZE TABLE test.zero_rows_per_granule FINAL;
INSERT INTO test.zero_rows_per_granule (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 5, 3000, 4000), ('2018-05-17', 6, 5000, 6000), ('2018-05-19', 7, 7000, 8000);
SELECT COUNT(*) FROM test.zero_rows_per_granule;
SELECT distinct(marks) from system.parts WHERE table = 'zero_rows_per_granule' and database='test';
DROP TABLE IF EXISTS test.zero_rows_per_granule;
-----
DROP TABLE IF EXISTS test.two_rows_per_granule;
CREATE TABLE test.two_rows_per_granule (
p Date,
k UInt64,
v1 UInt64,
v2 Int64
) ENGINE MergeTree() PARTITION BY toYYYYMM(p) ORDER BY k SETTINGS index_granularity_bytes = 40;
INSERT INTO test.two_rows_per_granule (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 2, 3000, 4000), ('2018-05-17', 3, 5000, 6000), ('2018-05-18', 4, 7000, 8000);
SELECT COUNT(*) FROM test.two_rows_per_granule;
SELECT distinct(marks) from system.parts WHERE table = 'two_rows_per_granule' and database='test';
OPTIMIZE TABLE test.two_rows_per_granule FINAL;
INSERT INTO test.two_rows_per_granule (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 5, 3000, 4000), ('2018-05-17', 6, 5000, 6000), ('2018-05-19', 7, 7000, 8000);
SELECT COUNT(*) FROM test.two_rows_per_granule;
SELECT distinct(marks) from system.parts WHERE table = 'two_rows_per_granule' and database='test';
DROP TABLE IF EXISTS test.two_rows_per_granule;
------
DROP TABLE IF EXISTS test.four_rows_per_granule;
CREATE TABLE test.four_rows_per_granule (
p Date,
k UInt64,
v1 UInt64,
v2 Int64
) ENGINE MergeTree() PARTITION BY toYYYYMM(p) ORDER BY k SETTINGS index_granularity_bytes = 110;
INSERT INTO test.four_rows_per_granule (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 2, 3000, 4000), ('2018-05-17', 3, 5000, 6000), ('2018-05-18', 4, 7000, 8000);
SELECT COUNT(*) FROM test.four_rows_per_granule;
SELECT distinct(marks) from system.parts WHERE table = 'four_rows_per_granule' and database='test';
OPTIMIZE TABLE test.four_rows_per_granule FINAL;
INSERT INTO test.four_rows_per_granule (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 5, 3000, 4000), ('2018-05-17', 6, 5000, 6000), ('2018-05-19', 7, 7000, 8000);
SELECT COUNT(*) FROM test.four_rows_per_granule;
SELECT distinct(marks) from system.parts WHERE table = 'four_rows_per_granule' and database='test';
DROP TABLE IF EXISTS test.four_rows_per_granule;
----- More interesting tests ------
DROP TABLE IF EXISTS test.huge_granularity_small_blocks;
CREATE TABLE test.huge_granularity_small_blocks (
p Date,
k UInt64,
v1 UInt64,
v2 Int64
) ENGINE MergeTree() PARTITION BY toYYYYMM(p) ORDER BY k SETTINGS index_granularity_bytes = 1000000;
INSERT INTO test.huge_granularity_small_blocks (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 2, 3000, 4000), ('2018-05-17', 3, 5000, 6000), ('2018-05-18', 4, 7000, 8000);
SELECT COUNT(*) FROM test.huge_granularity_small_blocks;
SELECT distinct(marks) from system.parts WHERE table = 'huge_granularity_small_blocks' and database='test';
INSERT INTO test.huge_granularity_small_blocks (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 5, 3000, 4000), ('2018-05-17', 6, 5000, 6000), ('2018-05-19', 7, 7000, 8000);
OPTIMIZE TABLE test.huge_granularity_small_blocks FINAL;
SELECT COUNT(*) FROM test.huge_granularity_small_blocks;
SELECT distinct(marks) from system.parts WHERE table = 'huge_granularity_small_blocks' and database='test';
DROP TABLE IF EXISTS test.huge_granularity_small_blocks;
----- Some alter tests ----
DROP TABLE IF EXISTS test.adaptive_granularity_alter;
CREATE TABLE test.adaptive_granularity_alter (
p Date,
k UInt64,
v1 UInt64,
v2 Int64
) ENGINE MergeTree() PARTITION BY toYYYYMM(p) ORDER BY k SETTINGS index_granularity_bytes = 110;
INSERT INTO test.adaptive_granularity_alter (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 2, 3000, 4000), ('2018-05-17', 3, 5000, 6000), ('2018-05-18', 4, 7000, 8000);
SELECT COUNT(*) FROM test.adaptive_granularity_alter;
SELECT distinct(marks) from system.parts WHERE table = 'adaptive_granularity_alter' and database='test';
OPTIMIZE TABLE test.adaptive_granularity_alter FINAL;
ALTER TABLE test.adaptive_granularity_alter MODIFY COLUMN v1 Int16;
SELECT COUNT(*) FROM test.adaptive_granularity_alter;
SELECT distinct(marks) from system.parts WHERE table = 'adaptive_granularity_alter' and database='test';
INSERT INTO test.adaptive_granularity_alter (p, k, v1, v2) VALUES ('2018-05-15', 1, 1000, 2000), ('2018-05-16', 5, 3000, 4000), ('2018-05-17', 6, 5000, 6000), ('2018-05-19', 42, 42, 42);
SELECT COUNT(*) FROM test.adaptive_granularity_alter;
SELECT distinct(marks) from system.parts WHERE table = 'adaptive_granularity_alter' and database='test';
ALTER TABLE test.adaptive_granularity_alter MODIFY COLUMN v2 String;
INSERT INTO test.adaptive_granularity_alter (p, k, v1, v2) VALUES ('2018-05-15', 100, 1000, 'aaaa'), ('2018-05-16', 101, 3000, 'bbbb'), ('2018-05-17', 102, 5000, 'cccc'), ('2018-05-19', 103, 7000, 'dddd');
OPTIMIZE TABLE test.adaptive_granularity_alter FINAL;
SELECT k, v2 FROM test.adaptive_granularity_alter WHERE k >= 100 OR k = 42;
SELECT sum(marks) from system.parts WHERE table = 'adaptive_granularity_alter' and database='test';
DROP TABLE IF EXISTS test.adaptive_granularity_alter;