BRIN索引是块级索引,有别于B-TREE等索引,BRIN记录并不是以行号为单位记录索引明细,而是记录每个数据块或者每段连续的数据块的统计信息。因此BRIN索引空间占用特别的小,对数据写入、更新、删除的影响也很小。
操作符
<
<=
=
>=
>
应用场景
流式日志数据,按时间顺序不断的顺序插入。索引占用空间小,性能要求高。
准备工作
创建表及插入数据
CREATE TABLE t_brin (id int, info text, crt_time timestamp); INSERT INTO t_brin SELECT generate_series(1,1000000), md5(random()::text), clock_timestamp();
查询数据
SELECT ctid,* FROM t_brin limit 3;
ctid | id | info | crt_time (0,1) | 1 | 81c3f4f603c0c17e45778b2dd2d72f4d | 2024-11-06 09:26:57.549121 (0,2) | 2 | b4b77e95a1580480107b038776b3cc9c | 2024-11-06 09:26:57.551548 (0,3) | 3 | 6ebf5ebdd3df3428c279de2d5c7aab9f | 2024-11-06 09:26:57.551558
创建索引
CREATE INDEX idx_t_brin_1 ON t_brin USING brin (id) WITH (pages_per_range=1); CREATE INDEX idx_t_brin_2 ON t_brin USING brin (crt_time) WITH (pages_per_range=1); CREATE INDEX idx_t_brin_3 ON t_brin(id); CREATE INDEX idx_t_brin_4 ON t_brin(crt_time);
查询索引
\di+
Schema | Name | Type | Owner | Table | Size | Description wangjian | idx_t_brin_1 | index | wangjian | t_brin | 272 kB | wangjian | idx_t_brin_2 | index | wangjian | t_brin | 352 kB | wangjian | idx_t_brin_3 | index | wangjian | t_brin | 21 MB | wangjian | idx_t_brin_4 | index | wangjian | t_brin | 21 MB |
示例一
使用索引执行计划。
EXPLAIN (analyze, verbose,timing,costs, buffers) SELECT * FROM t_brin WHERE id BETWEEN 100 AND 200;
Index Scan using idx_t_brin_3 on public.t_brin (cost=0.42..5.79 rows=107 width=45) (actual time=0.006..0.020 rows=101 loops=1) Output: id, info, crt_time Index Cond: ((t_brin.id >= 100) AND (t_brin.id <= 200)) Buffers: shared hit=5 (main=5 vm=0 fsm=0) Query Identifier: -5761088690410512151 Planning: Buffers: shared hit=40 (main=38 vm=2 fsm=0) Planning Time: 0.232 ms Execution Time: 0.045 ms
删除索引后,执行计划。
DROP INDEX idx_t_brin_3;
EXPLAIN (analyze,verbose,timing,costs,buffers) SELECT * FROM t_brin WHERE id BETWEEN 100 AND 200;
Bitmap Heap Scan on public.t_brin (cost=37.83..241.69 rows=107 width=45) (actual time=1.333..1.351 rows=101 loops=1) Output: id, info, crt_time Recheck Cond: ((t_brin.id >= 100) AND (t_brin.id <= 200)) Rows Removed by Index Recheck: 93 Heap Blocks: lossy=2 Buffers: shared hit=51 (main=51 vm=0 fsm=0) -> Bitmap Index Scan on idx_t_brin_1 (cost=0.00..37.80 rows=186 width=0) (actual time=1.327..1.327 rows=20 loops=1) Index Cond: ((t_brin.id >= 100) AND (t_brin.id <= 200)) Buffers: shared hit=49 (main=49 vm=0 fsm=0) Query Identifier: -5761088690410512151 Planning: Buffers: shared hit=5 (main=5 vm=0 fsm=0) dirtied=1 (main=0 vm=0 fsm=0) Planning Time: 0.054 ms Execution Time: 1.381 ms
示例二
使用索引执行计划。
EXPLAIN (analyze, verbose, timing, costs, buffers) SELECT * FROM t_brin WHERE crt_time BETWEEN '2017-06-27 22:50:19.172224' AND '2017-06-27 22:50:19.182224';
Index Scan using idx_t_brin_4 on public.t_brin (cost=0.42..2.64 rows=1 width=45) (actual time=0.003..0.003 rows=0 loops=1) Output: id, info, crt_time Index Cond: ((t_brin.crt_time >= '2017-06-27 22:50:19.172224'::timestamp without time zone) AND (t_brin.crt_time <= '2017-06-27 22:50:19.182224'::timestamp without time zone)) Buffers: shared hit=3 (main=3 vm=0 fsm=0) Query Identifier: 2646955540723493075 Planning: Buffers: shared hit=9 (main=7 vm=2 fsm=0) Planning Time: 0.061 ms Execution Time: 0.019 ms
删除索引后,执行计划。
DROP INDEX idx_t_brin_4;
EXPLAIN (analyze, verbose, timing, costs, buffers) SELECT * FROM t_brin WHERE crt_time BETWEEN '2017-06-27 22:50:19.172224' AND '2017-06-27 22:50:19.182224';
Bitmap Heap Scan on public.t_brin (cost=49.90..152.73 rows=1 width=45) (actual time=1.449..1.449 rows=0 loops=1) Output: id, info, crt_time Recheck Cond: ((t_brin.crt_time >= '2017-06-27 22:50:19.172224'::timestamp without time zone) AND (t_brin.crt_time <= '2017-06-27 22:50:19.182224'::timestamp without time zone)) Buffers: shared hit=65 (main=65 vm=0 fsm=0) -> Bitmap Index Scan on idx_t_brin_2 (cost=0.00..49.90 rows=93 width=0) (actual time=1.447..1.447 rows=0 loops=1) Index Cond: ((t_brin.crt_time >= '2017-06-27 22:50:19.172224'::timestamp without time zone) AND (t_brin.crt_time <= '2017-06-27 22:50:19.182224'::timestamp without time zone)) Buffers: shared hit=65 (main=65 vm=0 fsm=0) Query Identifier: 2646955540723493075 Planning: Buffers: shared hit=5 (main=5 vm=0 fsm=0) dirtied=1 (main=0 vm=0 fsm=0) Planning Time: 0.058 ms Execution Time: 1.477 ms
文档内容是否对您有帮助?