2.4、jsonb索引使用
TBase为文档jsonb提供了GIN索引,GIN 索引可以被用来有效地搜索在大量jsonb文档(数据)中出现 的键或者键值对。
2.4.1、创建立jsonb索引
postgres=# create index t_jsonb_f_jsonb_idx on t_jsonb using gin(f_jsonb);
CREATE INDEX
postgres=# \d+ t_jsonb
Table "public.t_jsonb"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
---------+---------+-----------+----------+---------+----------+--------------+-------------
id | integer | | | | plain | |
f_jsonb | jsonb | | | | extended | |
Indexes:
"t_jsonb_f_jsonb_idx" gin (f_jsonb)
Distribute By: SHARD(id)
Location Nodes: ALL DATANODES
2.4.2、测试查询的性能
postgres=# select count(1) from t_jsonb;
count
----------
10000000
(1 row)
postgres=# analyze t_jsonb;
ANALYZE
—没有索引开销
postgres=# select * from t_jsonb where f_jsonb @> '{"col1":9999}';
id | f_jsonb
------+--------------------------------
9999 | {"col1": 9999, "col2": "9999"}
9999 | {"col1": 9999, "col2": "9999"}
9999 | {"col1": 9999, "col2": "9999"}
9999 | {"col1": 9999, "col2": "9999"}
9999 | {"col1": 9999, "col2": "9999"}
(5 rows)
Time: 2473.488 ms (00:02.473)
—有索引开销
postgres=# select * from t_jsonb where f_jsonb @> '{"col1":9999}';
id | f_jsonb
------+--------------------------------
9999 | {"col1": 9999, "col2": "9999"}
9999 | {"col1": 9999, "col2": "9999"}
9999 | {"col1": 9999, "col2": "9999"}
9999 | {"col1": 9999, "col2": "9999"}
9999 | {"col1": 9999, "col2": "9999"}
(5 rows)
Time: 217.968 ms