Django-Postgres:如何在JsonB字段上创建索引 [英] Django - postgres: How to create an index on a JsonB field

查看:390
本文介绍了Django-Postgres:如何在JsonB字段上创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许在ID的JsonB字段上建立索引,该ID深入我们的Django项目中的json数据很深。 JSONB数据如下所示:

I want to allow indexing on JsonB field on an ID which is a few levels deep into the json data in our Django project. Here's what the JSONB data looks like:

  "foreign_data":{
      "some_key": val
      "src_data": {
              "VEHICLE": {
                  "title": "615",
                  "is_working": true,
                  "upc": "85121212121",
                  "dealer_name": "CryptoDealer",
                  "id": 1222551
              }
        }
 }

我想使用Django视图在 id 字段上建立索引,但不确定如何实现。

I want to index on the field id using Django views but not sure how to achieve that. Happy to post my Django ViewSet if it helps.

推荐答案

t=# create table d(i bigserial, j jsonb);
CREATE TABLE
t=# insert into d(j) select ('{"foreign_data":{
      "some_key": '||g||',
      "src_data": {
              "VEHICLE": {
                  "title": "615",
                  "is_working": true,
                  "upc": "85121212121",
                  "dealer_name": "CryptoDealer",
                  "id": '||g||'
              }
        }
 }}')::jsonb from generate_series(1,1222600) g;
INSERT 0 1222600
t=# create index ji on d (cast (j->'foreign_data'->'src_data'->'VEHICLE'->>'id' as int));
CREATE INDEX

为了使用基于fn()的索引,您必须重复查询中的函数:

in order to use such fn() based index youhave to "repeat" function in query:

t=# explain analyze select * from d 
where cast (j->'foreign_data'->'src_data'->'VEHICLE'->>'id' as int) = 1222551;
                                                          QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
 Index Scan using ji on d  (cost=0.43..8.45 rows=1 width=215) (actual time=0.021..0.021 rows=1 loops=1)
   Index Cond: ((((((j -> 'foreign_data'::text) -> 'src_data'::text) -> 'VEHICLE'::text) ->> 'id'::text))::integer = 1222551)
 Planning time: 1.585 ms
 Execution time: 0.045 ms
(4 rows)

执行比索引便宜。但是如果您跳过手续并运行:

as you see cost is tiny and execution is cheap over index. but if you "skip" formalities and run:

t=# explain analyze select * from d 
where j->'foreign_data'->'src_data'->'VEHICLE'->>'id' = '1222551';
                                                         QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
 Gather  (cost=1000.00..50122.31 rows=6113 width=215) (actual time=335.996..336.000 rows=1 loops=1)
   Workers Planned: 2
   Workers Launched: 2
   ->  Parallel Seq Scan on d  (cost=0.00..48511.01 rows=2547 width=215) (actual time=223.548..332.213 rows=0 loops=3)
         Filter: (((((j -> 'foreign_data'::text) -> 'src_data'::text) -> 'VEHICLE'::text) ->> 'id'::text) = '1222551'::text)
         Rows Removed by Filter: 407533
 Planning time: 0.096 ms
 Execution time: 343.090 ms
(8 rows)

索引将不使用

这篇关于Django-Postgres:如何在JsonB字段上创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆