Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-postgres:如何在JsonB字段上创建索引_Python_Django_Postgresql_Jsonb - Fatal编程技术网

Python Django-postgres:如何在JsonB字段上创建索引

Python Django-postgres:如何在JsonB字段上创建索引,python,django,postgresql,jsonb,Python,Django,Postgresql,Jsonb,我想允许在Django项目中的json数据中深入几层的ID上对JsonB字段进行索引。以下是JSONB数据的外观: "foreign_data":{ "some_key": val "src_data": { "VEHICLE": { "title": "615", "is_working": true, "upc": "851212

我想允许在Django项目中的json数据中深入几层的ID上对JsonB字段进行索引。以下是JSONB数据的外观:

  "foreign_data":{
      "some_key": val
      "src_data": {
              "VEHICLE": {
                  "title": "615",
                  "is_working": true,
                  "upc": "85121212121",
                  "dealer_name": "CryptoDealer",
                  "id": 1222551
              }
        }
 }
我想使用Django视图对字段
id
进行索引,但不确定如何实现。如果有帮助的话,很高兴发布我的Django视图集

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()的索引,您必须在查询中使用“repeat”函数:

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)
正如您所看到的,成本很小,执行比索引便宜。但如果你“跳过”手续,然后跑:

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)
索引将不被使用