12

I am trying to add a fulltext index onto an existing table in MySQL. I can successfully add a single column fulltext index on this table, however if I try and add a multicolumn index it fails. I am wanting to add a multicolumn fulltext index to enable searching across multiple columns in a SELECT query.

Schema below:

CREATE TABLE `emailarchive_people` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `emailAddress` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `person` (`name`,`emailAddress`)
) ENGINE=InnoDB AUTO_INCREMENT=100230 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

The following single index creation query works:

ALTER TABLE
    `emailarchive_people`
ADD
    FULLTEXT INDEX `name` (`name`);

However the following query:

ALTER TABLE
    `emailarchive_people`
ADD
    FULLTEXT INDEX `personFT` (`name`,`emailAddress`);

...fails with the following response:

Error Code: 1062. Duplicate entry 'NULL-NULL' for key 'emailarchive_people.personFT'

I have also tried dropping the unique person key before attempting to add the personFT index, but I get the same error.

5 Answers 5

7

This is a bug in MySQL: https://bugs.mysql.com/bug.php?id=109242

Likely since you are using FULLTEXT INDEX that this is the case.

5
  • Oh wow, so basically it is run it until it works? That worked for me if it helps anyone.
    – hectorviov
    Apr 1, 2023 at 0:57
  • I encountered this kind of issue on MySQL 8.0.23 with 160k records when I created a full text index with multiple column. Apr 19, 2023 at 17:07
  • @hectorviov perhaps, it may be that you have so many rows it will never succeed. It seems that more rows = more chance to encounter this issue. May 3, 2023 at 10:50
  • It seems the bug is now fixed and will be part of 8.2.0. Not sure why they are not putting it in 8.0.34.
    – Sarang
    Oct 15, 2023 at 20:09
  • Bug has been fixed in release 8.0.35
    – zuc0001
    Nov 1, 2023 at 9:45
1

As a workaround, you can create a new table with the same definition, create a multi-column full-text search index on this empty table, then copy all the data from the existing table and switch them.

CREATE TABLE `emailarchive_people_new` LIKE `emailarchive_people`;
ALTER TABLE `emailarchive_people_new` 
    ADD FULLTEXT INDEX `personFT` (`name`,`emailAddress`);
INSERT INTO `emailarchive_people_new` SELECT * FROM `emailarchive_people`;
RENAME TABLE `emailarchive_people` TO `emailarchive_people_old`, 
             `emailarchive_people_new` TO `emailarchive_people`;

NOTE: It can cause some downtime and new data added during switching can be lost. Make sure you have enough disk storage for the table copy and for indexes.

0

I am getting exactly the same error when applying a multi fulltext index.

It worked with only a few data rows. With 10,000+ it fails.

Server version: 10.5.15-MariaDB-0+deb11u1 Debian 11

ALTER TABLE `customer`
ADD FULLTEXT INDEX `search` (`firstname`, `lastname`, `postcode`, `email`);

/* SQL Fehler (1062): Duplicate entry 'NULL' for key 'customer.key_paydate' */

The error message makes no sense as customer.key_paydate is not an unique key and no entry of that column is NULL

0

It happened to me too.

An exception occurred while executing 'CREATE FULLTEXT INDEX search_order_address ON shipping_order (pickup_address, delivery_address) WITH PARSER ngram':

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'shipping_order.search_order_address'

I searched and there was a bug report, docs with fulltext optimizations and some blog entry with this bug (this was probably what worked for me).

So what I tried was OPTIMIZE TABLE (not sure if this is required but just in case)

set GLOBAL innodb_optimize_fulltext_only=ON;
OPTIMIZE TABLE shipping_order;

and just after this

ALTER TABLE shipping_order ENGINE=INNODB;

Next time I run this

CREATE FULLTEXT INDEX search_order_address ON shipping_order (pickup_address, delivery_address) WITH PARSER ngram;

it worked.

1
  • Thanks for the suggestion, unfortunately these steps didn't work for me. Looks like I'm stuck with this problem until the MySQL folks fix the bug per answer from @notoriouspyro. Jan 25, 2023 at 22:48
0

In my case I have a table with 2.5 mln records and what worked for me is setting innodb-ddl-buffer-size=64M variable in the server conf file and restarting the server. It seems like this variable is dynamic, so you cna also change it during the runtime with

SET GLOBAL innodb_ddl_buffer_size=67108864;

After that I could add a FULLTEXT INDEX on several columns without any NULL value errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.