按不使用索引的查询分组

Group by query not using index

这是我的查询 -

select bill, render, count (*)
from dx
group by bill, render

这是我为它创建的索引 -

CREATE INDEX bill_render ON dx (bill, render);

为什么我的查询不使用带计数 (*) 和不带计数 (*) 的索引?

"Finalize GroupAggregate  (cost=70441673.89..89334654.09 
   rows=61437331 width=27)"
    "  Group Key: bill, render"
    "  ->  Gather Merge  (cost=70441673.89..87798720.82 rows=122874662 width=27)"
    "        Workers Planned: 2"
    "        ->  Partial GroupAggregate  
    (cost=70440673.86..73614935.97 rows=61437331 width=27)"
        "              Group Key: bill, render"
    "              ->  Sort  (cost=70440673.86..71080646.06 rows=255988880 width=19)"
    "                    Sort Key: bill, render"
    "                    ->  Parallel Seq Scan on dx (cost=0.00..18940581.80 rows=255988880 width=19)"

原因是它不能执行仅索引扫描,而普通索引扫描(必须获取 table 行以检查可见性)会比顺序扫描慢。

如果您想在 PostgreSQL 中进行仅索引扫描,您必须确保大多数 table 块在 可见性映射.然后 PostgreSQL 可以跳过昂贵的堆获取。

可见性地图由 VACUUM 维护,因此 运行 table 以提高性能。