PostgreSQL不使用仅索引扫描

PostgreSQL不使用仅索引扫描,postgresql,indexing,Postgresql,Indexing,我用的是Postgres 12。我有一个sales_history表,有999900行。表中的列包括: order_id numeric NOT NULL, product_id numeric NOT NULL, customer_id numeric NOT NULL, sales_person_id numeric, quantity numeric NOT NULL, unit_price numeric, sale_amount numeric, sales_date date, to

我用的是Postgres 12。我有一个sales_history表,有999900行。表中的列包括:

order_id numeric NOT NULL,
product_id numeric NOT NULL,
customer_id numeric NOT NULL,
sales_person_id numeric,
quantity numeric NOT NULL,
unit_price numeric,
sale_amount numeric,
sales_date date,
total_amount numeric,
CONSTRAINT sales_history_pkey PRIMARY KEY (order_id)
我的样本数据:

我有一个疑问:

select sales_date
from sales_history
where (quantity * unit_price) between 5000000 and 10000000
我为(数量*单价)和列sales\u date创建了一个索引,我希望查询只使用索引扫描

create index idx_sale_diff on sales_history( (quantity * unit_price), sales_date )
但是,查询使用位图扫描:

我试着抽真空和分析,但仍然是位图扫描

有人能解释一下为什么这个查询没有使用索引扫描吗?据我所知,查询所需的所有列都在索引中可用,因此无需进行位图扫描


提前感谢:D

表的可见性映射中可能没有足够的标记为“all visible”的块,因此PostgreSQL在许多情况下必须访问表以确定行是否可见

解决办法是

VACUUM sales_history;
这将更新可见性贴图


您必须定期对表进行
真空处理
以获得仅索引扫描。对于
INSERT
-仅插入表,这在v13以下的PostgreSQL版本中不会自动发生。

表的可见性映射中可能没有足够的标记为“所有可见”的块,因此PostgreSQL在许多情况下必须访问表以确定行是否可见

解决办法是

VACUUM sales_history;
这将更新可见性贴图

您必须定期对表进行
真空处理
以获得仅索引扫描。使用
INSERT
-仅插入表,这在v13以下的PostgreSQL版本中不会自动发生