時(shí)間:2024-02-07 12:09作者:下載吧人氣:18
任何一個(gè)關(guān)系型數(shù)據(jù)庫(kù)關(guān)于模糊匹配(like)的優(yōu)化都是一件痛苦的事,相對(duì)而言,諸如like ‘abc%’之類的還好一點(diǎn),可以通過(guò)創(chuàng)建索引來(lái)優(yōu)化,但對(duì)于like ‘c%’之類的,真的就沒(méi)有辦法了。
這里介紹一種postgresql關(guān)于like ‘c%’的優(yōu)化方法,是基于全文檢索的特性來(lái)實(shí)現(xiàn)的。
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)
網(wǎng)友評(píng)論