【问题标题】:Postgres which type of index to use for varchar search by prefixPostgres 通过前缀使用哪种类型的索引进行 varchar 搜索
【发布时间】:2021-11-08 16:35:12
【问题描述】:

对于我经常按前缀搜索的 postgres varchar 列,我应该使用什么类型的索引?

select * from customer.customers where name like 'James%'

我的桌子是这样的

James Bond
James Blond
James Sunderland
David Copperfield
David Beckham

【问题讨论】:

    标签: sql postgresql indexing


    【解决方案1】:

    将 B-tree 索引与支持模式匹配的运算符类一起使用:

    CREATE INDEX ON customer.customers (name text_pattern_ops);
    

    【讨论】:

      【解决方案2】:

      您的查询很好。您甚至可以在其上使用常规索引:

      create index idx_customer_name on customer(name);
      

      这是因为like 将在模式不以通配符开头时使用 b 树索引。对于更一般的模式,您需要 GIST 或 GIN 索引。

      【讨论】:

      • 但是除非他的排序规则是“c”,否则索引需要用text_pattern_ops声明
      猜你喜欢
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多