From 7f7d78bc18d0e2c6f26c607a3d17b77c666fb41f Mon Sep 17 00:00:00 2001 From: Thirunarayanan Balathandayuthapani Date: Mon, 21 Oct 2024 10:31:24 +0530 Subject: [PATCH] MDEV-35183 ADD FULLTEXT INDEX unnecessarily DROPS FTS COMMON TABLES MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - InnoDB fulltext rebuilds the FTS COMMON table while adding the new fulltext index. This can be optimized by avoiding rebuilding the FTS COMMON table in case of FTS COMMON TABLE already exists. Reviewed-by: Marko Mäkelä --- mysql-test/suite/innodb_fts/r/fulltext.result | 25 +++++++++++++++++++ mysql-test/suite/innodb_fts/t/fulltext.test | 16 ++++++++++++ storage/innobase/handler/handler0alter.cc | 13 ++++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/mysql-test/suite/innodb_fts/r/fulltext.result b/mysql-test/suite/innodb_fts/r/fulltext.result index 2de08657a6e..54725f489fa 100644 --- a/mysql-test/suite/innodb_fts/r/fulltext.result +++ b/mysql-test/suite/innodb_fts/r/fulltext.result @@ -784,3 +784,28 @@ f1 MATCH(f1) AGAINST ("test" IN BOOLEAN MODE) test 0.000000001885928302414186 DROP TABLE t1; # End of 10.3 tests +# +# MDEV-35183 ADD FULLTEXT INDEX unnecessarily DROPS FTS +# COMMON TABLES +# +CREATE TABLE t1 ( +ID INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, +title VARCHAR(200), book VARCHAR(200), +FULLTEXT fidx(title)) ENGINE = InnoDB; +INSERT INTO t1(title) VALUES('database'); +ALTER TABLE t1 DROP INDEX fidx; +select space into @common_space from information_schema.innodb_sys_tables where name like "test/FTS_%_CONFIG"; +ALTER TABLE t1 ADD FULLTEXT fidx_1(book); +select space=@common_space from information_schema.innodb_sys_tables where name like "test/FTS_%_CONFIG"; +space=@common_space +1 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `ID` int(10) unsigned NOT NULL AUTO_INCREMENT, + `title` varchar(200) DEFAULT NULL, + `book` varchar(200) DEFAULT NULL, + PRIMARY KEY (`ID`), + FULLTEXT KEY `fidx_1` (`book`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci +DROP TABLE t1; diff --git a/mysql-test/suite/innodb_fts/t/fulltext.test b/mysql-test/suite/innodb_fts/t/fulltext.test index 3ddc1856b7f..02a9f32dbed 100644 --- a/mysql-test/suite/innodb_fts/t/fulltext.test +++ b/mysql-test/suite/innodb_fts/t/fulltext.test @@ -803,3 +803,19 @@ SELECT f1, MATCH(f1) AGAINST ("test" IN BOOLEAN MODE) FROM t1; DROP TABLE t1; --echo # End of 10.3 tests + +--echo # +--echo # MDEV-35183 ADD FULLTEXT INDEX unnecessarily DROPS FTS +--echo # COMMON TABLES +--echo # +CREATE TABLE t1 ( + ID INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, + title VARCHAR(200), book VARCHAR(200), + FULLTEXT fidx(title)) ENGINE = InnoDB; +INSERT INTO t1(title) VALUES('database'); +ALTER TABLE t1 DROP INDEX fidx; +select space into @common_space from information_schema.innodb_sys_tables where name like "test/FTS_%_CONFIG"; +ALTER TABLE t1 ADD FULLTEXT fidx_1(book); +select space=@common_space from information_schema.innodb_sys_tables where name like "test/FTS_%_CONFIG"; +SHOW CREATE TABLE t1; +DROP TABLE t1; diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc index 936333ecba5..d30c88414cd 100644 --- a/storage/innobase/handler/handler0alter.cc +++ b/storage/innobase/handler/handler0alter.cc @@ -6293,6 +6293,7 @@ prepare_inplace_alter_table_dict( dberr_t error; ulint num_fts_index; dict_add_v_col_t* add_v = NULL; + bool fts_instance_created = false; ha_innobase_inplace_ctx*ctx; DBUG_ENTER("prepare_inplace_alter_table_dict"); @@ -6539,6 +6540,7 @@ new_clustered_failed: new_table_name, NULL, n_cols + n_v_cols, n_v_cols, flags, flags2); + fts_instance_created = (ctx->new_table->fts != nullptr); /* The rebuilt indexed_table will use the renamed column names. */ ctx->col_names = NULL; @@ -6732,6 +6734,7 @@ wrong_column_name: ctx->new_table->fts = fts_create( ctx->new_table); ctx->new_table->fts->doc_col = fts_doc_id_col; + fts_instance_created = true; } /* Check if we need to update mtypes of legacy GIS columns. @@ -7228,8 +7231,11 @@ op_ok: ctx->trx->commit(); trx_start_for_ddl(ctx->trx, op); - if (!ctx->new_table->fts - || ib_vector_size(ctx->new_table->fts->indexes) == 0) { + /* If table rebuild happens or fulltext index are + being added when common tables doesn't exist then + do create new fulltext common tables else use the + existing fulltext common tables */ + if (fts_instance_created) { error = fts_create_common_tables( ctx->trx, ctx->new_table, true); @@ -7240,6 +7246,9 @@ op_ok: if (error != DB_SUCCESS) { goto error_handling; } + } + + if (!ib_vector_size(ctx->new_table->fts->indexes)) { ctx->new_table->fts->dict_locked = true;