PostgreSQL模糊匹配走索引

前端之家收集整理的这篇文章主要介绍了PostgreSQL模糊匹配走索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

数据库版本:postgresql 9.6.0
作者:高铭杰
邮箱:jackgo73@outlook.com
日期:2017年5月28日



场景 lower(name) like 'pf%'

  1. create table users (id int primary key,name varchar(255));
  2.  
  3. Create or replace function random_string(length integer) returns text as $$ declare chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
  4. result text := '';
  5. i integer := 0;
  6. begin if length < 0 then raise exception 'Given length cannot be less than 0';
  7. end if;
  8. for i in 1..length loop
  9. result := result || chars[1+random()*(array_length(chars,1)-1)];
  10. end loop;
  11. return result;
  12. end;
  13. $$ language plpgsql;
  14.  
  15. insert into users values(generate_series(1,50000),random_string(15));

普通bt:不走索引

pg_trgm模块提供函数和操作符测定字母数字文本基于三元模型匹配的相似性,还有支持快速搜索相似字符串的索引操作符类。三元模型是一组从一个字符串中获得的三个连续的字符。我们可以通过计数两个字符串共享的三元模型的数量来测量它们的相似性。这个简单的想法证明在测量许多自然语言词汇的相似性时是非常有效的。

  1. CREATE INDEX users_idx0 ON users (name);

全字匹配查询(走索引)

  1. explain select * from users where name='pfDNQVmhqDrF1EY';
  2. QUERY PLAN -------------------------------------------------------------------------
  3. Index Scan using users_idx0 on users (cost=0.29..8.31 rows=1 width=20)
  4. Index Cond: ((name)::text = 'pfDNQVmhqDrF1EY'::text)
  5. (2 rows)

函数全字匹配(不走索引)

  1. explain select * from users where lower(name)='pfDNQVmhqDrF1EY';
  2. QUERY PLAN -----------------------------------------------------------
  3. Seq Scan on users (cost=0.00..1069.00 rows=250 width=20)
  4. Filter: (lower((name)::text) = 'pfDNQVmhqDrF1EY'::text)
  5. (2 rows)

模糊匹配(不走索引)

  1. explain select * from users where name like 'pf%';
  2. QUERY PLAN --------------------------------------------------------
  3. Seq Scan on users (cost=0.00..944.00 rows=5 width=20)
  4. Filter: ((name)::text ~~ 'pf%'::text)
  1. explain select * from users where name like 'pf_';
  2. QUERY PLAN --------------------------------------------------------
  3. Seq Scan on users (cost=0.00..944.00 rows=5 width=20)
  4. Filter: ((name)::text ~~ 'pf_'::text)

字段带函数的bt索引:函数走索引

  1. drop index users_idx0;
  2. CREATE INDEX users_dex1 ON users (lower(name));

函数全字匹配(走索引)

  1. explain select * from users where lower(name)='pfDNQVmhqDrF1EY';
  2. QUERY PLAN
  3. ---------------------------------------------------------------------------
  4. Bitmap Heap Scan on users (cost=6.23..324.34 rows=250 width=20)
  5. Recheck Cond: (lower((name)::text) = 'pfDNQVmhqDrF1EY'::text)
  6. -> Bitmap Index Scan on users_dex1 (cost=0.00..6.17 rows=250 width=0)
  7. Index Cond: (lower((name)::text) = 'pfDNQVmhqDrF1EY'::text)
  8. (4 rows)

模糊匹配(不走索引)

  1. explain select * from users where lower(name) like 'pf%';
  2. QUERY PLAN -----------------------------------------------------------
  3. Seq Scan on users (cost=0.00..1069.00 rows=250 width=20)
  4. Filter: (lower((name)::text) ~~ 'pf%'::text)
  5. (2 rows)

声明操作符类的bt索引:like走索引

定义索引的同时可以为索引的每个字段声明一个操作符类。
CREATE INDEX name ON table (column opclass [sort options] [,…]);
这个操作符类指明该索引用于该字段时要使用的操作符。

  1. CREATE INDEX users_dex2 ON users (lower(name) varchar_pattern_ops);

模糊匹配(走索引)

  1. explain select * from users where lower(name) like 'pf%';
  2. QUERY PLAN
  3. ------------------------------------------------------------------------------------------------------
  4. Bitmap Heap Scan on users (cost=4.82..144.00 rows=5 width=20)
  5. Filter: (lower((name)::text) ~~ 'pf%'::text)
  6. -> Bitmap Index Scan on users_dex2 (cost=0.00..4.82 rows=53 width=0)
  7. Index Cond: ((lower((name)::text) ~>=~ 'pf'::text) AND (lower((name)::text) ~<~ 'pg'::text))
  8. (4 rows)

场景2 name like '%pf%'

  1. Create or replace function random_string(length integer) returns text as $$ declare chars text[] := '{0,1)-1)];
  2. end loop;
  3. return result;
  4. end;
  5. $$ language plpgsql;
  6. create table users (id int primary key,name varchar(255));
  7. insert into users values(generate_series(1,random_string(15));

声明操作符bt:不走索引

  1. CREATE INDEX idx_name ON users USING btree (lower(name) varchar_pattern_ops);
  1. explain (analyze true,format yaml,verbose true,buffers true) select * from users where lower(name) like '%pf%';\
  2. QUERY PLAN
  3. -----------------------------------------------------------
  4. - Plan: +
  5. Node Type: "Seq Scan" +
  6. Parallel Aware: false +
  7. Relation Name: "users" +
  8. Schema: "public" +
  9. Alias: "users" +
  10. Startup Cost: 0.00 +
  11. Total Cost: 1069.00 +
  12. Plan Rows: 5 +
  13. Plan Width: 20 +
  14. Actual Startup Time: 0.320 +
  15. Actual Total Time: 86.841 +
  16. Actual Rows: 710 +
  17. Actual Loops: 1 +
  18. Output: +
  19. - "id" +
  20. - "name" +
  21. Filter: "(lower((users.name)::text) ~~ '%pf%'::text)"+
  22. Rows Removed by Filter: 49290 +
  23. Shared Hit Blocks: 319 +
  24. Shared Read Blocks: 0 +
  25. Shared Dirtied Blocks: 0 +
  26. Shared Written Blocks: 0 +
  27. Local Hit Blocks: 0 +
  28. Local Read Blocks: 0 +
  29. Local Dirtied Blocks: 0 +
  30. Local Written Blocks: 0 +
  31. Temp Read Blocks: 0 +
  32. Temp Written Blocks: 0 +
  33. Planning Time: 0.188 +
  34. Triggers: +
  35. Execution Time: 86.975

声明pg_trgm操作符bt:可以走索引

  1. CREATE EXTENSION pg_trgm;
  2.  
  3. CREATE INDEX idx_users_name_trgm_gist ON users USING gist (name gist_trgm_ops);
  1. explain (analyze true,verbose true,buffers true) select * from users where name like '%pf%';
  2. QUERY PLAN
  3. ------------------------------------------------------------------------------------------------------------------------------------------
  4. Bitmap Heap Scan on public.users (cost=32.19..371.08 rows=505 width=20) (actual time=19.314..53.132 rows=193 loops=1)
  5. Output: id,name
  6. Recheck Cond: ((users.name)::text ~~ '%pf%'::text)
  7. Rows Removed by Index Recheck: 49807
  8. Heap Blocks: exact=319
  9. Buffers: shared hit=972
  10. -> Bitmap Index Scan on idx_users_name_trgm_gist (cost=0.00..32.06 rows=505 width=0) (actual time=19.175..19.175 rows=50000 loops=1)
  11. Index Cond: ((users.name)::text ~~ '%pf%'::text)
  12. Buffers: shared hit=653
  13. Planning time: 0.188 ms
  14. Execution time: 53.231 ms
  15. (11 rows)

猜你在找的Postgre SQL相关文章