我正在使用Postgres12。我有一个具有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
我为(数量*单价)和列sales_date创建索引,并且我希望查询使用仅索引扫描
create index idx_sale_diff on sales_history( (quantity * unit_price), sales_date )
我尝试进行VACUUM和ANALYZE,但它仍然是位图扫描
有人可以解释为什么这个查询没有使用仅索引扫描吗?据我了解,查询所需的所有列都在索引中可用,因此无需进行位图扫描
先谢谢您了:D
答案 0 :(得分:0)
表的可见性图中可能没有足够的标记为“所有可见”的块,因此PostgreSQL在许多情况下必须访问该表以确定该行是否可见。
解决方案是
VACUUM sales_history;
这将更新可见性地图。
您必须定期VACUUM
一个表才能进行仅索引扫描。对于仅INSERT
表,在v13以下的PostgreSQL版本中不会自动发生这种情况。