【问题标题】:How to index a jsonb nexted column that uses Getpath operator #>> so we can a specfic value如何索引使用 Getpath 运算符 #>> 的 jsonb nexted 列,以便我们可以获取特定值
【发布时间】:2022-07-02 09:10:54
【问题描述】:

我们有一个 jsonb 列 'roller' 作为

{
  "roller": {
    "dob": "8/16/1956",
    "ext": {
      "helio_status": ""
    },
    "grade": "11",
    "gender": "M",
    "sis_id": "3456704387",
    "location": {
      "zip": "99999"
    },
    "state_id": "546556560",
    "roller_number": "ASWE51460438"
  }
}

我们传递一个我们想在{roller, sis_id}中找到的变量来检查它是否存在

 u.roller#>>'{roller, sis_id}' = 'SE223dsd'

我们在滚筒上创建了一个 gin 索引,但它不起作用,它总是对所有它们进行 seq 扫描。

是否有特定类型的索引会更好?

【问题讨论】:

    标签: postgresql indexing jsonb postgresql-12


    【解决方案1】:

    如果您总是寻找该特定路径,则常规 B-Tree 索引可能是最佳选择:

    create index on the_table ( (roller#>>'{roller, sis_id}') );
    

    您必须确保始终在 WHERE 子句中使用表达式 roller#>>'{roller, sis_id}'。不是等价的东西,例如roller -> 'roller' ->> 'sis_id'

    【讨论】:

    • 如果我们需要对 state id 或 zip 进行搜索,我们是否需要为每个创建单独的索引
    猜你喜欢
    • 2022-01-18
    • 2015-09-04
    • 1970-01-01
    • 2020-08-31
    • 2014-07-03
    • 2020-01-02
    • 2021-10-30
    • 1970-01-01
    相关资源
    最近更新 更多