JSONBを使用したPostgreSQLの参加


16

私はこのSQLを持っています:

CREATE TABLE test(id SERIAL PRIMARY KEY, data JSONB);

INSERT INTO test(data) VALUES
   ('{"parent":null,"children":[2,3]}'),
   ('{"parent":1,   "children":[4,5]}'),
   ('{"parent":1,   "children":[]}'),
   ('{"parent":2,   "children":[]}'),
   ('{"parent":2,   "children":[]}');

それは与えるだろう:

 id |                 data                 
----+--------------------------------------
  1 | {"parent": null, "children": [2, 3]}
  2 | {"parent": 1, "children": [4, 5]}
  3 | {"parent": 1, "children": []}
  4 | {"parent": 2, "children": []}
  5 | {"parent": 2, "children": []}

通常の1対多を行うと、次のように表示されます。

SELECT * 
FROM test x1
  LEFT JOIN test x2
    ON x1.id = (x2.data->>'parent')::INT;
 id |                 data                 | id |               data                
----+--------------------------------------+----+-----------------------------------
  1 | {"parent": null, "children": [2, 3]} |  2 | {"parent": 1, "children": [4, 5]}
  1 | {"parent": null, "children": [2, 3]} |  3 | {"parent": 1, "children": []}
  2 | {"parent": 1, "children": [4, 5]}    |  4 | {"parent": 2, "children": []}
  2 | {"parent": 1, "children": [4, 5]}    |  5 | {"parent": 2, "children": []}
  5 | {"parent": 2, "children": []}        |    | 
  4 | {"parent": 2, "children": []}        |    | 
  3 | {"parent": 1, "children": []}        |    | 

子供に基づいて参加する方法(LEFT JOINまたはを使用WHERE IN)?私はもう試した:

SELECT data->>'children' FROM test;
 ?column? 
----------
 [2, 3]
 [4, 5]
 []
 []
 []

SELECT json_array_elements((data->>'children')::TEXT) FROM t...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

SELECT json_array_elements((data->>'children')::JSONB) FROM ...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

SELECT json_to_record((data->>'children')::JSON) FROM test;
ERROR:  function returning record called in context that cannot accept type record
HINT:  Try calling the function in the FROM clause using a column definition list.

SELECT * FROM json_to_record((test.data->>'children')::JSON);
ERROR:  missing FROM-clause entry for table "test"
LINE 1: SELECT * FROM json_to_record((test.data->>'children')::JSON)...

回答:


23

これはより効率的です:

jsonし、json_array_elements()PG 9.3で

SELECT p.id AS p_id, p.data AS p_data
     , c.id AS c_id, c.data AS c_data
FROM   test p
LEFT   JOIN LATERAL json_array_elements(p.data->'children') pc(child) ON TRUE
LEFT   JOIN test c ON c.id = pc.child::text::int;
  • への参照では->なく演算子を使用->>childrenます。あなたがそれを持っている方法では、最初にjson/ jsonb にキャストしtext、次にに戻りjsonます。

  • セットを返す関数を呼び出すクリーンな方法はLEFT [OUTER] JOIN LATERALです。これに、子のない行が含まれます。それらを除外するには、[INNER] JOIN LATERALorまたはCROSS JOIN-に変更するか、カンマを使用した簡略構文に変更します。

    , json_array_elements(p.data->'children') pc(child)
  • 結果で重複する列名を避ける。

SQLフィドル。

jsonbし、jsonb_array_elements()PG 9.4で

EXPLAIN 
SELECT p.id AS p_id, p.data AS p_data
     , c.id AS c_id, c.data AS c_data
FROM   test p
LEFT   JOIN LATERAL jsonb_array_elements(p.data->'children') pc(child) ON TRUE
LEFT   JOIN test c ON c.id = pc.child::text::int;
-------------------------------------------------------------------------------------------
 Hash Left Join  (cost=37.69..4826.24 rows=123000 width=72)
   Hash Cond: (((pc.child)::text)::integer = c.id)
   ->  Nested Loop Left Join  (cost=0.01..2482.31 rows=123000 width=68)
         ->  Seq Scan on test p  (cost=0.00..22.30 rows=1230 width=36)
         ->  Function Scan on jsonb_array_elements pc  (cost=0.01..1.01 rows=100 width=32)
   ->  Hash  (cost=22.30..22.30 rows=1230 width=36)
         ->  Seq Scan on test c  (cost=0.00..22.30 rows=1230 width=36)

脇:Aは次のようになり、基本的なデータ型のDB設計を正規化方法このため、より効率的。


9.4rc1にそれは与える:LINE 4: LEFT JOIN LATERAL json_array_elements(p.data->'children') ... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Kokizzu

^始まりであるjson_array_elements
Kokizzu

1
ああ、私の悪い、jsonb_代わりにjson_関数を使用する必要があります
Kokizzu 14年

3

気にしないで、道を見つけた

SELECT *
 FROM ( SELECT *, json_array_elements((data->>'children')::JSON) child FROM test) x1
   LEFT JOIN test x2
    ON x1.child::TEXT::INT = x2.id
;

 id |                 data                 | child | id |               data
----+--------------------------------------+-------+----+-----------------------------------
  1 | {"parent": null, "children": [2, 3]} | 2     |  2 | {"parent": 1, "children": [4, 5]}
  1 | {"parent": null, "children": [2, 3]} | 3     |  3 | {"parent": 1, "children": []}
  2 | {"parent": 1, "children": [4, 5]}    | 4     |  4 | {"parent": 2, "children": []}
  2 | {"parent": 1, "children": [4, 5]}    | 5     |  5 | {"parent": 2, "children": []}

                                                QUERY PLAN                                                 
-----------------------------------------------------------------------------------------------------------
 Hash Left Join  (cost=37.67..4217.38 rows=123000 width=104)
   Hash Cond: ((((json_array_elements(((test.data ->> 'children'::text))::json)))::text)::integer = x2.id)
   ->  Seq Scan on test  (cost=0.00..643.45 rows=123000 width=36)
   ->  Hash  (cost=22.30..22.30 rows=1230 width=36)
         ->  Seq Scan on test x2  (cost=0.00..22.30 rows=1230 width=36)

または

SELECT *
 FROM test x1
    LEFT JOIN ( SELECT *, json_array_elements((data->>'children')::JSON) child FROM test) x2
    ON x1.id = x2.child::TEXT::INT
;

 id |                 data                 | id |                 data                 | child 
----+--------------------------------------+----+--------------------------------------+-------
  2 | {"parent": 1, "children": [4, 5]}    |  1 | {"parent": null, "children": [2, 3]} | 2
  3 | {"parent": 1, "children": []}        |  1 | {"parent": null, "children": [2, 3]} | 3
  4 | {"parent": 2, "children": []}        |  2 | {"parent": 1, "children": [4, 5]}    | 4
  5 | {"parent": 2, "children": []}        |  2 | {"parent": 1, "children": [4, 5]}    | 5
  1 | {"parent": null, "children": [2, 3]} |    |                                      | 

                                                QUERY PLAN                                                 
-----------------------------------------------------------------------------------------------------------
 Hash Right Join  (cost=37.67..4217.38 rows=123000 width=104)
   Hash Cond: ((((json_array_elements(((test.data ->> 'children'::text))::json)))::text)::integer = x1.id)
   ->  Seq Scan on test  (cost=0.00..643.45 rows=123000 width=36)
   ->  Hash  (cost=22.30..22.30 rows=1230 width=36)
         ->  Seq Scan on test x1  (cost=0.00..22.30 rows=1230 width=36)
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.