为什么不使用我的杜松子酒索引Postgres?

时间:2016-02-09 18:37:08

标签: postgresql database-indexes

使用此表:

<form>
  <input type="text" name="search_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='introdu codul'" id="search_code" placeholder="introdu codul" autocomplete="off" value="" /><br/>
  <input type="" name="insert_code" onfocus="clearField(this, this.placeholder='');" onblur="this.placeholder='scaneaza codul'" id="insert_code" placeholder="scaneaza codul" autocomplete="off" value="" /><br/><br/>
  <input type="submit" id="button" name="button" value="verifica COD" />
</form>

<div id="result"></div>

此查询未使用杜松子酒索引:

=> \d "user"
                                          Table "public.user"
        Column        |            Type             |                     Modifiers                     
----------------------+-----------------------------+---------------------------------------------------
 id                   | integer                     | not null default nextval('user_id_seq'::regclass)
 email                | character varying(255)      | 
Indexes:
    "user_pkey" PRIMARY KEY, btree (id)
    "user_email_key" UNIQUE CONSTRAINT, btree (email)
    "user_email_idx" gin (email gin_trgm_ops)

为什么?

2 个答案:

答案 0 :(得分:0)

https://hashrocket.com/blog/posts/exploring-postgres-gin-index

注意事项 这种方法的唯一缺点是输入查询必须至少为3个字母,因为Postgres需要能够从输入查询中提取至少一个trigram才能使用我们的trigram索引。

答案 1 :(得分:0)

因为你实际上并没有使用trigram索引或你的postgres版本&lt; 9.1。

select email from "user" where similarity(email, 'xyz@gmail.com') > 0.5;

其中0.5是您的阈值,0完全不同,1精确匹配

UPD:考虑您将1个字符作为匹配字词,因为一个符号可以匹配大量文档,但可能会显示性能不佳

相关问题