文章标题

第一部分:基础知识:

索引

官方介绍索引是帮助MySQL高效获取数据的数据结构。笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里,不用一页一页查阅找出需要的资料。关键字index

————————————————————-

唯一索引

强调唯一,就是索引值必须唯一,关键字unique index

创建索引:

1、create unique index 索引名 on 表名(列名);

2、alter table 表名 add unique index 索引名 (列名);

删除索引:

1、 drop index 索引名 on 表名;

2、 alter table 表名 drop index 索引名;

————————————————————-

主键

主键就是唯一索引的一种,主键要求建表时指定,一般用auto_increatment列,关键字是primary key

主键创建:
MySQL
creat table test2 (id int not null primary key auto_increment);
1

creat table test2 (id int not null primary key auto_increment);

————————————————————-

全文索引

InnoDB不支持,Myisam支持性能比较好,一般在 CHAR、VARCHAR 或 TEXT 列上创建。

Create table 表名( id int not null primary anto_increment,title

varchar(100),FULLTEXT(title))type=myisam

——————————

单列索引与多列索引

索引可以是单列索引也可以是多列索引(也叫复合索引)。按照上面形式创建出来的索引是单列索引,现在先看看创建多列索引:
MySQL
create table test3 (id int not null primary key auto_increment,uname char
(8) not null default ”,password char(12) not null,INDEX(uname,password))type
=myisam;
1
2
3

create table test3 (id int not null primary key auto_increment,uname char
(8) not null default ”,password char(12) not null,INDEX(uname,password))type
=myisam;

注意:INDEX(a, b, c)可以当做a或(a, b)的索引来使用,但和b、c或(b,c)的索引来使用这是一个最左前缀的优化方法,在后面会有详细的介绍,你只要知道有这样两个概念

————————————————————-

聚集索引

一种索引,该索引中键值的逻辑顺序决定了表中相应行的物理顺序。聚集索引确定表中数据的物理顺序。Mysql中myisam表是没有聚集索引的,innodb有(主键就是聚集索引),聚集索引在下面介绍innodb结构的时有详细介绍。

————————————————————-

查看表的索引

通过命令:Show index from 表名

如:
MySQL
mysql> show index from test3;
+——-+————+———-+————–+————-+———–+————-+———-+——–+—-+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part |
Packed | Null | Index_type | Comment |
+——-+————+———-+————–+————-+———–+————-+———-+——–+—-+
| test3 | 0 | PRIMARY | 1 | id | A | 0 | NULL |
NULL | | BTREE | |
+——-+————+———-+————–+————-+———–+————-+———-+——–+——+
1
2
3
4
5
6
7
8

mysql> show index from test3;
+——-+————+———-+————–+————-+———–+————-+———-+——–+—-+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part |
Packed | Null | Index_type | Comment |
+——-+————+———-+————–+————-+———–+————-+———-+——–+—-+
| test3 | 0 | PRIMARY | 1 | id | A | 0 | NULL |
NULL | | BTREE | |
+——-+————+———-+————–+————-+———–+————-+———-+——–+——+

Table:表名

Key_name:什么类型索引(这了是主键)

Column_name:索引列的字段名

Cardinality:索引基数,很关键的一个参数,平均数值组=索引基数/表总数据行,平均数值组越接近1就越有可能利用索引

Index_type:如果索引是全文索引,则是fulltext,这里是b+tree索引,b+tre也是这篇文章研究的重点之一

其他的就不详细介绍,更多:

第二部分:MYISAM和INNODB索引结构

1、 简单介绍B-tree B+ tree树

B-tree结构视图

一棵m阶的B-tree树,则有以下性质

(1)Ki表示关键字值,上图中,k1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值