微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL 中 index scan 与 seq scan 的对比学习

数据量很小的时候,我们可以看到,seq scan 比 index scan 更加有效。那是因为 index scan 至少要发生两次I/O,一次是 读取索引块, 一次是读取数据块。当index 很大的时候,情况可能会更加复杂。

postgres=# select a.relpages,a.reltuples,a.relfilenode,a.reltype,b.typname from pg_class a,pg_type b where a.relname like 'gaotab%' and a.reltype=b.oid;
relpages | reltuples | relfilenode | reltype | typname ----------+-----------+-------------+---------+---------
1   | 100   | 16396   | 16386 | gaotab

数据量为 100条记录。

预估成本:

set session enable_seqscan=false;
SET
postgres=# explain select name from gaotab where id=50;
QUERY PLAN ---------------------------------------------------------------------
Index Scan using idx_id on gaotab (cost=0.00..8.27 rows=1 width=5)
 Index Cond: (id = 50) (2 rows)
true;
QUERY PLAN ------------------------------------------------------
Seq Scan on gaotab (cost=0.00..2.25 rows=1 width= Filter: (id = 2 rows)

实际执行:

postgres=# explain analyze QUERY PLAN -------------------------------------------------------------------------------- -------------------------------
5) (actual tim e=0.112..0.113 rows=1 loops=1)
50) Total runtime: 0.133 ms (3 rows)
QUERY PLAN -------------------------------------------------------------------------------- ----------------
Seq Scan on gaotab (cost=2.25 rows=5) (actual time=0.014..0.018 rows= Filter: (id = 50) Rows Removed by Filter: 99 Total runtime: 0.034 ms (4 rows)

等到数据量大的时候,就是截然不同了。

数据为1000条记录时,通过查询可以看到,已经跨越了7个page:

postgres=# analyze;
ANALYZE
postgres=# 7   | 1000   | 16386 | gaotab (1 row)
postgres=#

再次预估成本,此时seq scan 已经开始变得不划算了:

postgres=# explain select name from gaotab where id=QUERY PLAN ---------------------------------------------------------------------
using idx_id on gaotab (cost=8.276)
 Index Cond: (id 2 rows)
=# set session enable_seqscan= session enable_indexscan=Bitmap Heap Scan on gaotab (cost4.26 Recheck Cond: (id 50)
 -> Bitmap Index Scan on idx_id (cost=0)
4 rows)
 session enable_bitmapscan=-------------------------------------------------------
Seq Scan on gaotab (cost19.50=#

实际执行

QUERY PLAN ------------------------------------------------------------------------------------------------------------
10000000000.00..10000000008.27 rows=6) (actual time=0.020. .0.022 rows=1)  Index Cond: (id = 0.051 ms (3 rows)
set session enable_indexscan=set session enable_bitmapscan=QUERY PLAN -------------------------------------------------------------------------------------------------
19.50 rows=0.015..0.095 rows=999 Total runtime: 0.109 ms (postgres=#

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐