Database YugaByte’;JSONB列中的SQL支持索引属性?
我在JSONB列中查找索引属性,但在文档中找不到。是的,这是受支持的,我在这里添加了一个内联示例。但不幸的是,我们似乎还没有记录这一点。你能开一个针对我们的GitHub问题吗 在执行以下操作之前,我已经在我的机器上安装了YB,并使用Database YugaByte’;JSONB列中的SQL支持索引属性?,database,distributed-database,yugabyte-db,Database,Distributed Database,Yugabyte Db,我在JSONB列中查找索引属性,但在文档中找不到。是的,这是受支持的,我在这里添加了一个内联示例。但不幸的是,我们似乎还没有记录这一点。你能开一个针对我们的GitHub问题吗 在执行以下操作之前,我已经在我的机器上安装了YB,并使用ysqlsh连接到它(您也可以使用psql) 1。创建一个带有JSONB列的表 postgres=# CREATE TABLE orders ( ID serial NOT NULL PRIMARY KEY,
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)
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)
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\u expr\u idx”lsm(((信息->项目::文本)->“产品::文本)散列)
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=# CREATE TABLE orders (
ID serial NOT NULL PRIMARY KEY,
info json NOT NULL
);
CREATE TABLE
Time: 1706.060 ms (00:01.706)
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)
请注意,根据查询计划,此查询将使用索引执行查找