GBase 8a全文索引创建、更新和删除方法

本文介绍GBase 8a MPP数据库集群全文索引FULLTEXT的创建索引,更新索引和删除索引的方法。

参考

GBase 8a全文索引功能安装部署方法
GBase 8a全文索引创建、更新和删除方法
GBase 8a全文索引常用配置文件和配置参数
GBase 8a全文索引提高模糊查询性能使用样例
GBase 8a全文索引多分词器的功能介绍和使用

创建全文索引

CREATE FULLTEXT INDEX index_name ON table_name (column_name) [INDEX_DATA_PATH='path'] [WITH PARSER seg_name]
  • index_name : 全文索引名字,一张表内必须唯一
  • table_name : 表名字
  • column_name :列名字,只支持单列
  • INDEX_DATA_PATH:可选项,设置索引数据路径标志。如不填写,则索引数据保存在默认路径上
  • WITH PARSER :指定分词器的名字。不指定则使用默认分词器。

如下是使用默认分词器和指定分词器的样例。

gbase> create fulltext index idx_name on t1(name);
Query OK, 0 rows affected (Elapsed: 00:00:00.18)
Records: 0  Duplicates: 0  Warnings: 0

gbase> create fulltext index idx_memo on t1(memo) with parser part4;
Query OK, 0 rows affected (Elapsed: 00:00:00.16)
Records: 0  Duplicates: 0  Warnings: 0

创建指定索引数据路径的例子

gbase> create fulltext index idx_name on t1(name) INDEX_DATA_PATH='/home/gbase/gbasefulltextdata' with parser part2;
Query OK, 0 rows affected (Elapsed: 00:00:00.29)
Records: 0  Duplicates: 0  Warnings: 0

gbase> update index idx_name on t1;
Query OK, 7 rows affected (Elapsed: 00:00:01.27)

gbase> system ls -l /home/gbase/gbasefulltextdata
total 0
drwx------. 3 gbase gbase 59 Jul  1 16:46 gbase_testdb_t1_n1_C00001.FTD
drwx------. 2 gbase gbase  6 Jul  1 16:46 gbase_testdb_t1_n2_C00001.FTD
drwx------. 2 gbase gbase  6 Jul  1 16:46 gbase_vc00001_testdb_t1_C00001.FTD
gbase>

查看全文索引列表

通过show index from TABLE_NAME查看该表的索引信息。

gbase> show index from t1;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1    |          1 | idx_memo |            1 | memo        | NULL      |        NULL |     NULL | NULL   | YES  | FULLTEXT   |         |
| t1    |          1 | idx_name |            1 | name        | NULL      |        NULL |     NULL | NULL   | YES  | FULLTEXT   |         |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (Elapsed: 00:00:00.00)

查看全文索引的建表语句

其中包含了索引数据位置,分词名字等信息。没有的则用默认值。

gbase> show create table t1;
。。。。
| t1    | CREATE TABLE "t1" (
  "id" int(11) DEFAULT NULL,
  "name" varchar(100) DEFAULT NULL,
  "dept" int(11) DEFAULT NULL,
  "birth" date DEFAULT NULL,
  "memo" longtext,
  FULLTEXT "idx_memo" ("memo") WITH PARSER "part4",
  FULLTEXT "idx_name" ("name")INDEX_DATA_PATH='/home/gbase/gbasefulltextdata' WITH PARSER "part2"
) ENGINE=EXPRESS DEFAULT CHARSET=utf8 TABLESPACE='sys_tablespace' |

更新全文索引数据

UPDATE INDEX index_name ON table_name [WITH ANALYZE];

其中WITH ANALYZE分析指令,加入后更新全文索时会对不连续数据重整,提升I/O 速度,从而提升性能。但重建时需要时间和资源的,建议根据系统负载,找闲暇时定期执行。

gbase> update index idx_memo on t1;
Query OK, 7 rows affected (Elapsed: 00:00:01.10)

gbase> update index idx_name on t1;
Query OK, 7 rows affected (Elapsed: 00:00:01.15)

删除全文索引

和删除其它索引一样的语法。

DROP INDEX index_name ON table_name
gbase> drop index idx_memo on t1;
Query OK, 0 rows affected (Elapsed: 00:00:00.39)
Records: 0  Duplicates: 0  Warnings: 0

gbase> drop index idx_name on t1;
Query OK, 0 rows affected (Elapsed: 00:00:00.13)
Records: 0  Duplicates: 0  Warnings: 0