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

本文介绍了如何通过全文检索技术解决 PostgreSQL 中多条件查询的复杂性和性能问题。使用 pg_scws 分词插件创建全文检索索引,并通过示例展示了创建和查询过程,从而实现对行级记录的快速检索,提升查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       例如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)

  参考链接:

  https://github.com/jaiminpan/pg_scws

  http://www.postgres.cn/docs/13/textsearch.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值