简体   繁体   中英

When should I use FULLTEXT index?

I have a table like this:

// qanda
+----+---------+-----------------------+------------------------+
| id |  title  |        content        |          tags          |
+----+---------+-----------------------+------------------------+
| 1  | title1  | content1              | <a>tagA<a/><a>tagB</a> |
| 2  | title2  | content2              | <a>tagA</a><a>tagC</a> |
| 3  | title3  | content3              | <a>tagM</a><a>tagB</a> |
| 4  | title4  | content4              | <a>tagD</a>            |
| 5  | title5  | content5              | <a>tagA</a><a>tagG</a> |
+----+---------+-----------------------+------------------------+

Now I'm trying to search into tags column. So I want this output:

+----+---------+-----------------------+------------------------+
| 1  | title1  | content1              | <a>tagA<a/><a>tagB</a> |
| 3  | title3  | content3              | <a>tagM</a><a>tagB</a> |
+----+---------+-----------------------+------------------------+

Ok well, Which one is better?

. . . WHERE tags LIKE '%>tagB<%'                -- regular index

. . . WHERE MATCH ( tags ) AGAINST ( '>tagB<')   -- fulltext index

Note: Sometimes I need to search for multiple tags. EX: I want to select both tagB and tagD , So this is expected output:

+----+---------+-----------------------+------------------------+
| 1  | title1  | content1              | <a>tagA<a/><a>tagB</a> |
| 3  | title3  | content3              | <a>tagM</a><a>tagB</a> |
| 4  | title4  | content4              | <a>tagD</a>            |
+----+---------+-----------------------+------------------------+
. . . WHERE tags LIKE '%>tagB<%'                -- regular index

This wouldn't use an index set on the column because your term starts with a wildcard.

. . . WHERE MATCH ( tags ) AGAINST ( '>tagB<')   -- fulltext index

Bette to do it like this:

. . . WHERE MATCH ( tags ) AGAINST ( 'tagB')   -- fulltext index

As the fulltext index on buildup removes special chars and stopwords.

Also when using the fulltext index there is the minimum wordlength to consider, though there are some options you can configure http://dev.mysql.com/doc/refman/5.7/en/fulltext-fine-tuning.html

Answering the question, which one is better: I would definitely try the fulltext index, as you are not able to use the regular index when starting the search with a wildcard.

A better way of saving the data would be to normalize your tables, so put the tags in another table each in it's own field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM