Postgres 在 BIGINT 列上使用索引吗?

问题描述 投票:0回答:2

我试图了解 Postgres 是否在 BIGINT 列上使用索引。创建表和相应索引的 ddl 有效。但是当通过Google搜索时,有一些文章(相当旧)说BIGINT列索引存在一些问题(它们没有被使用)。不幸的是,我没有足够的样本数据来使用 EXPLAIN 来验证这一点。

create table ab.sample
(
  id integer primary key generated always as identity,
  some_id bigint
);

create unique index sample_some_id_idx on ab.sample(some_id);
postgresql
2个回答
0
投票

我想说在正确的条件下(即足够的选择性保证索引扫描),绝对......你使用的是哪个版本?

insert into sample (some_id)
select generate_series(1,1000000);

analyze sample;

explain
select *
from sample
where some_id between 500000 and 500010;

结果:

Index Scan using sample_some_id_idx on sample  (cost=0.42..2.84 rows=11 width=12)
  Index Cond: ((some_id >= 500000) AND (some_id <= 500010))

0
投票

https://dba.stackexchange.com/questions/146294/why-does-postgresql-perform-a-seq-scan-when-comparing-a-numeric-value-with-a-big/339617#339617

确保其中之一

  • 将参数
    some_id
    绑定为预备语句中的
    bigint
  • bigint
    一样手动将任何数字文字转换为
    some_id = 1234::bigint

请注意,即使使用 ORM 管理的预准备语句,如果

some_id
从 ORM 的角度映射到
ulong
类型,它仍然会隐式导致
::numeric
绕过
bigint
列上的索引。

仅供参考:

© www.soinside.com 2019 - 2024. All rights reserved.