2

请问 Django 的 JSONField 字段有没有必要进行索引?

 2 years ago
source link: https://www.v2ex.com/t/813305
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Django

请问 Django 的 JSONField 字段有没有必要进行索引?

  Phishion · 1 天前 · 352 次点击

比如我有一个 JSONField 字段,内容如下

{
    "A": 1,
    "B": 2
}

我想筛选字段 "A" 的值为 2 ,或者 字段里面包含 B 这个 Key ,这种条件下,Django 的索引会加快查询速度么?

3 条回复    2021-11-06 22:56:19 +08:00

Vegetable   1 天前

其实文档里有介绍一些

https://docs.djangoproject.com/zh-hans/3.2/ref/models/fields/#jsonfield

索引

Index 和 Field.db_index 都创建了一个 B 树索引,在查询 JSONField 的时候并不是特别有用。仅在 PostgreSQL 上,可以使用 GinIndex 比较适合。

PostgreSQL 用户

PostgreSQL 有两种基于 JSON 的原生数据类型:json 和 jsonb 。json 和 jsonb 。它们之间的主要区别在于它们的存储方式和查询方式。PostgreSQL 的 json 字段是作为 JSON 的原始字符串表示来存储的,当根据键来查询时,必须同时进行解码。jsonb 字段是基于 JSON 的实际结构存储的,它允许索引。这样做的代价是在写入 jsonb 字段时增加了一点成本。JSONField 使用 jsonb 。

Oracle 用户

Oracle 数据库不支持存储 JSON 标量值。只支持 JSON 对象和数组(在 Python 中使用 dict 和 list 表示)。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK