当前位置:网站首页>ClickHouse 二级索引
ClickHouse 二级索引
2022-08-04 22:56:00 【jasong】
1 ClickHouse稀疏索引
个人理解(就是目录,就是每页的关键字 + 加关键字所在位置
index(第n个索引,关键字) ,
mrk(偏移,偏移对应的位置offset)
index->mrk->bin)
2 二级索引
关键字 | 说明 |
|---|---|
index name | 索引别名 |
Index expression | 索引源字段 |
Type | minmax, set, bloom filter,map |
GRANULARITY | 索引粒度 ,如ClickHouse 默认稀疏索引默认是8192 ,我理解 8192*GRANULARITY就是 skip_index.mrk 的block 大小 |
skpidx{index_name}.idx | which contains the ordered expression values) |
skpidx{index_name}.mrk2 | which contains the corresponding offsets into the associated data column files. |
3 表索引设置
- use_skip_indexes (0 or 1, default 1). 默认过滤所有index
- force_data_skipping_indexes 强制使用哪个index
4 管理索引
ALTER TABLE [db].table_name [ON CLUSTER cluster] ADD INDEX name expression TYPE type GRANULARITY value [FIRST|AFTER name]- Adds index description to tables metadata.ALTER TABLE [db].table_name [ON CLUSTER cluster] DROP INDEX name- Removes index description from tables metadata and deletes index files from disk.ALTER TABLE [db.]table_name [ON CLUSTER cluster] MATERIALIZE INDEX name [IN PARTITION partition_name]- Rebuilds the secondary indexnamefor the specifiedpartition_name. Implemented as a mutation. IfIN PARTITIONpart is omitted then it rebuilds the index for the whole table data.
5 Example
CREATE TABLE table_name
(
u64 UInt64,
i32 Int32,
s String,
...
INDEX a (u64 * i32, s) TYPE minmax GRANULARITY 3,
INDEX b (u64 * length(s)) TYPE set(1000) GRANULARITY 4
) ENGINE = MergeTree()
SELECT count() FROM table WHERE s < 'z'
SELECT count() FROM table WHERE u64 * i32 == 10 AND u64 * length(s) >= 1234
CREATE TABLE data
(
key Int,
d1 Int,
d1_null Nullable(Int),
INDEX d1_idx d1 TYPE minmax GRANULARITY 1,
INDEX d1_null_idx assumeNotNull(d1_null) TYPE minmax GRANULARITY 1
)
Engine=MergeTree()
ORDER BY key;
SELECT * FROM data_01515;
SELECT * FROM data_01515 SETTINGS force_data_skipping_indices=''; -- query will produce CANNOT_PARSE_TEXT error.
SELECT * FROM data_01515 SETTINGS force_data_skipping_indices='d1_idx'; -- query will produce INDEX_NOT_USED error.
SELECT * FROM data_01515 WHERE d1 = 0 SETTINGS force_data_skipping_indices='d1_idx'; -- Ok.
SELECT * FROM data_01515 WHERE d1 = 0 SETTINGS force_data_skipping_indices='`d1_idx`'; -- Ok (example of full featured parser).
SELECT * FROM data_01515 WHERE d1 = 0 SETTINGS force_data_skipping_indices='`d1_idx`, d1_null_idx'; -- query will produce INDEX_NOT_USED error, since d1_null_idx is not used.
SELECT * FROM data_01515 WHERE d1 = 0 AND assumeNotNull(d1_null) = 0 SETTINGS force_data_skipping_indices='`d1_idx`, d1_null_idx'; -- Ok.6 索引类型
7 支持哪些函数
Function (operator) / Index | primary key | minmax | ngrambf_v1 | tokenbf_v1 | bloom_filter |
|---|---|---|---|---|---|
equals (=, ==) | |||||
notEquals(!=, <>) | |||||
like | * | ||||
notLike | * | ||||
startsWith | * | ||||
endsWith | * | * | * | ||
multiSearchAny | * | * | * | * | |
in | |||||
notIn | |||||
less (<) | * | * | * | ||
greater (>) | * | * | * | ||
lessOrEquals (<=) | * | * | * | ||
greaterOrEquals (>=) | * | * | * | ||
empty | * | * | * | ||
notEmpty | * | * | * | ||
hasToken | * | * | * | * |
8 Demo
https://clickhouse.com/docs/en/guides/improving-query-performance/skipping-indexes#skip-best-practices
1 创建 默认 8192 的稀疏索引
CREATE TABLE skip_table
(
my_key UInt64,
my_value UInt64
)
ENGINE MergeTree primary key my_key
SETTINGS index_granularity=8192;
INSERT INTO skip_table SELECT number, intDiv(number,4096) FROM numbers(100000000);
SELECT * FROM skip_table WHERE my_value IN (125, 700)
┌─my_key─┬─my_value─┐
│ 512000 │ 125 │
│ 512001 │ 125 │
│ ... | ... |
└────────┴──────────┘2 创建 8192 * 2 的二级索引
ALTER TABLE skip_table ADD INDEX vix my_value TYPE set(100) GRANULARITY 2;
/*ALTER TABLE xx ADD INDEX game_id_index game_id TYPE bloom_filter(0.01) GRANULARITY 1;*/3 生效历史数据
ALTER TABLE skip_table MATERIALIZE INDEX vix;4 验证
SELECT * FROM skip_table WHERE my_value IN (125, 700)
┌─my_key─┬─my_value─┐
│ 512000 │ 125 │
│ 512001 │ 125 │
│ ... | ... |
└────────┴──────────┘
8192 rows in set. Elapsed: 0.051 sec. Processed 32.77 thousand rows, 360.45 KB (643.75 thousand rows/s., 7.08 MB/s.)
see detail
SET send_logs_level='trace';
<Debug> default.skip_table (933d4b2c-8cea-4bf9-8c93-c56e900eefd1) (SelectExecutor): Index `vix` has dropped 6102/6104 granules.下方为图形解释,每个稀疏索引为 8192*2 ,索引每2两个Granule为一个Skip Index ,1 Block
边栏推荐
猜你喜欢

基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!

如何根据地址获取函数名

C5750X7R2E105K230KA(电容器)MSP430F5249IRGCR微控制器资料

postman接口测试

How to make a video gif?Try this video making gif artifact

功耗控制之DVFS介绍

各行各业都受到重创,游戏行业却如火如荼,如何加入游戏模型师职业

养殖虚拟仿真软件提供高沉浸式的虚拟场景互动操作体验学习

3D建模师为了让甲方爸爸过稿,还可以这么做,就是在赚血汗钱啊

直接插入排序
随机推荐
文章占位 文章占位
【字符串函数内功修炼】strlen + strstr + strtok + strerror(三)
现在学习次世代3D游戏建模还能找到高薪好工作吗
Reconfigure the ffmpeg plugin in chrome
【游戏建模模型制作全流程】使用ZBrush制作骷髅王
最温馨的家园
【软件测试】常用ADB命令
js中小数四则运算精度问题原因及解决办法
typeScript-promise
BUG | The interface returns abnormal data
xss总结
BUG | 接口返回异常数据
【3D建模制作技巧分享】在zbrush中如何雕刻头发 ZBrush头发雕刻小技巧
【2020】【论文笔记】超表面:多功能和可编程——
得不到你的心,就用“分布式锁”锁住你的人
【3D建模制作技巧分享】ZBrush如何使用Z球
各行各业都受到重创,游戏行业却如火如荼,如何加入游戏模型师职业
ANT1.7下载以及配置方法
TypeScript - the use of closure functions
panic: reflect: reflect.Value.SetString using value obtained using unexported field