Bug #121937 ngram phrase search repeatedly verifies long matching prefixes

Submitted: 26 Nov 2:43
Reporter: QiFan Liu Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: FULLTEXT search Severity:S5 (Performance)
Version:26.10.0 OS:Linux
Assigned to: CPU Architecture:x86
Tags: fulltext, innodb, match, NGRAM, phrase search

[26 Nov 2:43] QiFan Liu
Description:
An absent quoted phrase against one 250003-character document takes 3006.6–3269.6 ms, while a controlled boundary check returning the same scalar result takes 12.3–12.6 ms. The phrase query was 243.5–263.0 times slower across three alternating runs.

Both statements return matching_rows = 0. The document ends in 'x' and contains no sequence of 128 'x' characters followed by 'y'. The faster boundary-check statement demonstrates that the same absent-match result can be determined by checking only the final character, without phrase verification.

EXPLAIN for the phrase query reports:

-> Rows fetched before execution  (cost=0..0 rows=1)

Rebuilding and analyzing the table with OPTIMIZE TABLE does not remove the behavior. After OPTIMIZE TABLE reported "Table does not support optimize, doing recreate + analyze instead" and status "OK", the phrase query took 2996.5–3216.1 ms versus 12.0–12.6 ms for the boundary check, or 239.1–268.1 times longer.

How to repeat:
-- Run as root:

DROP DATABASE IF EXISTS db1;
CREATE DATABASE db1;
USE db1;

CREATE TABLE t1 (
  id INT NOT NULL PRIMARY KEY,
  body LONGTEXT NOT NULL,
  FULLTEXT KEY ft_body (body) WITH PARSER ngram
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO t1 VALUES (1, CONCAT('xy ', REPEAT('x', 250000)));

SELECT COUNT(*) AS matching_rows
FROM t1
WHERE RIGHT(body, 1) = 'y';
-- returns 0; measured at 12.3–12.6 ms

SELECT COUNT(*) AS matching_rows
FROM t1
WHERE MATCH(body) AGAINST(
  CONCAT('"', REPEAT('x', 128), 'y"') IN BOOLEAN MODE
);
-- returns 0; measured at 3006.6–3269.6 ms

EXPLAIN SELECT COUNT(*) AS matching_rows
FROM t1
WHERE MATCH(body) AGAINST(
  CONCAT('"', REPEAT('x', 128), 'y"') IN BOOLEAN MODE
);

-- For the rebuilt-index variant, run the following after the INSERT, then repeat both SELECT statements:

OPTIMIZE TABLE t1;

SELECT COUNT(*) AS matching_rows
FROM t1
WHERE RIGHT(body, 1) = 'y';

SELECT COUNT(*) AS matching_rows
FROM t1
WHERE MATCH(body) AGAINST(
  CONCAT('"', REPEAT('x', 128), 'y"') IN BOOLEAN MODE
);

-- The phrase query was still measured at 2996.5–3216.1 ms, compared with 12.0–12.6 ms for the boundary check.

Suggested fix:
Use the positional FULLTEXT postings to reject impossible phrase positions without restarting tokenization and comparing the same long prefix at every candidate position.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.048 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.