使用db_index
参数创建索引只会创建btree
索引:
1 2
| class JsonIdx(models.Model): content = models.JSONField(db_index=True)
|
1 2 3 4
| CREATE INDEX json_idx_jsonidx_content_d01e171b ON public.json_idx_jsonidx USING btree (content ASC NULLS LAST) TABLESPACE pg_default;
|
创建gin
索引:
1 2 3 4 5 6 7
| class JsonIdx(models.Model): content = models.JSONField()
class Meta: indexes = [ GinIndex(fields=['content']) ]
|
1 2 3 4
| CREATE INDEX json_idx_js_content_61b1a2_gin ON public.json_idx_jsonidx USING gin (content) TABLESPACE pg_default;
|
创建gin
索引,opclass=jsonb_path_ops
:
1 2 3 4 5 6 7
| class JsonIdx(models.Model): content = models.JSONField()
class Meta: indexes = [ GinIndex(name='json_idx_gin_idx', fields=['content'], opclasses=['jsonb_path_ops']) ]
|
1 2 3 4
| CREATE INDEX json_idx_gin_idx ON public.json_idx_jsonidx USING gin (content jsonb_path_ops) TABLESPACE pg_default;
|
结合文章Postgresql:JSON索引,建议采用最后一种方式创建索引。