列存压缩表,简称AOCS表
数据生成
create table testao(date text, time text, open float, high float, low float, volume int) with(APPENDONLY=true,ORIENTATION=column);
create index testao_idx on testao using btree (volume);
insert into testao select t, t, t, t, t, t from generate_series(1, 1000000) as t;
现象
执行计划如下:
postgres=> explain select * from testao where volume = 100 limit 1;
QUERY PLAN
------------------------------------------------------------------------------------------------------------
Limit (cost=100.95..200.98 rows=1 width=40)
-> Gather Motion 4:1 (slice1; segments: 4) (cost=100.95..200.98 rows=1 width=40)
-> Limit (cost=100.95..200.96 rows=1 width=40)
-> Bitmap Append-Only Column-Oriented Scan on testao (cost=100.95..200.96 rows=1 width=40)
Recheck Cond: volume = 100
-> Bitmap Index Scan on testao_idx (cost=0.00..100.95 rows=1 width=0)
Index Cond: volume = 100
Settings: effective_cache_size=8GB; gp_statistics_use_fkeys=on
Optimizer status: legacy query optimizer
(9 rows)
我们看到使用Bitmap Index Scan索引扫描
如何通过索引找到数据
索引页包含记录的tid,而tid包含segfileno和rownum信息,通过segfileno可以定位到文件,通过rownum可以定位到block及具体值。
如何通过rownum快速定位到block
对于索引,GP将会创建一个pg_aoblkdi_oid辅助表(block directory),里面包含每个block在文件的偏移位置fileOffset、segfileno、firstRowNum,并在firstRowNum列上创建索引,只要给出一个rownum,通过索引在pg_aoblkdi_oid辅助表中可以快速得到block在文件的偏移位置fileOffset,然后取出数据。