当它开始使用你的索引时,你可以做一个非常 apx 的计算来了解它。
t=# drop table s07;
DROP TABLE
t=# create table s07 (i int, r int, t text);
CREATE TABLE
t=# insert into s07 select 1,1,'some text';
INSERT 0 1
t=# insert into s07 select 2,2,'some text';
INSERT 0 1
t=# insert into s07 select 3,3,'some text';
INSERT 0 1
t=# insert into s07 select 4,4,'some text';
INSERT 0 1
t=# create index s07i on s07 (i);
CREATE INDEX
t=# analyze s07;
ANALYZE
t=# SELECT relname, relkind, reltuples, relpages FROM pg_class WHERE relname LIKE 's07%';
relname | relkind | reltuples | relpages
---------+---------+-----------+----------
s07 | r | 4 | 1
s07i | i | 4 | 2
(2 rows)
尽管索引具有相同数量的行和更少的列,但在少量数据上它实际上需要两倍的空间!关系-1页,索引-2,所以planner进行seq扫描:
t=# explain analyze select i from s07 where i < 4;
QUERY PLAN
---------------------------------------------------------------------------------------------
Seq Scan on s07 (cost=0.00..1.05 rows=4 width=4) (actual time=0.003..0.004 rows=3 loops=1)
Filter: (i < 4)
Rows Removed by Filter: 1
Planning time: 0.086 ms
Execution time: 0.013 ms
(5 rows)
所以在填充了一些数据之后:
t=# insert into s07 select i,i,'some text' from generate_series(1,99,1) i;
INSERT 0 99
t=# analyze s07;
ANALYZE
t=# SELECT relname, relkind, reltuples, relpages FROM pg_class WHERE relname LIKE 's07%';
relname | relkind | reltuples | relpages
---------+---------+-----------+----------
s07 | r | 103 | 1
s07i | i | 103 | 2
(2 rows)
t=# explain analyze select i from s07 where i < 4;
QUERY PLAN
---------------------------------------------------------------------------------------------
Seq Scan on s07 (cost=0.00..2.29 rows=7 width=4) (actual time=0.008..0.016 rows=6 loops=1)
Filter: (i < 4)
Rows Removed by Filter: 97
Planning time: 0.119 ms
Execution time: 0.029 ms
(5 rows)
同一张照片。所以放更多的数据:
t=# insert into s07 select i,i,'some text' from generate_series(1,299,1) i;
INSERT 0 299
t=# analyze s07;
ANALYZE
t=# SELECT relname, relkind, reltuples, relpages FROM pg_class WHERE relname LIKE 's07%';
relname | relkind | reltuples | relpages
---------+---------+-----------+----------
s07 | r | 402 | 3
s07i | i | 402 | 2
(2 rows)
t=# explain analyze select i from s07 where i < 4;
QUERY PLAN
-------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on s07 (cost=4.22..7.33 rows=9 width=4) (actual time=0.005..0.007 rows=9 loops=1)
Recheck Cond: (i < 4)
Heap Blocks: exact=1
-> Bitmap Index Scan on s07i (cost=0.00..4.21 rows=9 width=0) (actual time=0.002..0.002 rows=9 loops=1)
Index Cond: (i < 4)
Planning time: 0.099 ms
Execution time: 0.017 ms
(7 rows)
使用条件索引,这样的计算当然会更复杂,但基本上在这里你可以看到,即使是 100 行,从一页过滤然后加载两页也更便宜。
现在你当然可以change this behavior with config,例如当我们有 100 行时,如果你运行:
t=# set cpu_tuple_cost to 1;
SET
t=# set cpu_index_tuple_cost to 0.000001;
SET
t=# explain analyze select i from s07 where i < 4;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------
Index Only Scan using s07i on s07 (cost=0.14..15.16 rows=7 width=4) (actual time=0.012..0.014 rows=6 loops=1)
Index Cond: (i < 4)
Heap Fetches: 6
Planning time: 0.058 ms
Execution time: 0.028 ms
(5 rows)
虽然页数是用于 Seq 扫描的:
t=# SELECT relname, relkind, reltuples, relpages FROM pg_class WHERE relname LIKE 's07%';
relname | relkind | reltuples | relpages
---------+---------+-----------+----------
s07 | r | 103 | 1
s07i | i | 103 | 2
(2 rows)
当然你可以暂时用set enable_seqscan=off查看执行计划和时间
更新
docs 中甚至提到 100 行对于 indes 扫描来说太小了:
从 100 行中选择 1 行几乎不会(作为索引的候选),因为 100 行
可能适合单个磁盘页面,并且没有计划可以
依次获取 1 个磁盘页面。