美文网首页
MySQL索引

MySQL索引

作者: 爱喝气泡水 | 来源:发表于2019-07-13 19:40 被阅读0次

一:索引概述

1、索引是一种将数据库中单列或这多列的值进行排序的结构。

2、通过索引查数据,不但可以提高查询速率,也可以降低服务器负载。当查询数据时,系统可以不用遍历数据表中所有记录,而是查询索引列。

3、但是创建索引和维护需要耗费时间,并且该耗费时间与数据量的大小成正比;另外索引需要占用物理空间,给数据的维护造成很多麻烦。

4、索引可以提高查询速率,但是会影响用户操作数据库的插入操作。因为:插入时数据库系统会按照索引进行排序。

5、所有存储引擎对每个表至少支持16个索引。总索引长度不能超过256字节。

二:索引有两种存储类型,包括BTREE索引和哈希索引,BTREE索引为系统默认索引。

三:索引分类:

1、普通索引:不应用任何条件的索引,可以在任何数据类型中创建。字段本身的约束条件可以判断其值是否为空或唯一。

2、唯一索引:使用UNIQUE参数可以设置唯一索引。索引的值必须唯一,主键是一种特殊的唯一索引。

3、全文索引:使用FULLTEXT参数可以设置索引为全文索引。全文索引只能创建在CHAR、VARCHAR、TEXT类型的字段上。只适用于MyISAM存储引擎中。

4、单列索引:只对应一个字段的索引。可以包括以上三种索引方式。

5、多列索引:在表的多个字段中创建一个索引,使用查询时必须使用多个字段中的第一个字段才能使索引生效。

6、空间索引:使用SPATIAL参数可以设置空间索引,只能建立在空间数据模型上。MySQL中只有MyISAM存储引擎支持空间索引。而且索引字段不能为空值,且字段数据类型必须为GEOMETRY、POINT、LINESTRING、POLYGON等类型。

四:查看表结构

show create table normal_index;

五:创建索引语法

1、创建普通索引:任意类型的字段

(1)创建表时创建索引

    create table normal_index(

    id int(11) auto_increment primary key not null, 

    name varchar(64) not null,

    math int(5) not null,

    chiness varchar(64) not null,

    index name_index(name)

    );

(2)在已创建的表中创建索引:

    create index chiness_index on normal_index(chiness);

(3)修改数据表结构添加索引:

    alter table table_name add  unique|fulltext|spatial   index      cloumn_index_name(cloumn);

(4)删除索引:

    drop index index_name on table_name;

2、创建唯一索引:索引字段的值必须唯一

(1)创建表时创建索引

    create table normal_index1(

    id int(11) auto_increment primary key not null,

    name varchar(64) not null,

    math int(5) not null,

    chiness varchar(64) not null,

    unique index name_unique_index(name asc)

    );

(2)在已创建的表中创建索引:

    create unique index chiness_unique_index on normal_index1(chiness);

3、创建全文索引:只能用在char,varchar、text类型的字段上

(书上说使用INNODB存储引擎时,会报错。表示只有MyISAM存储引擎才支持。但是我在mysql8下面使用INNODB存储引擎是可以创建成功。)

(1)创建表时创建索引

    create table normal_index2(

    id int(11) auto_increment primary key not null,

    name varchar(64) not null,

    math int(5) not null,

    chiness varchar(64) not null,

    FULLTEXT index name_fulltext_index(name)

    );

(2)在已创建的表中创建索引:

    create fulltext index chiness_fulltext_index on normal_index2(chiness);

3、创建单列索引:

(1)创建表时创建索引

    create table normal_index3(

    id int(11) auto_increment primary key not null,

    name varchar(64) not null,

    math int(5) not null,

    chiness varchar(64) not null,    

    index name_cloumn_index(name(20))

    );

(2)在已创建的表中创建索引:

    create index chiness_cloumn_index on normal_index3(chiness(20));

3、创建多列索引:查询条件只有使用了第一个字段才会使用到索引

(1)创建表时创建索引

    create table normal_index4(

    id int(11) auto_increment primary key not null,

    name varchar(64) not null,

    math int(5) not null,

    chiness varchar(64) not null,

    index name_cloumns_index(name,math)

    );

(2)在已创建的表中创建索引:

    create index chiness_cloumns_index on normal_index4(chiness,math);

3、创建空间索引:只有MyISAM存储引擎才能支持该类型索引,该字段必须有非空约束,数据类型必须为GEOMETRY、POINT、LINESTRING、POLYGON等类型。

(1)创建表时创建索引

    create table normal_index5(

    id int(11) auto_increment primary key not null,

    name geometry not null,

    math int(5) not null,

    chiness geometry not null,

    spatial index name_spatial_index(name)

    )engine = MyISAM;

(2)在已创建的表中创建索引:

    create spatIal chiness_spatial_index on normal_index5(chiness);

相关文章

  • MySQL索引及查询优化书目录

    MySQL索引的原理之索引目的 MySQL索引的原理之索引原理 MySQL索引的原理之索引的类型 MySQL索引的...

  • 高性能的索引策略

    MySQL查询基础-查询执行过程 MySQL聚簇索引 MySQL覆盖索引 MySQL索引扫描排序 MySQL冗余和...

  • MySQL索引的使用

    MySQL索引 MySQL索引可以快速提高MySQL的检索速度。索引分单列索引和组合索引单列索引:即一个索引只包含...

  • Mysql索引与锁

    本文以Mysql5.7为例测试。 1:mysql索引方法 Mysql的索引方法分为btree索引和hash索引。 ...

  • 索引(二)

    mysql索引的新手入门详解mysql索引之三:索引使用注意规则 索引(Index)是帮助 MySQL 高效获取数...

  • MySQL 索引分类

    MySQL索引的分类(根据数据结构) 索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL...

  • MySQL--索引

    MySQL索引 查看索引 创建索引 创建唯一索引 创建主键索引 删除索引 删除主键 MySQL视图 创建视图 删除...

  • mysql索引

    索引 mysql索引的建立对于mysql的高效运行是很重要的,索引可以大大提高mysql的检索速度。索引分单列索引...

  • 5.2MySQL创建高性能索引考察点

    MySQL索引的基础和类型延伸:MySQL索引的创建原则延伸:MySQL索引的注意事项 索引的基础索引类似于书籍的...

  • MySql 数据查询优化

    1. MySQL索引类型: mysql的索引有5种:主键索引、普通索引、唯一索引、全文索引、聚合索引(多列索引)。...

网友评论

      本文标题:MySQL索引

      本文链接:https://www.haomeiwen.com/subject/qswmkctx.html