PostgreSQL简单选择不使用大表上的索引

时间:2018-12-07 13:26:37

标签: sql postgresql database-performance

我有一个表“ accounting_line”,大约有304771383行

然后运行选择以返回单行:

select integervalue from accounting_line
where accounting_line.accounting_id = 2124651 and accounting_line.type_id = 13;

我在列(accounting_id,type_id)上都有索引,但是未使用索引... 查询速度很慢,大约需要2分钟。 请帮忙。选择单行应该很快吗?

这是说明选择的分析:

Gather  (cost=1000.00..3847029.00 rows=38 width=4) (actual time=91410.660..110394.859 rows=1 loops=1)
Workers Planned: 2
Workers Launched: 2
->  Parallel Seq Scan on accounting_line  (cost=0.00..3846025.20 rows=16 width=4) (actual time=104060.824..110387.831 rows=0 loops=3)
    Filter: ((accounting_id = 2124651) AND (type_id = 13))
    Rows Removed by Filter: 101590461
Planning time: 0.124 ms
Execution time: 110394.883 ms

这是表DDL:

create table accounting_line
(
    id serial not null constraint accounting_line_pk primary key,
    accounting_id integer constraint fkio32oufjgdbf586bpr58j892d references accounting,
    type_id smallint constraint fknx7ej42yfoxdicpo8yhat8gto references accounting_type,
    doublevalue real,
    integervalue integer,
    percentage boolean default false not null
)
;

alter table accounting_line owner to orgdb
;

create index accounting_line_accounting_id_type_id_index
    on accounting_line (accounting_id, type_id)
;

1 个答案:

答案 0 :(得分:0)

在accounting_line上进行分析时,什么都没做,但在清理,分析并重新编制索引后,它又开始工作了