重新聚簇表
更新時間 2025-02-05 09:36:58
最近更新時間: 2025-02-05 09:36:58
分享文章
TeleDB支持按某個索引重新聚簇表,即按某個索引重新排序表數據,這樣在使用到這個索引時,可以掃描更少的頁,提高SQL效率。本文為您介紹該場景下的測試情況。
注意聚簇表需要重寫表,可能會對業務有影響;聚簇完成后新寫入的數據不會再按聚簇的索引排序,對于數據變更頻繁的表并不適用。
以下是關于該場景的測試情況:
-
重新聚簇前SQL執行計劃和耗時:
可以看到聚簇前要掃描的block數為5869(Buffers: shared hit=5869),耗時25ms。
teledb=# explain (analyze,buffers) select count(1) from t1 where f2=1;" QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=7208.99..7209.00 rows=1 width=8) (actual time=25.505..25.505 rows=1 loops=1) Buffers: shared hit=5869 -> Bitmap Heap Scan on t1 (cost=124.87..7185.62 rows=9348 width=0) (actual time=3.567..23.002 rows=10051 loops=1) Recheck Cond: (f2 = 1) Heap Blocks: exact=5838 Buffers: shared hit=5869 -> Bitmap Index Scan on t1_f2_idx (cost=0.00..122.53 rows=9348 width=0) (actual time=2.405..2.405 rows=10051 loops=1) Index Cond: (f2 = 1) Buffers: shared hit=31 Planning time: 0.626 ms Execution time: 25.659 ms (11 rows) -
按索引t1_f2_idx重新聚簇:
teledb=# CLUSTER t1 USING t1_f2_idx; CLUSTER -
重新聚簇后SQL執行計劃和耗時:
可以看到聚簇后掃描的block數為116(Buffers: shared hit=116),耗時9.9ms。性能提升了2倍以上。
teledb=# explain (analyze,buffers) select count(1) from t1 where f2=1; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=7201.23..7201.24 rows=1 width=8) (actual time=9.808..9.808 rows=1 loops=1) Buffers: shared hit=116 -> Bitmap Heap Scan on t1 (cost=124.87..7177.86 rows=9348 width=0) (actual time=1.312..7.348 rows=10051 loops=1) Recheck Cond: (f2 = 1) Heap Blocks: exact=85 Buffers: shared hit=116 -> Bitmap Index Scan on t1_f2_idx (cost=0.00..122.53 rows=9348 width=0) (actual time=1.219..1.219 rows=10051 loops=1) Index Cond: (f2 = 1) Buffers: shared hit=31 Planning time: 0.696 ms Execution time: 9.969 ms (11 rows)