PostgreSQL Bitmap堆扫描索引非常慢,但Index Only Scan很快

 jack_liujh_598 发布于 2022-12-14 08:26

我创建一个43kk行的表,用值1..200填充它们.因此,通过表格传播的每个数字约为220k.

create table foo (id integer primary key, val bigint);
insert into foo
  select i, random() * 200 from generate_series(1, 43000000) as i;
create index val_index on foo(val);
vacuum analyze foo;
explain analyze select id from foo where val = 55;

结果:http: //explain.depesz.com/s/fdsm

我希望总运行时间<1s,是否可能?我有SSD,核心i5(1,8),4GB RAM.9,3 Postgres.

如果我使用Index Only扫描,它的工作速度非常快:

explain analyze select val from foo where val = 55;

http://explain.depesz.com/s/7hm

但我需要选择id而不是val,所以Incex Only扫描不适合我的情况.

提前致谢!

附加信息:

SELECT relname, relpages, reltuples::numeric, pg_size_pretty(pg_table_size(oid)) 
FROM pg_class WHERE oid='foo'::regclass;

结果:

"foo";236758;43800000;"1850 MB"

配置:

"cpu_index_tuple_cost";"0.005";""
"cpu_operator_cost";"0.0025";""
"cpu_tuple_cost";"0.01";""
"effective_cache_size";"16384";"8kB"
"max_connections";"100";""
"max_stack_depth";"2048";"kB"
"random_page_cost";"4";""
"seq_page_cost";"1";""
"shared_buffers";"16384";"8kB"
"temp_buffers";"1024";"8kB"
"work_mem";"204800";"kB"

fasth.. 6

我在这里得到了答案:http: //ask.use-the-index-luke.com/questions/235/postgresql-bitmap-heap-scan-on-index-is-very-slow-but-index-only-扫描是快速

诀窍是使用id和value的复合索引:

create index val_id_index on foo(val, id);

因此,将使用仅索引扫描,但我现在可以选择ID.

select id from foo where val = 55;

结果:

http://explain.depesz.com/s/nDt3

但这仅适用于版本9.2+的Postgres.如果您被迫使用以下版本,请尝试其他选项.

1 个回答
  • 我在这里得到了答案:http: //ask.use-the-index-luke.com/questions/235/postgresql-bitmap-heap-scan-on-index-is-very-slow-but-index-only-扫描是快速

    诀窍是使用id和value的复合索引:

    create index val_id_index on foo(val, id);
    

    因此,将使用仅索引扫描,但我现在可以选择ID.

    select id from foo where val = 55;
    

    结果:

    http://explain.depesz.com/s/nDt3

    但这仅适用于版本9.2+的Postgres.如果您被迫使用以下版本,请尝试其他选项.

    2022-12-14 08:36 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有