mysql-------Four index types

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 KEYso 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、texton columns of type. Can be created via ALTER TABLEor 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. MyISAMFull-text indexing is supported, InnoDBwhich 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子句中不该创建索引


经常使用不是唯一的几个值不是频繁变化

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324378100&siteId=291194637