BRIN stands for Block Range Index. BRIN indexes are different from indexes such as B-tree indexes. BRIN indexes do not store index details based on row IDs, but store the statistics of each data block or a series of consecutive data blocks. Therefore, BRIN indexes occupy a small amount of memory and have small impacts on data writes, updates, and deletion.
Operators
<
<=
=
>=
>
Scenarios
Streaming log data is inserted into a database in chronological order. The indexes must occupy a small amount of memory and the database must have high performance.
Preperation
Create tables and insert data.
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();
Query data.
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 the index.
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);
Query the index.
\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 |
Example 1
View the execution plan after the index is created.
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
Delete the index and view the execution plan.
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
Example 2
View the execution plan after the index is created.
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
Delete the index and view the execution plan.
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