Bug #121934 InnoDB FULLTEXT proximity query repeats work for frequent separated terms
Description:
An InnoDB FULLTEXT proximity query is excessively slow when two frequent terms occur in large separated blocks.
The table contains one document with 200,000 occurrences of `alpha`, followed by 200,000 `gamma` fillers, then 200,000 occurrences of `beta`. The proximity query requests distance 1,000. It and a monotone boundary calculation both return:
matched
0
Three alternating measurements produced:
baseline: 28.5 ms; proximity query: 9714.4 ms; ratio: 340.8
baseline: 28.4 ms; proximity query: 9986.3 ms; ratio: 351.4
baseline: 28.9 ms; proximity query: 9737.3 ms; ratio: 337.1
The faster statement checks the nearest `alpha`/`beta` boundary, which is sufficient for this ordered-block document. The proximity query appears to repeat verification over many overlapping candidate ranges instead of retaining monotone position progress.
EXPLAIN FORMAT=TREE for the proximity query returned:
-> Rows fetched before execution (cost=0..0 rows=1)
How to repeat:
Run as root in the mysql client:
CREATE DATABASE db1;
USE db1;
CREATE TABLE t1 (
id INT NOT NULL PRIMARY KEY,
body LONGTEXT NOT NULL
) ENGINE=InnoDB;
INSERT INTO t1 VALUES
(1, CONCAT(REPEAT('alpha ', 200000),
REPEAT('gamma ', 200000),
REPEAT('beta ', 200000)));
ALTER TABLE t1 ADD FULLTEXT KEY ft_body (body);
SELECT COUNT(*) AS matched
FROM t1
WHERE CHAR_LENGTH(
SUBSTRING_INDEX(SUBSTRING_INDEX(body, 'beta ', 1), 'alpha ', -1)
) / CHAR_LENGTH('gamma ') <= 1000;
-- returns 0; measured at 28.4-28.9 ms
SELECT COUNT(*) AS matched
FROM t1
WHERE MATCH(body) AGAINST ('"alpha beta"@1000' IN BOOLEAN MODE);
-- returns 0; measured at 9714.4-9986.3 ms
EXPLAIN FORMAT=TREE
SELECT COUNT(*) AS matched
FROM t1
WHERE MATCH(body) AGAINST ('"alpha beta"@1000' IN BOOLEAN MODE);
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.039 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.
Description: An InnoDB FULLTEXT proximity query is excessively slow when two frequent terms occur in large separated blocks. The table contains one document with 200,000 occurrences of `alpha`, followed by 200,000 `gamma` fillers, then 200,000 occurrences of `beta`. The proximity query requests distance 1,000. It and a monotone boundary calculation both return: matched 0 Three alternating measurements produced: baseline: 28.5 ms; proximity query: 9714.4 ms; ratio: 340.8 baseline: 28.4 ms; proximity query: 9986.3 ms; ratio: 351.4 baseline: 28.9 ms; proximity query: 9737.3 ms; ratio: 337.1 The faster statement checks the nearest `alpha`/`beta` boundary, which is sufficient for this ordered-block document. The proximity query appears to repeat verification over many overlapping candidate ranges instead of retaining monotone position progress. EXPLAIN FORMAT=TREE for the proximity query returned: -> Rows fetched before execution (cost=0..0 rows=1) How to repeat: Run as root in the mysql client: CREATE DATABASE db1; USE db1; CREATE TABLE t1 ( id INT NOT NULL PRIMARY KEY, body LONGTEXT NOT NULL ) ENGINE=InnoDB; INSERT INTO t1 VALUES (1, CONCAT(REPEAT('alpha ', 200000), REPEAT('gamma ', 200000), REPEAT('beta ', 200000))); ALTER TABLE t1 ADD FULLTEXT KEY ft_body (body); SELECT COUNT(*) AS matched FROM t1 WHERE CHAR_LENGTH( SUBSTRING_INDEX(SUBSTRING_INDEX(body, 'beta ', 1), 'alpha ', -1) ) / CHAR_LENGTH('gamma ') <= 1000; -- returns 0; measured at 28.4-28.9 ms SELECT COUNT(*) AS matched FROM t1 WHERE MATCH(body) AGAINST ('"alpha beta"@1000' IN BOOLEAN MODE); -- returns 0; measured at 9714.4-9986.3 ms EXPLAIN FORMAT=TREE SELECT COUNT(*) AS matched FROM t1 WHERE MATCH(body) AGAINST ('"alpha beta"@1000' IN BOOLEAN MODE);