PostgreSQL n_distinct 统计设置

PostgreSQL n_distinct statistics setting

在 PostgreSQL 中有多种设置 n_distinct 的方法吗?这两个似乎都在做同样的事情,但最终在 pg_attribute 内改变了不同的值。这两个命令有什么区别?

alter table my_table alter column my_column set (n_distinct = 500);
alter table my_table alter column my_column set statistics 1000;
select
    c.relname,
    a.attname,
    a.attoptions,
    a.attstattarget
from
    pg_class c
    inner join
    pg_attribute a
        on c.oid = a.attrelid
where
    c.relname = 'my_table'
and
    a.attname = 'my_column'
order by
    c.relname,
    a.attname;
Name         |Value           
-------------|----------------
relname      |my_table       
attname      |my_column         
attoptions   |{n_distinct=500}
attstattarget|1000            

Both of these seem to be doing the same thing

为什么这么说?这两个命令显然是不同的。两者都与列统计和查询计划有关。但他们做的事情却截然不同。

统计目标...

controls the level of detail of statistics accumulated for this column by ANALYZE. See:

  • Check statistics targets in PostgreSQL

Basics in the manual.

设置n_distinct是完全不同的东西。这意味着对给定列期望的不同值的数量(或比率)进行硬编码。 (但只有在下一个ANALYZE之后才有效。)

dba.SE 上的相关答案以及 n_distinct 上的更多答案: