微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL default_statistics_target

  default_statistics_target(integer): Postgresql进行analyze的时候,参考的生成的列的柱状图的大小,可以理解为采样颗粒度。
  官方解释: Sets the default statistics target for table columns without a column-specific target set via ALTER TABLE SET STATISTICS. Larger values increase the time needed to do ANALYZE,but might improve the quality of the planner’s estimates. The default is 100. For more information on the use of statistics by the Postgresql query planner,refer to Section 14.2.
  就是说列没有通过ALTER TABLE SET STATISTICS语句进行自定义statistics target值,那么在analyze该列的时候,Postgresql会取default_statistics_target的值来替代。当default_statistics_target的值越高,那么Postgresql进行统计的约精确,当然,花费的时间也就越长了。
default_statistics_target参数可以在postgresql.conf文件中配置,如下

default_statistics_target = 100 # range 1-10000

范围要求是1到10000,可以通过下面两种方式查看当前环境的认值:

postgres=# show default_statistics_target ;
 default_statistics_target ---------------------------
 100
(1 row)
postgres=# select * from pg_settings where name='default_statistics_target';
-[ RECORD 1 ]------------------------------------------------------------------------------------------------------------
name       | default_statistics_target
setting    | 100
unit       | 
category   | Query Tuning / Other Planner Options
short_desc | Sets the default statistics target.
extra_desc | This applies to table columns that have not had a column-specific target set via ALTER TABLE SET STATISTICS.
context    | user
vartype    | integer
source     | default
min_val    | 1
max_val    | 10000
enumvals   | 
boot_val   | 100
reset_val  | 100
sourcefile | 
sourceline |

  可以看到,我当前环境的default_statistics_target的认值是100。

  如果不想使用认的default_statistics_target值,或者需要多某一个列特殊处理,则可以通通过ALTER TABLE tablename ALTER COLUMN colname SET STATISTISC 10来改变,这里的10比如就是你想更换的值。
  一旦列的default_statistics_target被你更改之后,Postgresql就不去参考认的default_statistics_target值了,它会先去系统表pg_attribute的对应表对应字段的attstattarget值,如果是-1,表示的是该列的取样颗粒度是采用认的值(default_statistics_target),如果是大于0的,那么就表示是使用着自己手动定义的,比如我的一个表的id通过ALTER TABLE SET STATISTICS的方法修改成20,查看attstattarget值的变化:

postgres=# create table tb13(id integer,name character varying,age integer);
CREATE TABLE postgres=# select oid from pg_class where relname='tb13';
  oid  
-------
 65697
(1 row)
postgres=# 
postgres=# select attrelid,attname,attstattarget from pg_attribute where attrelid =65697 and attname='id';
 attrelid | attname | attstattarget 
----------+---------+---------------
    65697 | id      |            -1
(1 row)
postgres=# 
postgres=# ALTER TABLE tb13 ALTER COLUMN id set STATISTICS 20;
ALTER TABLE postgres=# postgres=# select attrelid,attstattarget from pg_attribute where attrelid =65697 and attname='id';
 attrelid | attname | attstattarget 
----------+---------+---------------
    65697 | id      |            20
(1 row)

完。

原文地址:https://www.jb51.cc/postgresql/195272.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐