gpt4 book ai didi

postgresql - 为什么 vacuum analyze 会更改查询计划而 analyze 不会?

转载 作者:行者123 更新时间:2023-11-29 11:19:57 31 4
gpt4 key购买 nike

我想利用 Postgres 中仅索引扫描的强大功能,并用一张表进行了试验:

CREATE TABLE dest.contexts
(
id integer NOT NULL,
phrase_id integer NOT NULL,
lang character varying(5) NOT NULL,
ranking_value double precision,
index_min integer,
index_max integer,
case_sensitive boolean,
is_enabled boolean,
is_to_sync boolean NOT NULL DEFAULT true
);

insert into dest.contexts select * from source.contexts;

alter table dest.contexts
add constraint pk_contexts primary key (id, phrase_id, lang);

CREATE INDEX idx_contexts_
ON dest.contexts
USING btree
(id, is_enabled, lang, phrase_id, ranking_value, index_min, index_max, case_sensitive);

索引涵盖了我想在下一个查询中使用的所有列:

explain analyze
select ranking_value, index_min, index_max, case_sensitive
from dest.contexts
where id = 456 and is_enabled

我在创建后立即检查计划:

Bitmap Heap Scan on contexts  (cost=4.41..31.46 rows=12 width=17) (actual time=0.045..0.045 rows=0 loops=1)
Recheck Cond: (id = 456)
Filter: is_enabled
-> Bitmap Index Scan on idx_contexts_ (cost=0.00..4.40 rows=12 width=0) (actual time=0.038..0.038 rows=0 loops=1)
Index Cond: ((id = 456) AND (is_enabled = true))
Planning time: 0.631 ms
Execution time: 0.093 ms

这很奇怪,但没关系......

几秒钟后它变成另一个(autovacuum?)

Index Scan using pk_contexts on contexts  (cost=0.28..17.93 rows=6 width=17) (actual time=0.027..0.027 rows=0 loops=1)
Index Cond: (id = 456)
Filter: is_enabled
Planning time: 0.185 ms
Execution time: 0.070 ms

我试图强制它使用仅索引扫描:

analyze dest.contexts

但这并没有改变任何东西。那我做

vacuum verbose analyze dest.contexts;

INFO: vacuuming "dest.contexts"
INFO: index "pk_contexts" now contains 4845 row versions in 21 pages
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: index "idx_contexts_" now contains 4845 row versions in 37 pages
DETAIL: 0 index row versions were removed.
0 index pages have been deleted, 0 are currently reusable.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: "contexts": found 0 removable, 4845 nonremovable row versions in 41 out of 41 pages
DETAIL: 0 dead row versions cannot be removed yet.
There were 0 unused item pointers.
0 pages are entirely empty.
CPU 0.00s/0.00u sec elapsed 0.00 sec.
INFO: analyzing "dest.contexts"
INFO: "contexts": scanned 41 of 41 pages, containing 4845 live rows and 0 dead rows; 4845 rows in sample, 4845 estimated total rows

在这里我终于得到了我想要的:

Index Only Scan using idx_contexts_ on contexts  (cost=0.28..4.40 rows=6 width=17) (actual time=0.014..0.014 rows=0 loops=1)
Index Cond: ((id = 456) AND (is_enabled = true))
Filter: is_enabled
Heap Fetches: 0
Planning time: 0.247 ms
Execution time: 0.052 ms

下面是问题:

  1. 为什么analyze 不教它使用大型“全覆盖”索引?
  2. vacuum analyze 为什么要这样做?
  3. 我的 table 从头开始就塞满了一个大插页。为什么 vacuum 什么都不做?在我看来,那里没有什么可以吸尘的。

最佳答案

Analyze负责收集统计信息,analyze影响查询计划的选择和计划成本。

索引不包含有关元组的可见性信息。因此,读取数据页和过滤结果集也是应用事件,尽管 select 中的所有列都在索引中。如果一个页面中的所有元组都可见,Postgres 不需要额外的过滤操作。 Postgres 使用可见性映射 (vm) 来实现这一点。

Vacuum 在回收死元组时使用并更新可见性映射。因此,如果可能,vacuum 会更改查询计划以使用仅索引扫描。

https://www.postgresql.org/docs/9.6/static/sql-vacuum.html

https://www.postgresql.org/docs/9.6/static/storage-vm.html

关于postgresql - 为什么 vacuum analyze 会更改查询计划而 analyze 不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46004204/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com