MySQL index species

normal: Specifies the general index

unique: represents a unique, does not allow duplicate index, if the field is to ensure that information is not repeated, for example ID number as an index, it can be set to unique

full textl: indicates the index of full-text search. FULLTEXT searching for a long time an article, the best results. Used in a relatively short text, if it is a line or two of ordinary INDEX can.

 

PRIMARY, INDEX, UNIQUE This is a class
PRIMARY primary key. It is unique and can not be empty.
INDEX index, ordinary
UNIQUE unique index. It does not allow duplicate.
FULLTEXT is a full-text index for an article, retrieve textual information.

 

1, the general index

The most basic index, it does not have any restrictions for accelerating queries.

Creation Method:

a. Create a table with built when

CREATE TABLE mytable ( name VARCHAR(32) , INDEX index_mytable_name (name) );

B. After the construction of the table, create an index directly

CREATE INDEX index_mytable_name ON mytable(name);

c. Modify the table structure

ALTER TABLE mytable ADD INDEX index_mytable_name (name);

Note: If a string field, can specify the length of the index, followed by the index in column command can be a length (e.g.: name (11))

2, a unique index

The value of the index columns must be unique, but allow free value. If it is a combination of the index, the column value must be unique.

Creation Method:

a. Create a table with built when

CREATE TABLE mytable ( `name` VARCHAR(32) , UNIQUE index_unique_mytable_name (`name`) );

B. After the construction of the table, create an index directly

CREATE UNIQUE INDEX index_mytable_name ON mytable(name);

c. Modify the table structure

ALTER TABLE mytable ADD UNIQUE INDEX index_mytable_name (name);

Note: If a string field, can specify the length of the index, followed by the index in column command can be a length (e.g.: name (11))

3, the primary key index

It is a special unique index, a table can have only one primary key, does not allow nulls. In general it is time to build the table at the same time create a primary key index.

Creation Method:

a. Create a table with built when

CREATE TABLE mytable ( `id` int(11) NOT NULL AUTO_INCREMENT , `name` VARCHAR(32) , PRIMARY KEY (`id`) );

b. Modify the table structure

ALTER TABLE test.t1 ADD CONSTRAINT t1_pk PRIMARY KEY (id);

Note: If a string field, can specify the length of the index, followed by the index in column command can be a length (e.g.: name (11))

4, the combination index

It refers to the index created on multiple fields, only use the first field when creating an index in a query, the index will be used. The most left-prefix follow the set when using a composite index.

Creation Method:

a. Create a table with built when

CREATE TABLE mytable ( `id` int(11) , `name` VARCHAR(32) , INDEX index_mytable_id_name (`id`,`name`) );

B. After the construction of the table, create an index directly

CREATE INDEX index_mytable_id_name ON mytable(id,name);

c. Modify the table structure

ALTER TABLE mytable ADD INDEX index_mytable_id_name (id,name);

5, full-text indexing

主要用来查找文本中的关键字,而不是直接与索引中的值相比较。

fulltext索引跟其它索引大不相同,它更像是一个搜索引擎,而不是简单的where语句的参数匹配。

fulltext索引配合match against操作使用,而不是一般的where语句加like。

它可以在create table,alter table ,create index使用,不过目前只有char、varchar,text 列上可以创建全文索引。

创建方法:

a. 建表的时候一起创建

CREATE TABLE `article` ( `id` int(11) NOT NULL AUTO_INCREMENT ,`title` char(250) NOT NULL , `contents` text NULL , `create_at` int(10) NULL DEFAULT NULL , PRIMARY KEY (`id`), FULLTEXT (contents) );

b. 建表后,直接创建索引

CREATE FULLTEXT INDEX index_article_contents ON article(contents);

c. 修改表结构

ALTER TABLE article ADD FULLTEXT INDEX index_article_contents (contents);

总结

虽然索引可以增加查询数据,但对于更新、创建或者删除的时候,需要去维护索引,导致性能会受影响,因此,索引也不能建立太多。

Guess you like

Origin www.cnblogs.com/Rivend/p/12099972.html