createindex test_hash_key on test_hash usinghash (key);
查询示例
1 2 3
selectkey, value from test_hash wherekey = '70913971e3676ba202faba804a12f9bd';
查询计划
1 2 3 4
Index Scan using test_hash_key on test_hash (cost=0.00..8.02 rows=1 width=37) (actual time=0.010..0.011 rows=1 loops=1) Index Cond: (key = '70913971e3676ba202faba804a12f9bd'::text) Planning Time: 0.037 ms Execution Time: 0.021 ms
create table test_gist ( range int4range, value int, exclude using gist (range with &&) ); insert into test_gist with a as ( select (random() * 1000)::int v from generate_series(1, 100) ) select int4range(v, v + 11), (random() * 100)::int from a on conflict do nothing;
查询示例
1 2 3
selectrange, value from test_gist whererange @> 400;
查询计划
1 2 3 4 5 6 7
Bitmap Heap Scan on test_gist (cost=4.19..13.66 rows=6 width=36) (actual time=0.049..0.049 rows=1 loops=1) Recheck Cond: (range @> 400) Heap Blocks: exact=1 -> Bitmap Index Scan on test_gist_range_excl (cost=0.00..4.19 rows=6 width=0) (actual time=0.009..0.009 rows=1 loops=1) Index Cond: (range @> 400) Planning Time: 0.142 ms Execution Time: 0.068 ms
createindex test_brin_key on test_brin (key) include (value);
查询示例
1 2 3
selectkey, value from test_brin wherekeybetween11100and11150;
查询计划
1 2 3 4 5
Index Only Scan using test_brin_key on test_brin (cost=0.42..5.42 rows=50 width=8) (actual time=0.014..0.018 rows=51 loops=1) Index Cond: ((key >= 11100) AND (key <= 11150)) Heap Fetches: 0 Planning Time: 0.046 ms Execution Time: 0.029 ms
7. bloom
介绍
bloom索引虽然只能用于等值查询,但可通过多列字段索引高效的查询每一种可能的情况.
用法
创建表
1 2 3 4 5
createtable test_bloom as selectmd5(random()::text) as i1, md5(random()::text) as i2, md5(random()::text) as i3 from generate_series(1, 1000000);
创建索引
1
createindex test_bloom_all on test_bloom using bloom (i1, i2, i3);
查询示例
1 2 3
select i1,i2,i3 from test_bloom where i2 = '2efeb41703124b0c5a3ac7cfbc86029f';
查询计划
1 2 3 4 5 6 7 8
Bitmap Heap Scan on test_bloom (cost=15348.00..15352.01 rows=1 width=99) (actual time=5.933..8.660 rows=1 loops=1) Recheck Cond: (i2 = '2efeb41703124b0c5a3ac7cfbc86029f'::text) Rows Removed by Index Recheck: 4380 Heap Blocks: exact=3890 -> Bitmap Index Scan on test_bloom_all (cost=0.00..15348.00 rows=1 width=0) (actual time=5.615..5.616 rows=4381 loops=1) Index Cond: (i2 = '2efeb41703124b0c5a3ac7cfbc86029f'::text) Planning Time: 0.039 ms Execution Time: 8.678 ms