【发布时间】:2019-06-15 12:44:28
【问题描述】:
我在 JSONB 列中查找索引属性,但在文档中找不到。
【问题讨论】:
标签: database distributed-database yugabyte-db
我在 JSONB 列中查找索引属性,但在文档中找不到。
【问题讨论】:
标签: database distributed-database yugabyte-db
是的,这是支持的,我在这里添加了一个内联示例。但不幸的是,我们似乎还没有记录这一点。你能打开一个针对我们的 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)
请注意,根据查询计划,此查询将使用索引来执行查找。
【讨论】: