原 GreenPlum中如何根据统计信息计算值的分步情况
Tags: 原创PGGreenPlumPostgreSQL优化统计信息
前言
若有一张特大表,则做group by或where过滤是非常慢的,这个时候可以通过统计信息来快速获取一些数据。
更新统计信息
1 2 | ANALYZE pg_class; vacuum (verbose,ANALYZE) pg_class; |
查询统计信息
1 2 3 | select a.n_distinct,a.most_common_vals,most_common_freqs from pg_stats a where a.tablename='pg_class' and a.attname='relhasindex'; select count(*) from pg_class a where a.relhasindex='t'; select count(*) from pg_class; |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | postgres=# select a.n_distinct,a.most_common_vals,most_common_freqs from pg_stats a where a.tablename='pg_class' and a.attname='relhasindex'; n_distinct | most_common_vals | most_common_freqs ------------+------------------+------------------- 2 | {f,t} | {0.75,0.25} (1 row) postgres=# select count(*) from pg_class a where a.relhasindex='t'; count ------- 101 (1 row) postgres=# select count(*) from pg_class; count ------- 404 (1 row) postgres=# select trunc(101.00/404.00,2) ; trunc ------- 0.25 (1 row) |