【发布时间】:2020-09-06 06:19:36
【问题描述】:
我正在使用 Postgres 12。我有一个包含 999900 行的 sales_history 表。表中的列包括:
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
我为 (quantity * unit_price) 和列 sales_date 创建了一个索引,我希望查询使用仅索引扫描
create index idx_sale_diff on sales_history( (quantity * unit_price), sales_date )
我尝试了 VACUUM 和 ANALYZE 但它仍然是位图扫描
有人能解释一下为什么这个查询没有使用 index-only scan 吗?据我了解,查询所需的所有列都在索引中可用,因此无需进行位图扫描
提前致谢:D
【问题讨论】:
标签: postgresql indexing