一、DM8全文检索概述:理解核心概念
1.1 什么是全文检索:基础概念解析
全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置。当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式。达梦数据库(DM8)提供了强大的全文检索功能,支持中英文分词,能够高效处理大文本字段的复杂查询。
1.2 DM8全文检索架构:技术原理解析
DM8的全文检索主要通过全文索引来实现。其底层依赖于专门的全文检索组件和分词器。全文索引将文本数据分解为词元,并存储在独立的索引表中。当执行查询时,系统直接查找索引表而非遍历原始大文本数据,从而大幅提升查询速度。
#publish-mermaid-1786239970996-0{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1786239970996-0 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1786239970996-0 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1786239970996-0 .error-icon{fill:#552222;}#publish-mermaid-1786239970996-0 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1786239970996-0 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1786239970996-0 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1786239970996-0 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1786239970996-0 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1786239970996-0 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1786239970996-0 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1786239970996-0 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1786239970996-0 .marker.cross{stroke:#333333;}#publish-mermaid-1786239970996-0 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1786239970996-0 p{margin:0;}#publish-mermaid-1786239970996-0 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1786239970996-0 .cluster-label text{fill:#333;}#publish-mermaid-1786239970996-0 .cluster-label span{color:#333;}#publish-mermaid-1786239970996-0 .cluster-label span p{background-color:transparent;}#publish-mermaid-1786239970996-0 .label text,#publish-mermaid-1786239970996-0 span{fill:#333;color:#333;}#publish-mermaid-1786239970996-0 .node rect,#publish-mermaid-1786239970996-0 .node circle,#publish-mermaid-1786239970996-0 .node ellipse,#publish-mermaid-1786239970996-0 .node polygon,#publish-mermaid-1786239970996-0 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1786239970996-0 .rough-node .label text,#publish-mermaid-1786239970996-0 .node .label text,#publish-mermaid-1786239970996-0 .image-shape .label,#publish-mermaid-1786239970996-0 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1786239970996-0 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1786239970996-0 .rough-node .label,#publish-mermaid-1786239970996-0 .node .label,#publish-mermaid-1786239970996-0 .image-shape .label,#publish-mermaid-1786239970996-0 .icon-shape .label{text-align:center;}#publish-mermaid-1786239970996-0 .node.clickable{cursor:pointer;}#publish-mermaid-1786239970996-0 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1786239970996-0 .arrowheadPath{fill:#333333;}#publish-mermaid-1786239970996-0 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1786239970996-0 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1786239970996-0 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1786239970996-0 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1786239970996-0 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1786239970996-0 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1786239970996-0 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1786239970996-0 .cluster text{fill:#333;}#publish-mermaid-1786239970996-0 .cluster span{color:#333;}#publish-mermaid-1786239970996-0 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1786239970996-0 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1786239970996-0 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1786239970996-0 .icon-shape,#publish-mermaid-1786239970996-0 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1786239970996-0 .icon-shape p,#publish-mermaid-1786239970996-0 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1786239970996-0 .icon-shape .label rect,#publish-mermaid-1786239970996-0 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1786239970996-0 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1786239970996-0 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1786239970996-0 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].node rect,#publish-mermaid-1786239970996-0 [data-look=\”neo\”].cluster rect,#publish-mermaid-1786239970996-0 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239970996-0 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239970996-0 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
插入/更新大文本数据
触发器捕获变更
异步更新全文索引表
用户执行contains查询
查询解析与分词
检索全文索引表
返回匹配结果集
1.3 全文检索应用场景:业务价值体现
全文检索广泛应用于各类需要处理大量非结构化文本的业务场景中,如:企业知识库文档搜索、电商平台商品描述检索、日志系统故障排查以及政务系统的公文检索等。使用DM8全文检索可以避免使用like "%keyword%"带来的全表扫描性能问题。
二、DM8全文检索配置:环境搭建与词库管理
2.1 开启全文检索服务:系统环境配置
在DM8中使用全文检索前,需要确保数据库服务端已加载相关的全文检索插件。通常在dm.ini配置文件中设置参数,并确保对应的动态库已被正确加载。
2.2 中文分词器配置:提升检索准确性
DM8内置了中文分词器。对于特定的业务需求,可以通过修改分词词库来定制分词规则。确保分词准确是全文检索高效的核心。可以通过系统过程SPAlterDict来修改系统默认词库。
2.3 创建全文索引:核心操作步骤
要在表中使用全文检索,必须先创建全文索引。以下是创建全文索引的详细步骤:
代码示例:
— 1. 创建测试表
CREATE TABLE docs (
id INT PRIMARY KEY,
title VARCHAR(100),
content TEXT
);
— 2. 插入测试数据
INSERT INTO docs VALUES (1, "DM8数据库", "达梦数据库提供了强大的全文检索功能,支持中文分词。");
INSERT INTO docs VALUES (2, "性能优化", "通过建立全文索引可以大幅提升查询效率,避免全表扫描。");
COMMIT;
— 3. 创建全文索引
CREATE CONTEXT INDEX ctx_idx_content ON docs(content) LEXER CHINESE_LEXER;
三、DM8全文检索使用:语法解析与实战
3.1 全文检索SQL语法:基础查询操作
在DM8中,全文检索主要通过CONTAINS谓词来实现。基本语法如下:
SELECT * FROM table_name WHERE CONTAINS(column_name, "search_string") > 0;
其中,大于0表示匹配成功。search_string支持多种匹配模式。
3.2 contains子句使用:多条件组合查询
CONTAINS子句不仅支持单词查询,还支持逻辑运算符(AND, OR, NOT)以及通配符查询。
SELECT * FROM docs WHERE CONTAINS(content, "达梦 AND 全文检索") > 0;
SELECT * FROM docs WHERE CONTAINS(content, "数据库 OR 优化") > 0;
SELECT * FROM docs WHERE CONTAINS(content, "数据*") > 0;
3.3 全文检索实战演练:具体业务场景
假设我们需要在一个大型文章系统中搜索同时包含"达梦"和"性能"但不包含"测试"的文章,可以使用以下查询:
SELECT id, title, content FROM docs
WHERE CONTAINS(content, "达梦 AND 性能 AND NOT 测试") > 0;
执行流程图如下:
#publish-mermaid-1786239971031-1{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#publish-mermaid-1786239971031-1 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#publish-mermaid-1786239971031-1 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#publish-mermaid-1786239971031-1 .error-icon{fill:#552222;}#publish-mermaid-1786239971031-1 .error-text{fill:#552222;stroke:#552222;}#publish-mermaid-1786239971031-1 .edge-thickness-normal{stroke-width:1px;}#publish-mermaid-1786239971031-1 .edge-thickness-thick{stroke-width:3.5px;}#publish-mermaid-1786239971031-1 .edge-pattern-solid{stroke-dasharray:0;}#publish-mermaid-1786239971031-1 .edge-thickness-invisible{stroke-width:0;fill:none;}#publish-mermaid-1786239971031-1 .edge-pattern-dashed{stroke-dasharray:3;}#publish-mermaid-1786239971031-1 .edge-pattern-dotted{stroke-dasharray:2;}#publish-mermaid-1786239971031-1 .marker{fill:#333333;stroke:#333333;}#publish-mermaid-1786239971031-1 .marker.cross{stroke:#333333;}#publish-mermaid-1786239971031-1 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#publish-mermaid-1786239971031-1 p{margin:0;}#publish-mermaid-1786239971031-1 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#publish-mermaid-1786239971031-1 .cluster-label text{fill:#333;}#publish-mermaid-1786239971031-1 .cluster-label span{color:#333;}#publish-mermaid-1786239971031-1 .cluster-label span p{background-color:transparent;}#publish-mermaid-1786239971031-1 .label text,#publish-mermaid-1786239971031-1 span{fill:#333;color:#333;}#publish-mermaid-1786239971031-1 .node rect,#publish-mermaid-1786239971031-1 .node circle,#publish-mermaid-1786239971031-1 .node ellipse,#publish-mermaid-1786239971031-1 .node polygon,#publish-mermaid-1786239971031-1 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1786239971031-1 .rough-node .label text,#publish-mermaid-1786239971031-1 .node .label text,#publish-mermaid-1786239971031-1 .image-shape .label,#publish-mermaid-1786239971031-1 .icon-shape .label{text-anchor:middle;}#publish-mermaid-1786239971031-1 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#publish-mermaid-1786239971031-1 .rough-node .label,#publish-mermaid-1786239971031-1 .node .label,#publish-mermaid-1786239971031-1 .image-shape .label,#publish-mermaid-1786239971031-1 .icon-shape .label{text-align:center;}#publish-mermaid-1786239971031-1 .node.clickable{cursor:pointer;}#publish-mermaid-1786239971031-1 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#publish-mermaid-1786239971031-1 .arrowheadPath{fill:#333333;}#publish-mermaid-1786239971031-1 .edgePath .path{stroke:#333333;stroke-width:1px;}#publish-mermaid-1786239971031-1 .flowchart-link{stroke:#333333;fill:none;}#publish-mermaid-1786239971031-1 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1786239971031-1 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#publish-mermaid-1786239971031-1 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1786239971031-1 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#publish-mermaid-1786239971031-1 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#publish-mermaid-1786239971031-1 .cluster text{fill:#333;}#publish-mermaid-1786239971031-1 .cluster span{color:#333;}#publish-mermaid-1786239971031-1 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#publish-mermaid-1786239971031-1 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#publish-mermaid-1786239971031-1 rect.text{fill:none;stroke-width:0;}#publish-mermaid-1786239971031-1 .icon-shape,#publish-mermaid-1786239971031-1 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#publish-mermaid-1786239971031-1 .icon-shape p,#publish-mermaid-1786239971031-1 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#publish-mermaid-1786239971031-1 .icon-shape .label rect,#publish-mermaid-1786239971031-1 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#publish-mermaid-1786239971031-1 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#publish-mermaid-1786239971031-1 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#publish-mermaid-1786239971031-1 .node .neo-node{stroke:#9370DB;}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].node rect,#publish-mermaid-1786239971031-1 [data-look=\”neo\”].cluster rect,#publish-mermaid-1786239971031-1 [data-look=\”neo\”].node polygon{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].swimlane.cluster rect{filter:none;}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].node path{stroke:#9370DB;stroke-width:1px;}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].node .outer-path{filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].node .neo-line path{stroke:#9370DB;filter:none;}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].node circle{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].node circle .state-start{fill:#000000;}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].icon-shape .icon{fill:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239971031-1 [data-look=\”neo\”].icon-shape .icon-neo path{stroke:#9370DB;filter:drop-shadow(1px 2px 2px rgba(185, 185, 185, 1));}#publish-mermaid-1786239971031-1 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}是否
客户端发送查询请求
解析SQL语句
是否包含CONTAINS
提取检索词并进行分词
在全文索引表中匹配词元
获取匹配的记录ROWID
根据ROWID回表获取完整数据
走普通执行计划
返回最终结果集
四、DM8全文检索优化:性能提升策略
4.1 索引维护与重建:保障检索效率
随着数据的不断增删改,全文索引可能会产生碎片,导致查询性能下降。定期重建全文索引是必要的维护手段。
— 重建全文索引
ALTER CONTEXT INDEX ctx_idx_content ON docs REBUILD;
同时,也可以使用SYNC关键字在数据变更时立即同步索引,但可能会影响DML性能,建议在业务低峰期进行批量同步。
4.2 内存参数调优:突破性能瓶颈
全文检索的性能高度依赖内存。在dm.ini中,可以通过调整SORT_BUF_SIZE和MAX_BUFFER参数来优化检索性能。确保系统有足够的内存供分词和索引排序使用。



