搜索性能分析

查询如何被解析

考虑这个复杂的查询示例:

"hello world" @title "example program"~5 @body python -(php|perl) @* code

此搜索的完整含义是:

  • 在文档的任何字段中定位相邻出现的单词“hello”和“world”;
  • 此外,同一文档的标题字段中必须包含单词“example”和“program”,且它们之间最多(但不包括)有5个单词;(例如,“example PHP program”会匹配,但“example script to introduce outside data into the correct context for your program”则不会,因为这两个术语之间有5个或更多单词)
  • 再者,同一文档的正文字段中必须包含单词“python”,同时排除“php”或“perl”;
  • 最后,同一文档必须在任何字段中包含单词“code”。

OR运算符的优先级高于AND,因此“looking for cat | dog | mouse”意味着“looking for (cat | dog | mouse)”,而不是“(looking for cat) | dog | mouse”。

为了理解查询将如何执行,Manticore Search提供了查询性能分析工具来检查查询表达式生成的查询树。

在SQL中分析查询树

要通过SQL语句启用全文查询性能分析,必须在执行目标查询前激活它:

SET profiling =1;
SELECT * FROM test WHERE MATCH('@title abc* @body hey');

要查看查询树,请在运行查询后立即执行SHOW PLAN命令:

SHOW PLAN;

此命令将返回已执行查询的结构。请注意,这3条语句——SET profiling、查询本身和SHOW——必须在同一会话中执行。

在HTTP JSON中分析查询

使用HTTP JSON协议时,我们只需启用"profile":true即可在响应中获取全文查询树结构。

{
  "table":"test",
  "profile":true,
  "query":
  {
    "match_phrase": { "_all" : "had grown quite" }
  }
}

响应将包含一个profile对象,其中有一个query成员。

query属性保存转换后的全文查询树。每个节点包含:

  • type:节点类型,可以是AND、OR、PHRASE、KEYWORD等。
  • description:此节点的查询子树,以字符串形式表示(采用SHOW PLAN格式)
  • children:存在的任何子节点
  • max_field_pos:字段内的最大位置

关键字节点还将额外包括:

  • word:转换后的关键字。
  • querypos:此关键字在查询中的位置。
  • excluded:从查询中排除的关键字。
  • expanded:通过前缀扩展添加的关键字。
  • field_start:关键字必须出现在字段的开头。
  • field_end:关键字必须出现在字段的末尾。
  • boost:此关键字的IDF将乘以该值。
‹›
  • SQL
  • JSON
  • PHP
  • Python
  • Python-asyncio
  • javascript
  • Java
  • C#
  • Rust
  • TypeScript
  • Go
📋
SET profiling=1;
SELECT * FROM test WHERE MATCH('@title abc* @body hey');
SHOW PLAN \G
‹›
Response
*************************** 1\. row ***************************
Variable: transformed_tree
   Value: AND(
  OR(fields=(title), KEYWORD(abcx, querypos=1, expanded), KEYWORD(abcm, querypos=1, expanded)),
  AND(fields=(body), KEYWORD(hey, querypos=2)))
1 row in set (0.00 sec)

在某些情况下,由于扩展和其他转换,评估后的查询树可能与原始查询树有显著差异。

‹›
  • SQL
  • JSON
  • PHP
  • Python
  • Python-asyncio
  • javascript
  • Java
  • C#
  • Rust
  • TypeScript
  • Go
📋
SET profiling=1;
SELECT id FROM forum WHERE MATCH('@title way* @content hey') LIMIT 1;
SHOW PLAN;
‹›
Response
Query OK, 0 rows affected (0.00 sec)
+--------+
| id     |
+--------+
| 711651 |
+--------+
1 row in set (0.04 sec)
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Variable         | Value                                                                                                                                                                                                                                                                                                                                                                                                                   |
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| transformed_tree | AND(
  OR(
    OR(
      AND(fields=(title), KEYWORD(wayne, querypos=1, expanded)),
      OR(
        AND(fields=(title), KEYWORD(ways, querypos=1, expanded)),
        AND(fields=(title), KEYWORD(wayyy, querypos=1, expanded)))),
    AND(fields=(title), KEYWORD(way, querypos=1, expanded)),
    OR(fields=(title), KEYWORD(way*, querypos=1, expanded))),
  AND(fields=(content), KEYWORD(hey, querypos=2))) |
+------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

不运行查询的性能分析

SQL语句EXPLAIN QUERY允许显示给定全文查询的执行树,而无需对表执行实际的搜索查询。

‹›
  • SQL
SQL
📋
EXPLAIN QUERY index_base '@title running @body dog'\G
‹›
Response
 EXPLAIN QUERY index_base '@title running @body dog'\G
*************************** 1\. row ***************************
Variable: transformed_tree
   Value: AND(
      OR(
            AND(fields=(title), KEYWORD(run, querypos=1, morphed)),
            AND(fields=(title), KEYWORD(running, querypos=1, morphed))))
  AND(fields=(body), KEYWORD(dog, querypos=2, morphed)))

EXPLAIN QUERY ... option format=dot允许以分层格式显示所提供全文查询的执行树,适合通过现有工具(如https://dreampuf.github.io/GraphvizOnline)进行可视化:

EXPLAIN QUERY graphviz示例

‹›
  • SQL
SQL
📋
EXPLAIN QUERY tbl 'i me' option format=dot\G
‹›
Response
EXPLAIN QUERY tbl 'i me' option format=dot\G
*************************** 1. row ***************************
Variable: transformed_tree
   Value: digraph "transformed_tree"
{
0 [shape=record,style=filled,bgcolor="lightgrey" label="AND"]
0 -> 1
1 [shape=record,style=filled,bgcolor="lightgrey" label="AND"]
1 -> 2
2 [shape=record label="i | { querypos=1 }"]
0 -> 3
3 [shape=record,style=filled,bgcolor="lightgrey" label="AND"]
3 -> 4
4 [shape=record label="me | { querypos=2 }"]
}

查看匹配因子值

使用表达式排序器时,可以通过PACKEDFACTORS()函数显示计算出的因子值。

该函数返回:

  • 文档级别因素的值(如bm25、field_mask、doc_word_count)
  • 每个生成命中结果的字段列表(包括lcs、hit_count、word_count、sum_idf、min_hit_pos等)
  • 查询中每个关键词及其tf和idf值的列表

这些值可以用于理解某些文档在搜索中为何获得较低或较高的分数,或者用于细化现有的排名表达式。

‹›
  • SQL
SQL
📋
SELECT id, PACKEDFACTORS() FROM test1 WHERE MATCH('test one') OPTION ranker=expr('1')\G
‹›
Response
             id: 1
packedfactors(): bm25=569, bm25a=0.617197, field_mask=2, doc_word_count=2,
    field1=(lcs=1, hit_count=2, word_count=2, tf_idf=0.152356,
        min_idf=-0.062982, max_idf=0.215338, sum_idf=0.152356, min_hit_pos=4,
        min_best_span_pos=4, exact_hit=0, max_window_hits=1, min_gaps=2,
        exact_order=1, lccs=1, wlccs=0.215338, atc=-0.003974),
    word0=(tf=1, idf=-0.062982),
    word1=(tf=1, idf=0.215338)
1 row in set (0.00 sec)
Last modified: August 28, 2025