【GaussDB】GaussDB中的全文索引
全文索引主要应用场景是解决like模糊查询的效率问题,一般的btree类型索引,无法利用索引,容易造成全表扫描,效率低下。GaussDB中的全文索引也叫通用倒排索引(gin)。
自505.2版本开始,针对Ustore存储引擎,GaussDB集中式引入了ugin索引 ,like模糊条件可以利用ugin索引进行高效查询,但实际测试,对中文模糊搜索支持是有问题的,见实际测试示例。
支持情况
|
模式 |
Astore(gin) |
Ustore(ugin) |
|
集中式 |
√ |
√* |
|
分布式 |
√ |
× |
集中式
testdb=> select substring(version(),10,20) ||'_' ||gs_deployment();
?column?
------------------------------------------
GaussDB Kernel 505.2_BusinessCentralized
(1 row)
集中式开启Astore gin索引
505.2中,enable_astore_gin默认值是off,需要开启才能支持创建gin索引,否则报错
testdb=> CREATE INDEX trgm_idx ON test_trgm USING gin (to_tsvector('ngram'::regconfig, (t)::text)) ;
ERROR: Access method gin is not supported for astore/cstore.
此参数需要重启数据库
testdb=> show enable_astore_gin;
enable_astore_gin
-------------------
off
(1 row)
[omm@guassdb-121 ~]$ gs_guc set -N all -I all -Z datanode -c "enable_astore_gin=on"
The gs_guc run with the following arguments: [gs_guc -N all -I all -Z datanode -c enable_astore_gin=on set ].
Begin to perform the total nodes: 3.
Popen count is 3, Popen success count is 3, Popen failure count is 0.
Begin to perform gs_guc for datanodes.
Command count is 3, Command success count is 3, Command failure count is 0.
Total instances: 3. Failed instances: 0.
ALL: Success to perform gs_guc!
[omm@guassdb-121 ~]$ cm_ctl stop
[omm@guassdb-121 ~]$ cm_ctl start
gaussdb=# show enable_astore_gin;
enable_astore_gin
-------------------
on
(1 row)
集中式Astore gin索引
创建测试表与数据
CREATE TABLE test_trgm(t varchar(100)) WITH (STORAGE_TYPE=astore);
INSERT INTO test_trgm VALUES('qwertyu0001');
INSERT INTO test_trgm VALUES('qwertyu0099');
INSERT INTO test_trgm VALUES('2024新年快乐7days');
-- 使用存储过程多写入点数据
-- 创建函数来循环插入数据
CREATE OR REPLACE FUNCTION insert_test_trgm_data()
RETURNS void AS $$
DECLARE
i integer;
BEGIN
-- 从1循环到10000
FOR i IN 1..10000 LOOP
-- 使用lpad函数确保数字是4位,不足前面补0
INSERT INTO test_trgm VALUES (
'qwertyu' || lpad(i::text, 4, '0')
);
END LOOP;
END;
$$ LANGUAGE plpgsql;
-- 执行函数插入数据
SELECT insert_test_trgm_data();
-- 可选:删除函数(如果需要)
-- DROP FUNCTION IF EXISTS insert_test_trgm_data();
集中式Astore创建gin索引
testdb=> create index trgm_idx on test_trgm using gin(to_tsvector('ngram',t));
CREATE INDEX
集中式Astore gin索引使用
teestdb=> explain analyze select * from test_trgm where to_tsvector('ngram',t) @@ to_tsquery('新年') ;
id | operation | A-time | A-rows | E-rows | Peak Memory | A-width | E-width | E-costs
----+-----------------------------------------+--------+--------+--------+-------------+---------+---------+---------------
1 | -> Bitmap Heap Scan on test_trgm | 0.035 | 1 | 50 | 27KB | | 218 | 8.390..65.379
2 | -> Bitmap Index Scan using trgm_idx | 0.021 | 1 | 50 | 50KB | | 0 | 0.000..8.378
(2 rows)
Predicate Information (identified by plan id)
-------------------------------------------------------------------------------------------
1 --Bitmap Heap Scan on test_trgm
Recheck Cond: (to_tsvector('ngram'::regconfig, (t)::text) @@ '''新年'''::tsquery)
2 --Bitmap Index Scan using trgm_idx
Index Cond: (to_tsvector('ngram'::regconfig, (t)::text) @@ '''新年'''::tsquery)
(4 rows)
====== Query Summary =====
----------------------------------------
Datanode executor start time: 0.088 ms
Datanode executor run time: 0.058 ms
Datanode executor end time: 0.014 ms
Planner runtime: 0.259 ms
Query Id: 1950058638651436762
Total runtime: 0.173 ms
(6 rows)
结论:从如上实验可以看出,集中式Astore支持gin类型的全文索引。
集中式Ustore ugin索引
创建测试表与数据
CREATE TABLE test_trgm(t varchar(100 char)) WITH (STORAGE_TYPE=Ustore);
INSERT INTO test_trgm VALUES('qwertyu0001');
INSERT INTO test_trgm VALUES('qwertyu0099');
INSERT INTO test_trgm VALUES('2024新年快乐7days');
-- 使用存储过程多写入点数据
-- 创建函数来循环插入数据
CREATE OR REPLACE FUNCTION insert_test_trgm_data()
RETURNS void AS $$
DECLARE
i integer;
BEGIN
-- 从1循环到10000
FOR i IN 1..10000 LOOP
-- 使用lpad函数确保数字是4位,不足前面补0
INSERT INTO test_trgm VALUES (
'qwertyu' || lpad(i::text, 4, '0')
);
END LOOP;
END;
$$ LANGUAGE plpgsql;
-- 执行函数插入数据
SELECT insert_test_trgm_data();
-- 可选:删除函数(如果需要)
-- DROP FUNCTION IF EXISTS insert_test_trgm_data();
集中式Ustore创建ugin索引
testdb=> create index trgm_idx on test_trgm using ugin (t ugin_trgm_ops);
CREATE INDEX
集中式Ustore ugin索引使用
testdb=> select * from test_trgm where t like '%26新%';
t
-------------------
2026新年快乐7days
(1 row)
testdb=> explain select * from test_trgm where t like '%26新%';
id | operation | E-rows | E-width | E-costs
----+-----------------------------------------+--------+---------+----------------
1 | -> Bitmap Heap Scan on test_trgm | 400 | 218 | 15.101..59.101
2 | -> Bitmap Index Scan using trgm_idx | 400 | 0 | 0.000..15.001
(2 rows)
Predicate Information (identified by plan id)
-----------------------------------------------------
1 --Bitmap Heap Scan on test_trgm
Recheck Cond: ((t)::text ~~ '%24新%'::text)
2 --Bitmap Index Scan using trgm_idx
Index Cond: ((t)::text ~~ '%24新%'::text)
(4 rows)
但发现纯中文搜索的时候,并不能走到ugin索引,猜测可能是如下原因:
trgm索引基于三字节子串(trigram)分割文本,但中文字符是多字节(UTF-8中占3字节),而trgm默认按字节分割,可能导致中文分词异常。
testdb=> explain select * from test_trgm where t like '%新年快乐%';
id | operation | E-rows | E-width | E-costs
----+---------------------------+--------+---------+----------------
1 | -> Seq Scan on test_trgm | 1 | 12 | 0.000..164.088
(1 row)
Predicate Information (identified by plan id)
---------------------------------------------------
1 --Seq Scan on test_trgm
Filter: ((t)::text ~~ '%新年快乐%'::text)
(2 rows)
结论:从如上实验可以看出,集中式Ustore支持ugin索引,且不需要改造SQL,直接like模糊使用,但在纯中文模糊的情况下,是有问题的。
分布式
gaussdb=# select substring(version(),10,20) ||'_' ||gs_deployment();
?column?
---------------------------------
GaussDB Kernel 505.2_Distribute
(1 row)
分布式打开Astore gin索引
分布式中,enable_astore_gin默认也是off,需要打开。
[Ruby@gaussdb01 ~]$ gs_guc set -Z coordinator -Z datanode -N all -I all -c "enable_astore_gin=on"
The gs_guc run with the following arguments: [gs_guc -Z coordinator -Z datanode -N all -I all -c enable_astore_gin=on set ].
Begin to perform the total nodes: 3.
Popen count is 3, Popen success count is 3, Popen failure count is 0.
Begin to perform gs_guc for coordinators.
Command count is 3, Command success count is 3, Command failure count is 0.
Total instances: 3. Failed instances: 0.
ALL: Success to perform gs_guc!
Begin to perform the total nodes: 3.
Popen count is 3, Popen success count is 3, Popen failure count is 0.
Begin to perform gs_guc for datanodes.
Command count is 3, Command success count is 3, Command failure count is 0.
Total instances: 9. Failed instances: 0.
ALL: Success to perform gs_guc!
[Ruby@gaussdb01 ~]$ cm_ctl stop
[Ruby@gaussdb01 ~]$ cm_ctl start
gaussdb=# show enable_astore_gin;
enable_astore_gin
-------------------
on
(1 row)
分布式Astore gin索引
创建测试表与数据
CREATE TABLE test_trgm(t varchar(100)) WITH (STORAGE_TYPE=astore);
INSERT INTO test_trgm VALUES('qwertyu0001');
INSERT INTO test_trgm VALUES('qwertyu0099');
INSERT INTO test_trgm VALUES('2024新年快乐7days');
-- 使用存储过程多写入点数据
-- 创建函数来循环插入数据
CREATE OR REPLACE FUNCTION insert_test_trgm_data()
RETURNS void AS $$
DECLARE
i integer;
BEGIN
-- 从1循环到10000
FOR i IN 1..10000 LOOP
-- 使用lpad函数确保数字是4位,不足前面补0
INSERT INTO test_trgm VALUES (
'qwertyu' || lpad(i::text, 4, '0')
);
END LOOP;
END;
$$ LANGUAGE plpgsql;
-- 执行函数插入数据
SELECT insert_test_trgm_data();
分布式Astore创建gin索引
testdb=> create index trgm_idx on test_trgm using gin(to_tsvector('ngram',t));
CREATE INDEX
分布式Astore gin索引使用
testdb=> set max_datanode_for_plan=1;
SET
testdb=> explain verbose select * from test_trgm where to_tsvector('ngram',t) @@ to_tsquery('新年') ;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
-----
Data Node Scan (cost=0.00..0.00 rows=0 width=0)
Output: test_trgm.t
Node/s: All datanodes
Remote query: SELECT t FROM cuige.test_trgm WHERE to_tsvector('ngram'::regconfig, t::text) @@ to_tsquery('新年'::t
ext)
Remote SQL: SELECT t FROM cuige.test_trgm WHERE to_tsvector('ngram'::regconfig, t::text) @@ to_tsquery('新年'::text)
Datanode Name: dn_6001_6002_6003
Bitmap Heap Scan on cuige.test_trgm (cost=8.13..26.97 rows=16 width=218)
Output: t
Recheck Cond: (to_tsvector('ngram'::regconfig, (test_trgm.t)::text) @@ '''新年'''::tsquery)
-> Bitmap Index Scan on trgm_idx (cost=0.00..8.12 rows=16 width=0)
Index Cond: (to_tsvector('ngram'::regconfig, (test_trgm.t)::text) @@ '''新年'''::tsquery)
(13 rows)
结论:从如上实验可以看出,分布式Astore支持gin索引,使用方式和集中式一样。
分布式Ustore ugin索引
创建测试表与数据
CREATE TABLE test_trgm(t varchar(100)) WITH (STORAGE_TYPE=Ustore);
INSERT INTO test_trgm VALUES('qwertyu0001');
INSERT INTO test_trgm VALUES('qwertyu0099');
INSERT INTO test_trgm VALUES('2024新年快乐7days');
-- 使用存储过程多写入点数据
-- 创建函数来循环插入数据
CREATE OR REPLACE FUNCTION insert_test_trgm_data()
RETURNS void AS $$
DECLARE
i integer;
BEGIN
-- 从1循环到10000
FOR i IN 1..10000 LOOP
-- 使用lpad函数确保数字是4位,不足前面补0
INSERT INTO test_trgm VALUES (
'qwertyu' || lpad(i::text, 4, '0')
);
END LOOP;
END;
$$ LANGUAGE plpgsql;
-- 执行函数插入数据
SELECT insert_test_trgm_data();
-- 可选:删除函数(如果需要)
-- DROP FUNCTION IF EXISTS insert_test_trgm_data();
分布式Ustore创建ugin索引
gaussdb=# create index trgm_idx on test_trgm using ugin (t ugin_trgm_ops);
ERROR: Unsupported function.
DETAIL: Ugin index is not supported in distributed database.
gaussdb=# drop table test_trgm;
DROP TABLE
结论:从如上实验可以看出,分布式Ustore不支持ugin索引。
总结
从实验看出,虽然505.2引入了ugin索引,且使用方式简便,不需要改造SQL,直接模糊like搜索就可以完成,但实际测试发现,对中文的支持是存在一些问题的,在有实际需求场景下,建议仍然使用Astore创建gin索引。
转载:阿才漫谈
更多推荐



所有评论(0)