MySQL索引

2019-07-13  本文已影响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);

上一篇下一篇

猜你喜欢

热点阅读