The type of index
There are four types of mysql indexes: 主键索引
, 唯一索引
, 普通索引
and 全文索引
. By giving fields , the concurrency and pressure resistance of the project 添加索引
can be improved. An optimization method in mysql. The role of the index is equivalent to the number of pages in the table of contents .提高数据的读取速度
索引优化
图书的目录
快速找到所需的内容
Primary key index:
The primary key is a unique index, but it must be specified PRIMARY KEY
so that each table can have only one primary key.
alert table tablename add primary key (`字段名`)
Unique index:
All values of the indexed column can only appear once, i.e. must 唯一
, the value can be 空
.
alter table table_name add primary key (`字段名`);
Ordinary index: The index type, the value can be empty, there is no uniqueness restriction.
基本
alter table table_name add index (`字段名`);
Full-text index:
The index type of full-text index is FULLTEXT
. Full-text indexes can be created varchar、char、text
on columns of type. Can be created via ALTER TABLE
or CREATE INDEX command. For large datasets, creating a full-text index via the ALTER TABLE (or CREATE INDEX) command is faster than inserting records into an empty table with a full-text index. MyISAM
Full-text indexing is supported, InnoDB
which is supported after mysql5.6 全文索引
. Full-text indexing 不支持中文
requires borrowing sphinx(coreseek)
or迅搜<、code>技术处理中文。
alter table 表名 add FULLTEXT(`字段名`);
2. View all indexes and deletes of the table
#查看:
show indexes from `表名`;
#或
show keys from `表名`;
#删除
alter table `表名` drop index 索引名;
Third, the index mechanism
1. Why are we 添加完索引
after 查询速度为变快
?
The traditional query method is to traverse in the order of the table. No matter how many pieces of data are queried, mysql needs to traverse the data of the table from beginning to end.
After we add the index, mysql generally BTREE算法
generates an 索引文件
index when querying the database. file , 遍历(折半查找大幅查询效率)
find the corresponding key to obtain data
2. The cost of
indexing Will drop 3. On which columns are indexes used? 3.1 Fields that are frequently used as query conditions should create indexes 3.2 Fields with poor uniqueness are not suitable for creating indexes, although frequently used as query conditions, such as gender fields 3.3 Fields in 3.4 Summary: Fields that meet the following conditions should be indexed . a: must be in the where article b: the content of the field c: the content of the field .占用磁盘空间
二叉树类型的文件
更新非常频繁的字段
不适合作为索引
不会出现在where子句中
不该创建索引
经常使用
不是唯一的几个值
不是频繁变化