首页 > 数据库技术 > 详细

mysql --索引

时间:2019-09-21 22:34:09      阅读:81      评论:0      收藏:0      [点我收藏+]

优点:

(1)通过建立唯一索引或者主键索引,保证数据库表中每一行数据的唯一性

(2)大大提高了检索数据的效率,减少表的检索行数

 

缺点:

(1)在创建索引和维护索引的时候会耗费时间,随着数据量的增加时间复杂度增加

(2)索引文件会占用物理内存

(3)当对表的数据进行增删改的时候,所以也要动态的维护,这样就会降低数据的而维护速度

 

分类:单列索引,组合索引

单列索引

(1)一个索引只包含一个列

(2)可以存在多个单列索引

(3)单列索引分为:主键索引,唯一索引,普通索引

组合索引

一个组合可以包含两个或者两个以上的列

 

创建一个普通索引

方式一
create index 索引名 on 表名(字段名)
方式二
alter table 表名 add index 索引名 【using {btree|hash}】(字段名)
 

 例如:在学生表stu的学号列上的前5个字符建立一个而肾虚索引stu_id

方式一
create index stu_id  on stu(学号(5),asc);
方式二
alter table stu add index stu_id 【using {btree|hash}】(学号(5),asc);

  

创建唯一索引,与普通索引类似,必须唯一,可以存在空值

方式一
create unique index 索引名 on 表名(字段名)
方式二
alter table 表名 add  unique 【using {btree|hash}】(字段名)

  

创建主键索引,必须唯一,不可以存在空值 

方式一
创建表的时候添加
方式二
alter table 表名 add  primary key 【using {btree|hash}】(字段名)

 

 

组合索引

以上的语句中,字段定义多个,用,分割,只有一个索引名称

方式一
create  index 索引名 on 表名(字段名,字段名)
方式二
alter table 表名 add  index 【using {btree|hash}】(字段名,字段名)

  例如

方式一
create  index fuhe_id on stu(学号,课程号)
方式二
alter table stu add  index 【using {btree|hash}】(学号,课程号)

  

查看表中索引情况

show index from 表名;

 

创建表的过程创建索引:

#正常创建表
create table 【if not exists】表名(
       列名 数据类型,
       列名  数据类型,
       列名 数据类型,
       primary key 【using{hash|btree} 】(列名,列名),
       index 【key】【using{hash|btree} 】(列名,列名),
       unique 【key】【using{hash|btree} 】(列名,列名),)      ;          

  

删除索引

方式一
drop index 索引名称 on 表名;
方式二
alter table 表名 drop primary key| drop index key 索引名 foreign key 外键名;

 

explain 命令

直接在select 语句之前加入即可

显示如何使用索引来处理select语句以及连接表

mysql --索引

原文:https://www.cnblogs.com/ivyharding/p/11564769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!