MySQL Index Overview
- Index is an ordered structure of the database will be the value of the single or multiple columns
- Query data through the index can improve query speed, but also reduce the load on the server
MySQL Index Classification
- Ordinary index ( without applying any constraints index, can be created in any data type )
- Unique index ( value of the index must be unique )
- Full-text index ( created only in char , VARCHAR or text on the types of fields )
- Single index ( only one field corresponding to the index )
- Multi-column index ( create an index on multiple fields of the table )
- Space Index ( can only be established on the spatial data types )
Creating an index
语法:
create table table_name
(
属性名 数据类型[约束条件],
属性名 数据类型[约束条件],
......
[unique|fulltext|spatial] index }key[别名](属性名[ASC|DESC])
);
Create a regular index
#普通索引
create table score
(
id int auto_increment primary key not null,
name varchar(30) not null,
math int not null,
english int not null,
chinese int not null,
index(id)
);
show create table score;
Create a unique index
#唯一索引
create table t_address
(
id int auto_increment primary key not null,
name varchar(30),
address varchar(200),
unique index UQ_address(id asc)
);
show create table t_address
Create a full-text index
#全文索引
create table cards
(id int auto_increment primary key not null,
name varchar(30),
number int,
info varchar(50),
fulltext key ftk_number(number)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;
MySql comes with the full-text index can only be used for the database engine MYISAM data sheet, if other data engine, the full-text index will not take effect. In addition, MySql comes with full-text indexing full-text search can only be conducted in English, Chinese is currently unable to perform full-text search. If you need to text data included Chinese, including full-text search, we need to use Sphinx (Sphinx) / Coreseek technology to deal with Chinese.
Currently, when using MySql comes with the full-text index, if the query string is too short you will not get the desired search results. MySql full-text indexing can find the default minimum word length is 4 characters. In addition, if the query string contains stop words, then the stop words will be ignored.
If possible, try to create tables and insert all the data and then create a full-text index, but do not directly create full-text index When you create a table, because the former is higher than the latter's full-text indexing efficiency.
Create a separate index
#单列索引
create table telephone
(
id int auto_increment primary KEY not null,
name varchar(30),
tel varchar(30),
index idx_tel(tel(20))
);
Create multi-column index
#多列索引
create table information
(
id int primary key auto_increment not null,
name varchar(30) not null,
sex varchar(30) not null,
birthday VARCHAR(30) not null,
index idx_name_sex(name,sex)
);
Create a spatial index
#空间索引
create table list
(
id int PRIMARY key auto_increment not null,
goods geometry not null,
spatial index spt(goods)
)engine=myisam;
Open data table to create an index
语法:
create [unique|fulltext|spatial] index index_name
on table_name(属性[(length)][ASC|DESC]);
#在已有的表上创建索引
create index idx_id on t1(id)
Add index structure modification data table
语法:
alter table table_name add[unique|fulltext|spatial] index index_name(属性名[(length)][ASC|DESC]);
#添加索引
alter table t1
add unique index idx_name(name(10))
Delete Index
语法:
drop index index_name on table_name
#删除索引
drop index idx_name on t1