Bug #121863 InnoDB FULLTEXT: OPTIMIZE TABLE with innodb_optimize_fulltext_only deletes the postings of accent-variant spellings

Submitted: 22 Nov 22:47
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: FULLTEXT search Severity:S2 (Serious)
Version:26.10.0 (trunk 06bcb0eb0ad), 8.4.13 OS:Any
Assigned to: CPU Architecture:Any

[22 Nov 22:47] Ke Han
Description:
An InnoDB FULLTEXT index on a column with an accent-insensitive collation
(the default utf8mb4_0900_ai_ci, also utf8mb4_general_ci) can hold
different spellings of a word that are equal under the collation, for
example 'déjà' and 'deja', or 'naïve' and 'naive', written by different
documents. The manual's procedure for optimizing a FULLTEXT index,

  SET GLOBAL innodb_optimize_fulltext_only = ON;
  OPTIMIZE TABLE t;

then deletes the index entries of all but one of these spellings when the
table has deleted document ids to purge (here one deleted row). Afterwards
the documents with the other spellings are not found by any MATCH ... AGAINST
search (boolean or natural language mode, with either spelling), although
LIKE finds them. OPTIMIZE TABLE and CHECK TABLE ... EXTENDED report OK, there
is no warning, and INFORMATION_SCHEMA.INNODB_FT_INDEX_TABLE shows that the
rows for 'déjà' and 'naïve' are gone from the index tables. The loss stays
until the table is rebuilt. CREATE TABLE ... SELECT ... WHERE MATCH and
DELETE ... WHERE MATCH miss the documents (below the DELETE removes one of
the two documents that contain 'naive').

The index rows are deleted, not hidden: FLUSH TABLES does not bring the
documents back. With an accent-sensitive collation (utf8mb4_0900_as_ci)
the spellings are different words and nothing is lost, and case-only
variants are not affected (tokens are lower-cased).

How to repeat:
-- run with: mysql -uroot --table < file
SET NAMES utf8mb4;
CREATE DATABASE m210;
USE m210;
-- default collation utf8mb4_0900_ai_ci, where 'deja' = 'déjà' and 'naive' = 'naïve'
CREATE TABLE articles (id INT PRIMARY KEY, body TEXT, FULLTEXT KEY ft (body));
INSERT INTO articles VALUES (1, 'Déjà vu in the Hauptstraße'), (2, 'A naïve approach'), (3, 'draft to be deleted');
SET GLOBAL innodb_optimize_fulltext_only = ON;
OPTIMIZE TABLE articles;   -- writes documents 1-3 to the index tables
INSERT INTO articles VALUES (4, 'deja vu again on the strasse'), (5, 'naive but honest');
DELETE FROM articles WHERE id = 3;
SELECT id FROM articles WHERE MATCH(body) AGAINST ('deja' IN BOOLEAN MODE) ORDER BY id;
SELECT id FROM articles WHERE MATCH(body) AGAINST ('naive' IN BOOLEAN MODE) ORDER BY id;

OPTIMIZE TABLE articles;   -- routine FULLTEXT maintenance
SET GLOBAL innodb_optimize_fulltext_only = OFF;
SELECT id FROM articles WHERE MATCH(body) AGAINST ('deja' IN BOOLEAN MODE) ORDER BY id;
SELECT id FROM articles WHERE MATCH(body) AGAINST ('déjà' IN BOOLEAN MODE) ORDER BY id;
SELECT id FROM articles WHERE MATCH(body) AGAINST ('naive') ORDER BY id;
SELECT id FROM articles WHERE body LIKE '%deja%' OR body LIKE '%naive%' ORDER BY id;
CHECK TABLE articles EXTENDED;
SET GLOBAL innodb_ft_aux_table = 'm210/articles';
SELECT WORD, DOC_ID FROM information_schema.INNODB_FT_INDEX_TABLE ORDER BY WORD, DOC_ID;

CREATE TABLE deja_docs AS SELECT id FROM articles WHERE MATCH(body) AGAINST ('+deja' IN BOOLEAN MODE);
SELECT * FROM deja_docs;
DELETE FROM articles WHERE MATCH(body) AGAINST ('+naive' IN BOOLEAN MODE);
SELECT id, body FROM articles WHERE body LIKE '%naive%';

ALTER TABLE articles ENGINE=InnoDB;   -- a rebuild re-indexes the rows
SELECT id FROM articles WHERE MATCH(body) AGAINST ('deja' IN BOOLEAN MODE) ORDER BY id;

Result on 26.10.0 (8.4.13 gives the same):
  before the second OPTIMIZE:  'deja' -> 1, 4      'naive' -> 2, 5
  OPTIMIZE TABLE articles:     status OK
  after it:
    'deja'  (boolean mode)        -> 4             <- document 1 missing
    'déjà'  (boolean mode)        -> 4             <- document 1 missing
    'naive' (natural language)    -> 5             <- document 2 missing
    LIKE '%deja%' OR '%naive%'    -> 1, 2, 4, 5
    CHECK TABLE articles EXTENDED -> status OK
    INNODB_FT_INDEX_TABLE: again 6, approach 3, but 7, deja 6, hauptstraße 2,
                           honest 7, naive 7, strasse 6
                           (no rows for 'déjà' (doc id 2) and 'naïve' (doc id 3))
    deja_docs                     -> 4             <- 1, 4 expected
    after DELETE ... MATCH('+naive'): row 2 'A naïve approach' is still there
  after ALTER TABLE articles ENGINE=InnoDB: 'deja' -> 1, 4

Expected: the searches return the same documents after OPTIMIZE TABLE as
before it (1, 4 and 2, 5), as they do after the rebuild; the manual says
OPTIMIZE TABLE on a full-text index removes deleted document ids and
consolidates "multiple entries for the same word, where possible". It should
not remove the entries of live documents.

Suggested fix:
storage/innobase/fts/fts0opt.cc: fts_optimize_table() compacts the indexes
only when there are deleted document ids to purge:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/storag...
The words to optimize are read from the auxiliary index tables and grouped
by fts_optimize_index_fetch_node(), which starts a new word whenever the
bytes differ, so 'naïve' and 'naive' are two words, each with its own nodes:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/storag...
fts_optimize_compact() then rewrites word by word, and
fts_optimize_write_word() first runs
  DELETE FROM $table_name WHERE word = :word
and inserts the purged nodes of that spelling only:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/storag...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/storag...
The DELETE compares word in the collation of the indexed column, so it also
deletes the rows of the other spellings that are equal under the collation.
The spellings are processed one after the other, and the last one processed
is the only one that keeps its rows. Grouping the words with the same
collation-aware comparison that the DELETE uses (merging the nodes of all
spellings, as the FTS cache does), or deleting the old rows by the exact
bytes, would fix it.

fts_optimize_table() is also called by the FTS background thread when a
table's cache has counted FTS_OPTIMIZE_THRESHOLD (10,000,000) deleted
documents (fts_optimize_table_bk()), so the same loss is expected there
without the variable; I have not run that case:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/storag...

Workaround: rebuild the index (ALTER TABLE ... ENGINE=InnoDB, or drop and
re-create the FULLTEXT index) instead of OPTIMIZE TABLE with
innodb_optimize_fulltext_only = ON; after a loss, a rebuild restores the
entries.

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

Page generated in 0.040 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.