简体   繁体   English

Postgresql:序列扫描而不是索引扫描

[英]Postgresql: Seq Scan instead of Index Scan

I have following table:我有下表:

create table if not exists inventory
(
    expired_at timestamp(0),
    -- ...
);

create index if not exists inventory_expired_at_index
    on inventory (expired_at);

However when I run following query:但是,当我运行以下查询时:

EXPLAIN UPDATE "inventory" SET "status" = 'expired' WHERE "expired_at" < '2020-12-08 12:05:00';

I get next execution plan:我得到下一个执行计划:

Update on inventory  (cost=0.00..4.09 rows=2 width=126)
  ->  Seq Scan on inventory  (cost=0.00..4.09 rows=2 width=126)
        Filter: (expired_at < '2020-12-08 12:05:00'::timestamp without time zone)

Same happens for big dataset:大数据集也是如此:

EXPLAIN SELECT * FROM "inventory"  WHERE "expired_at" < '2020-12-08 12:05:00';
-[ RECORD 1 ]---------------------------------------------------------------------------
QUERY PLAN | Seq Scan on inventory  (cost=0.00..58616.63 rows=1281058 width=71)
-[ RECORD 2 ]---------------------------------------------------------------------------
QUERY PLAN |   Filter: (expired_at < '2020-12-08 12:05:00'::timestamp without time zone)

The question is: why not Index Scan but Seq Scan ?问题是:为什么不是Index Scan而是Seq Scan

This is a bit long for a comment.这是一个有点长的评论。

The short answer is that you have two rows in the table, so it doesn't make a difference.简短的回答是表格中有两行,所以没有什么区别。

The longer answer is that your are using an update , so the data rows have to be retrieved anyway.更长的答案是您正在使用update ,因此无论如何都必须检索数据行。 Using an index requires loading both the index and the data rows and then indirecting from the index to the data rows.使用索引需要加载索引和数据行,然后从索引间接到数据行。 It is a little more complicated.这有点复杂。 And with two rows, not worth the effort at all.而且有两排,根本不值得努力。

The power of indexes is to handle large amounts of data, not small amounts of data.索引的强大之处在于处理大量数据,而不是少量数据。

To respond to the large question: Database optimizers are not required to use an index.回答这个大问题:数据库优化器不需要使用索引。 They use some sort of measures (often cost-based optimization) to determine whether or not an index is appropriate.他们使用某种措施(通常基于成本的优化)来确定索引是否合适。 In your larger example, the optimizer has determined that the index is not appropriate.在您的较大示例中,优化器已确定索引不合适。 This could happen if the statistics are out-of-synch with the underlying data.如果统计信息与基础数据不同步,则可能会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM