B-Tree Indexをそのまま張るのはNG
create indexpost_json_obj
on post_json using btree (obj);
create index comment_json_obj
on comment_json using btree (obj);
-- obj全体を = で比較する場合しかindexが使われない
15.
式Index
create index post_json_id
onpost_json using btree ((obj->>'id'));
create index comment_json_post_id
on comment_json using btree ((obj->>'post_id'));
16.
Key名が決まらないならGIN※を
使おう
create index post_json_gin
onpost_json using gin (obj jsonb_path_ops);
create index post_json_tags_gin
on post_json using gin (obj->'tags');
-- 配列に対するindexも貼れる
(※ GIN = Generalized Inverted Index)
SchemafulなテーブルへのINSERT
create table post(
id integer
, tags jsonb -- flatに出来ないものはjsonbに突っ込んじゃう
, title text
, created_at timestamptz
);
insert into post
select (jsonb_populate_record(null::post, obj)).*
from post_json
;
select *
from post;
こんなクエリを実行すると
with recursive evalas (
select obj->>'car' as car
, obj->'cdr' as cdr
, 1 as depth
from lisp
union all
select cdr->>'car' as car
, cdr->'cdr' as cdr
, depth + 1 as depth
from eval
where (cdr->'cdr') is not null
)
select string_agg(car, '' order by depth) as msg
from eval
;
(https://gist.github.com/yuki-takeichi/c4812f7af46f7f84e958)