Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do FULLTEXT INDEXES on multiple columns work?

Tags:

When adding a FULLTEXT INDEX on 3 columns, does that add 1 single index on 3 columns, or does it add 3 separate indexes?

I ask this, because at the moment I'm using FULLTEXT like this:

ALTER TABLE myTable ADD FULLTEXT all3colsIndex (col1,col2,col3);
SELECT * FROM myTable WHERE MATCH (col1, col2, col3) AGAINST ('word');

I have just added a user option to my search interface where the user can remove one of the columns from the search. So am I able to do this without losing the index, where I'm only using 2 of the 3 columns:

ALTER TABLE myTable ADD FULLTEXT all3colsIndex (col1,col2,col3);
If (UserOptionRemoveCol == "selected") {
    SELECT * FROM myTable WHERE MATCH (col1, col2) AGAINST ('word');
} else {
    SELECT * FROM myTable WHERE MATCH (col1, col2, col3) AGAINST ('word');
}

Or would I have to create a new FULLTEXT index on the two columns, as well as the three?

ALTER TABLE myTable ADD FULLTEXT all3colsIndex (col1,col2,col3);
ALTER TABLE myTable ADD FULLTEXT 2colsIndex (col1,col2);
like image 288
TheCarver Avatar asked Sep 11 '12 02:09

TheCarver


People also ask

How does indexing on multiple columns work?

When the multicolumn index is accessed, the main portion of the index (the index on the first column) is accessed first. Each entry in the main index has a reference to the row's location in the main table. The main index also has a pointer to the secondary index where the related make is stored.

Can multi column fulltext indexes be used if so when?

The columns in the fulltext index can be placed in any sort of order, but all the columns as specified when creating the fulltext index must be referenced in the match() query.

Can indexing be done on multiple columns?

MySQL can use multiple-column indexes for queries that test all the columns in the index, or queries that test just the first column, the first two columns, the first three columns, and so on.

What will happen if you apply index on multiple column?

An index with more than one column aggregates the contents.


2 Answers

Glancing over the manual for CREATE FULLTEXT INDEX, it indicates that you are able to specify multiple columns by repeating the column_name as such:

CREATE FULLTEXT INDEX ON table_name (column_name1 [...], column_name2 [...]) ...

Given this information, I would assume it creates a single index across 3 columns. Further, I'm assuming that it works under the left-to-right rule with regards to composite indexes (I would verify this by checking the execution plan for the following statements). Therefore, a composite index on (col1, col2, col3) would have to be selected in that order for it to be used (SELECT col1, col2 ...). If you were to call col2 it would not use the index.

like image 60
Kermit Avatar answered Nov 13 '22 19:11

Kermit


Only one index, of type FULLTEXT, will be created. That index will "span" around multiple columns if necessary. However, the indexes themselves are not indexing the columns as much as their words. From the documentation:

InnoDB FULLTEXT indexes have an inverted index design. Inverted indexes store a list of words, and for each word, a list of documents that the word appears in. [...]

If you create one FULLTEXT grouping three columns, or three FULLTEXT grouping only one, these columns will in effect consist of entries in the FULLTEXT meta-data tables.

When incoming documents are tokenized, the individual words (also referred to as “tokens”) are inserted into the index tables along with position information and the associated Document ID (DOC_ID).

Basically, it seems multi-column FULLTEXT vs multiple single-column FULLTEXT indexes work the same, as the storage of the words is abstracted from the original separation of the columns.

like image 33
Félix Adriyel Gagnon-Grenier Avatar answered Nov 13 '22 18:11

Félix Adriyel Gagnon-Grenier