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)

完。

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...