Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between FULLTEXT and FULLTEXT KEY/INDEX?

MySQL documentations gives this format for creating a FULLTEXT index:

| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...) [index_option]

To implement full-text search with MySQL you can either create index by writing :

CREATE TABLE posts (
  id int(4) NOT NULL AUTO_INCREMENT,
  title varchar(255) NOT NULL,
  content text,
  PRIMARY KEY (id),
  FULLTEXT (title, content)
  ) ENGINE=MyISAM;

OR

CREATE TABLE posts (
  id int(4) NOT NULL AUTO_INCREMENT,
  title varchar(255) NOT NULL,
  content text,
  PRIMARY KEY (id),
  FULLTEXT KEY my_index_name (title, content)
  ) ENGINE=MyISAM;

with my_index_name a user defined name and not a field name.

  • What is the difference between them?
  • Is there any consequence for the system itself and for the developer ?

I am not able to find any clue in the docs:

  • http://dev.mysql.com/doc/refman/5.0/en/fulltext-natural-language.html
  • http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
like image 359
simo Avatar asked Mar 10 '15 11:03

simo


People also ask

What is a fulltext key?

FULLTEXT is the index type of full-text index in MySQL. InnoDB or MyISAM tables use Full-text indexes. Full-text indexes can be created only for VARCHAR, CHAR or TEXT columns. A FULLTEXT index definition can be given in the CREATE TABLE statement or can be added later using ALTER TABLE or CREATE INDEX.

What is the use of fulltext index?

A full-text index is a special type of index that provides index access for full-text queries against character or binary column data. A full-text index breaks the column into tokens and these tokens make up the index data.

What is fulltext index in SQL Server?

A fulltext index uses internal tables called full-text index fragments to store the inverted index data. This view can be used to query the metadata about these fragments. This view contains a row for each full-text index fragment in every table that contains a full-text index.

What is the difference between unique index and index?

Index: It is a schema object which is used to provide improved performance in the retrieval of rows from a table. Unique Index: Unique indexes guarantee that no two rows of a table have duplicate values in the key column (or columns).

What is a full-text index?

A full-text index is a special type of index that provides index access for full-text queries against character or binary column data. A full-text index breaks the column into tokens and these tokens make up the index data. Before you can create a full-text index you must create a FULL TEXT CATALOG...

How do you create a full text index in SQL?

One requirement for creating full-text indexes is that the table has a unique key defined on it. This option associates the unique key with the full-text index . In order to get the best performance this unique key column should be an integer, usually it's the primary key.

Is it possible to perform full-text search without indexes?

However, without indexes defined on the columns, the query engine must perform a full table scan to locate the necessary data. But there is a solution-full-text search. Full-text search refers to the functionality in SQL Server that supports full-text queries against character-based data.

What are the columns in the productdocs full-text index?

In the ProductDocs table, the two indexed columns are DocSummary (the fifth column defined) and DocContent (the sixth column defined). The following table shows the first 25 terms stored in the ProductDocs full-text index.


2 Answers

There is no difference, if you use the first syntax then mysql will choose the name of the index, and in most cases the column name, since you have 2 FULLTEXT (title, content) it most likely to choose the index name as title

And in the 2nd case you are explicitly providing a name.

You can run the command show create table posts after creating the tables and see what index names are for both cases.

like image 69
Abhik Chakraborty Avatar answered Oct 01 '22 23:10

Abhik Chakraborty


Omitting the index name

If you add an index/key to a table MySQL will store additional information on the specified column (set of columns) to speed up searches.

In your first example MySQL will generate an index and name it my_index_name. If you omit the name, MySQL will choose one for you. I could not find documentation on how the name is chosen but in my experience the name of the first column is usually reused as index name.

For this part of the discussion the fulltext option is irrelevant. It just defines the type of index/key you are creating:

You can create special FULLTEXT indexes, which are used for full-text searches.

Omitting the index/key keyword

It is sufficient to just give fulltext:

| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ...

but it is probably a good idea to use the fulltext index form, since the reader is reminded of an index.

index and key are synonyms

Note that there is no difference between index and key (see create table):

KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition. This was implemented for compatibility with other database systems.

Example

use test;

create table u (
    id int primary key,
    a varchar(10),
    b varchar(10),
    fulltext index (a, b)
);

show index from u;

will print (I have omitted the primary index from this output as well as some additional columns):

table key_name  seq_in_index  column_name
u     a         1             a
u     a         2             b

You need the index name for example if you want to delete it:

alter table u
  drop index a;

References

  • You can find the relevant documentation at create index and create table.
  • Some more information are available at : http://dev.mysql.com/doc/refman/5.0/en/index-hints.html
like image 44
Micha Wiedenmann Avatar answered Oct 01 '22 22:10

Micha Wiedenmann