閱讀690 返回首頁    go 技術社區[雲棲]


Greenplum列存壓縮表索引機製

列存壓縮表,簡稱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,然後取出數據。

掃描方式的選擇

為什麼AOCS表使用的索引方法是Bitmap Index Scan,而不是我們常見的Index Scan呢?

AO表的掃描方向隻能從前往後,而不能從後往前,heap表從前往後、從後往前都是支持的。通過索引找到的數據在AO文件位置並不是從前往後順序的。如圖所示,假設我們的條件是id<=7,通過索引找到的記錄的順序是1,3,5,7。如果是Index Scan,那麼就要先從fileOffset位置掃描到第三個位置找到value=1,然後繼續掃描到第四個位置value=3,然後繼續從fileOffset位置開始掃描第一個位置value=5,繼續掃描到第二個位置value=7,可以看到使用Index Scan可能會有多次回頭重新開始掃描,增加了IO。為了避免這個問題,隻使用Bitmap Index Scan,將會先掃描所有滿足索引的值,然後按照tid排序,按照rownum從小到大掃描,一次從前往後掃描就可以得到索引對應的值了。

f3c120767819e6fa.png

最後更新:2017-08-13 22:24:15

  上一篇:go  雲服務器 ECS 建站教程:快速搭建 Moodle 課程管理係統
  下一篇:go  SEO優化技巧:如何打造高流量網站