微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL如何对某行记录进行模糊查询

在某些场景下,我们可能需要对表的某行字段进行查询

例如BI报表的下拉框,用户可能会勾选多个条件进行查询,那么我们查询会很麻烦。

例如:

bill@bill=>create table test1(c1 int,c2 text,c3 text,c4 text);
CREATE TABLE
bill@bill=>insert into test1 values(1,'post','china','bill');
INSERT 0 1
如果我们要查询该表某行包含china的记录,我们可能需要这么去写:

select * from test1 where c1 ~ 'china' or c2 ~ 'china' or ...
可以看到这样写会相当麻烦,而且性能也不尽人意。

那么我们有什么办法能解决这类问题呢?

全文检索,我们可以通过行级别的全文检索来处理这类问题。

例子:
这里我们以pg_scws分词插件来演示。
下载地址:https://github.com/jaiminpan/pg_scws

安装:
git clone https://github.com/jaiminpan/pg_scws
cd pg_scws
USE_PGXS=1 make && make install

以上面的表为例,我们可以将改行记录转成一个全文检索的文本:

bill@bill=>select to_tsvector('scwscfg',test1::text) from test1;
to_tsvector

'1':1 'bill':4 'china':3 'post':2
(1 row)
查询

bill@bill=> select to_tsvector('scwscfg',test1::text) @@ to_tsquery('post & china') from test1;
?column?

t
(1 row)
至此,我们实现了对行级记录的全文检索查询,接着让我们再创建行级全文检索的索引来加速查询

首先我们需要创建immutable类型的函数

bill@bill=> create or replace function f1(regconfig,text) returns tsvector as $$
bill$# select to_tsvector($1,$2);
bill$# $$ language sql immutable strict;
CREATE FUNCTION
需要将record_out和textin函数转为immutable类型:

bill@bill=>alter function record_out(record) immutable;
ALTER FUNCTION
bill@bill=>alter function textin(cstring) immutable;
ALTER FUNCTION
创建索引:

bill@bill=> create index idx_test1 on test1 using gin (f1('scwscfg'::regconfig,test1::text)) ;
CREATE INDEX
查询测试:

bill@bill=>select * from test1 where f1('scwscfg'::regconfig,test1::text) @@ to_tsquery('china') ;
c1 | c2 | c3 | c4
----+------+-------+------
1 | post | china | bill
(1 row)
验证是否可以使用索引:

bill@bill=>set enable_seqscan = off;
SET
bill@bill=> explain select * from test1 where f1('scwscfg'::regconfig,test1::text) @@ to_tsquery('china') ;
QUERY PLAN

Bitmap Heap Scan on test1 (cost=2.85..4.67 rows=1 width=100)
Recheck Cond: (to_tsvector('scwscfg'::regconfig, (test1.)::text) @@ to_tsquery('china'::text))
-> Bitmap Index Scan on idx_test1 (cost=0.00..2.85 rows=1 width=0)
Index Cond: (to_tsvector('scwscfg'::regconfig, (test1.
)::text) @@ to_tsquery('china'::text))
(4 rows)

   

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐