Skip to content

MDEV-37370 fts_phrase_or_proximity_search() is quadratic instead of linear - #5975

Open
futureweb wants to merge 1 commit into
MariaDB:11.4from
futureweb:MDEV-37370-fts-phrase-linear
Open

futureweb wants to merge 1 commit into
MariaDB:11.4from
futureweb:MDEV-37370-fts-phrase-linear

Conversation

@futureweb

@futureweb futureweb commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor
  • The Jira issue number for this PR is: MDEV-37370

Description

fts_phrase_or_proximity_search() scanned the match list of every other query word from the start again for each document of the first word (see the removed FIXME). As in the fix for MySQL Bug #37682648 (MySQL 8.0.43), without its boilerplate, each word keeps a cursor into its match list, so matching sorted lists is linear in their lengths for a fixed number of query words. As suggested in the Jira issue, ib_vector_t is kept and there is no Vector_wrapper. The template parameter for the query mode is not part of this change.

The match lists are not always sorted by document ID: cache nodes and overlapping posting lists on disk can produce unsorted lists. With such lists the old scan could already miss documents. For example, after two transactions committed out of document ID order, alpha has the list 10 and bravo the list 30, 10; the scan for bravo stops at 30, and '"alpha bravo"' misses document 10. So each list is checked once and sorted by document ID with std::sort() if it is not sorted, before the cursors are used.

Benchmark (release build of main with all our fixes, the ngram parser proposed in MDEV-10267 with n=2, 20,000 Latin documents, boolean mode queries in which each query word becomes a phrase of n-grams): 37.2 s without this commit, 10.1 s with it, with the same number of hits (1,076,673). MySQL 8.4.13 takes 9.5 s on the same data. A CJK corpus, which has short match lists, is unchanged.

Note for reviewers: the Jira issue has Fix Version 11.8 and is assigned to Thirunarayanan. This PR is based on 11.4 because the sort also fixes wrong results there. If you prefer 11.8, I will rebase it.

Release Notes

InnoDB fulltext phrase and proximity searches are faster for words that occur in many rows. They also no longer miss rows in some cases after transactions were committed out of document ID order.

How can this PR be tested?

New test case in innodb_fts.innodb_fts_proximity: phrase and proximity searches with gaps between document IDs, repeated words, lists that end early, data in the cache and on disk, and two out-of-order commits that give an unsorted first and an unsorted second match list. Without the fix, the phrase and proximity queries miss document 10 after the out-of-order commit.

The innodb_fts suite and the main fulltext tests pass on 11.4 (debug build). On main with all our fixes and the ngram parser of MDEV-10267, the innodb_fts, maria and plugins suites and the main fulltext, plugin and alter tests pass.

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the latest MariaDB development branch.
  • This is a bug fix and the PR is based against the earliest maintained branch in which the bug can be reproduced. (Jira also lists 10.11, whose first GA release is more than three years old. For the Fix Version 11.8, see the note above.)

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

🤖 Generated with Claude Code

@futureweb
futureweb marked this pull request as ready for review October 24, 2026 18:48
@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Oct 26, 2026
@gkodinov gkodinov self-assigned this Oct 26, 2026

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! This is a preliminary review.

The change fulfills the formal preliminary review criteria, hence I'm approving it.

Stay tuned for the final review. One clarification question below.

--source include/have_sequence.inc
--source include/not_embedded.inc

# MDEV-37370 fts_phrase_or_proximity_search() is quadratic instead of linear

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU, it seems that you have two fixes:

  1. the performance improvement
  2. the fix for out of order IDs indexing and update of the memory cache around these.

Correct me if I'm wrong, but I do not think the two are related and can be tested in isolation.

Your tests here seem to be testing the latter.

I guess proper testing for the former claim would also be needed. In your description you mention some performance results it seems. Please share (preferably in the jira itself) how can one go about testing the performance: setup, driver etc. Preferably with the old parser so that we don't compare apples to oranges. This will be needed by our testing team.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're related in one direction. For each candidate document from the first match list, the old scan went through the other lists from their start, and with a list that isn't sorted by document ID it could already miss documents – that's the out-of-order part of the test, which fails on 11.4 without the patch. The cursors never go back, so with unsorted lists they would miss more; sorting first is what makes them safe. So the sort fixes wrong results on its own, and the cursors depend on it. If you prefer, I'll split it into two commits – the sort first, then the cursors – or file the wrong results as a separate MDEV.

And yes, the test checks results only – I've reworded its header to say so. The out-of-order part needs the sort, and the patterns before it (gaps between document IDs, repeated words, lists that end early, data in the cache and on disk) check that the cursors don't skip matches. For the performance, I've put a benchmark with the built-in parser into MDEV-37370: the setup, the queries and the times on 11.4 with and without the patch.

@gkodinov gkodinov assigned Thirunarayanan and unassigned gkodinov Oct 26, 2026
@futureweb
futureweb force-pushed the MDEV-37370-fts-phrase-linear branch from b50e9c9 to e3cb4b5 Compare October 26, 2026 17:30
@gkodinov

gkodinov commented Nov 3, 2026

Copy link
Copy Markdown
Member

FYI: According to our development cycle we work on bugs In the following periods 15 Mar-30 Apr, 15 Jun-30 Jul, 15 Sep-30 Oct and 15 Dec-31 Jan. So, please, expect to get a review somewhere between these two dates and the goal is to have your PR merged before the second date

@Thirunarayanan Thirunarayanan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK to push after addressing the comment

GROUP BY pattern ORDER BY pattern;

SELECT pattern, COUNT(*) AS matches FROM t1
WHERE MATCH(body) AGAINST ('"alpha absent"' IN BOOLEAN MODE)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it hits the changed code at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right: when a word of the phrase has no match, fts_query_phrase_search() returns before it calls fts_phrase_or_proximity_search(). I removed that query. To check the others, I ran the test with a debug print in fts_phrase_or_proximity_search(): every remaining query of this test case reaches it, and the two out-of-order cases also sort (the first match list in the first case, the second one in the second).

@@ -0,0 +1,147 @@
--source include/have_innodb.inc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the test case in existing test ? (innodb_fts_proximity.test in innodb_fts suite)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: the test case is now at the end of innodb_fts_proximity.test, and phrase_proximity_order is removed. Since innodb_fts_proximity also runs with system versioning, which doesn't support TRUNCATE, the second out-of-order case now drops and recreates the table. I also rebased onto the current 11.4.

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requesting changes to follow up on the last request

…inear

Retain a cursor for each word instead of restarting its match-list
scan for every document. Matching ordered lists is linear in their
lengths for a fixed number of query tokens.

Cache nodes and overlapping disk posting lists can produce unsorted
match lists, where the old scan could miss documents. Check each list
and sort it by document ID only when needed before using the cursors.

Keep ib_vector_t, phrase filtering and proximity verification.
Cover gaps, repeated words, list exhaustion, disk/cache data and
out-of-order commits with unsorted first and second match lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@futureweb
futureweb force-pushed the MDEV-37370-fts-phrase-linear branch from e3cb4b5 to bb3f3c2 Compare November 26, 2026 13:53
@futureweb
futureweb requested a review from gkodinov November 26, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

3 participants