我一直在JSONB列中寻找索引属性,但是无法在文档中找到。
答案 0 :(得分:0)
是的,这受支持,我在此处添加示例内联。但不幸的是,我们似乎尚未对此进行记录。您能对我们提出一个GitHub问题吗? https://github.com/YugaByte/yugabyte-db
在执行以下操作之前,我已经在计算机上安装了YB,并使用ysqlsh
连接到它(也可以使用psql
)。
1。创建一个带有JSONB
列的表
postgres=# CREATE TABLE orders (
ID serial NOT NULL PRIMARY KEY,
info json NOT NULL
);
CREATE TABLE
Time: 1706.060 ms (00:01.706)
2。在JSONB
属性上创建索引
postgres=# CREATE INDEX ON orders((info->'items'->>'product'));
CREATE INDEX
Time: 519.093 ms
描述表格现在应该显示索引:
postgres=# \d+ orders;
Table "public.orders"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+------------------------------------+----------+--------------+-------------
id | integer | | not null | nextval('orders_id_seq'::regclass) | plain | |
info | json | | not null | | extended | |
Indexes:
"orders_pkey" PRIMARY KEY, lsm (id HASH)
"orders_expr_idx" lsm (((info -> 'items'::text) ->> 'product'::text) HASH)
请注意以下显示索引的行:
"orders_expr_idx" lsm (((info -> 'items'::text) ->> 'product'::text) HASH)
3。插入一些数据
postgres=# INSERT INTO orders (info)
VALUES
('{ "customer": "John Doe", "items": {"product": "Beer" ,"qty": 6}}'),
('{ "customer": "Lily Bush", "items": {"product": "Diaper","qty": 24}}'),
('{ "customer": "Josh William", "items": {"product": "Toy Car","qty": 1}}'),
('{ "customer": "Mary Clark", "items": {"product": "Toy Train","qty": 2}}')
);
4。查询说明计划
postgres=# EXPLAIN SELECT * from orders WHERE info->'items'->>'product'='Beer';
QUERY PLAN
-------------------------------------------------------------------------------
Index Scan using orders_expr_idx on orders (cost=0.00..4.12 rows=1 width=36)
Index Cond: (((info -> 'items'::text) ->> 'product'::text) = 'Beer'::text)
(2 rows)
请注意,根据查询计划,此查询将使用索引来执行查找。