软件下载吧文章资讯

分类分类

postgresql 数据库基础 关于like%xxx% 全模糊匹配 的优化操作

2024-02-07 12:09作者:下载吧

任何一个关系型数据库关于模糊匹配(like)的优化都是一件痛苦的事,相对而言,诸如like ‘abc%’之类的还好一点,可以通过创建索引来优化,但对于like ‘c%’之类的,真的就没有办法了。

这里介绍一种postgresql关于like ‘c%’的优化方法,是基于全文检索的特性来实现的。

测试数据准备(环境centos6.5 + postgresql 9.6.1)。

postgres=# create table ts(id int,name text);
CREATE TABLE
postgres=# d ts
Table “public.ts”
Column | Type | Modifiers
——–+———+———–
id | integer |
name | text |
postgres=# insert into ts select n,n||’_pjy’ from generate_series(1,2000) n;
INSERT 0 2000
postgres=# insert into ts select n,n||’_mdh’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_lmm’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_syf’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_wbd’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_hhh’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_sjw’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_jjs’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_ymd’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_biu’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# insert into ts select n,n||’_dfl’ from generate_series(1,2000000) n;
INSERT 0 2000000
postgres=# select count(*) from ts;
count
———-
20002000
(1 row)

展开全部

相关文章

说两句网友评论
    我要跟贴
    取消