InnoDB 存储引擎详解
接下来重点介绍四种常见的存储引擎:InnoDB、MyISAM、Memory、TokuDB。这部分内容分为上下两个小节,本小节重点介绍常用存储引擎的对比,以及 InnoDB 存储引擎。@H_502_6@
1. 常用存储引擎的对比
2. InnoDB 存储引擎
从 5.5 版本开始,InnoDB 是MysqL的默认事务性引擎,也是最重要、使用最广泛的存储引擎。InnoDB 具有提交、回滚、自动崩溃恢复的事务安全保障,拥有独立的缓存和日志,提供行级别的锁粒度和强大的并发能力。@H_502_6@
下面将介绍 InnoDB 区别于其他存储引擎的特点。@H_502_6@
2.1 自动增长列
MysqL> create table t1(
-> int not null auto_increment,
-> varchar() default null,
-> primary key()
-> ) engine = innodb;
Query OK, rows affected ( sec)
MysqL> insert into t1(,) values(null,'1'),(,'2');
Query OK, rows affected ( sec)
Records: Duplicates: Warnings:
MysqL> select * from t1;
+----+------+
| | |
+----+------+
| | |
| | |
+----+------+
rows in set ( sec)
MysqL> create table t2(
-> int not null auto_increment,
-> varchar() default null,
-> key(,)
-> ) engine = innodb;
ERROR (): Incorrect table deFinition; there can only one auto column and it must defined as key
MysqL> create table t2(
-> int not null auto_increment,
-> varchar() default null,
-> key(,)
-> ) engine = innodb;
Query OK, rows affected ( sec)
2.2 主键和索引
InnoDB 表是基于聚簇索引建立的,聚簇索引也叫主索引,也是表的主键,InnoDB 表的每行数据都保存在主索引的叶子节点上。InnoDB 表必须包含主键,如果创建表时,没有显式指定主键,InnoDB 会自动创建一个长度为 6 个字节的 long 类型隐藏字段作为主键。所有的 InnoDB 表都应该显式指定主键。@H_502_6@
InnoDB 表中,除主键之外的索引,叫做二级索引。二级索引必须包含主键列,如果主键列很大的话,其他的所有索引都会很大。因此,主键是否设计合理,对所有的索引都会产生影响。@H_502_6@
一般来说,主键的设计原则大致如下:@H_502_6@
2.3 存储方式
InnoDB 存储表和索引的方式,有以下两种:@H_502_6@
innodb_file_per_table = #1为开启独享表空间
使用独享表空间时,可以很方便对单表进行备份和恢复操作,但是直接复制 .ibd 文件是不行的,因为缺少共享表空间的数据字典信息,但是可以通过下面的命令,实现 .ibd 文件和 .frm 文件能被正确识别和恢复。@H_502_6@
alter table xxx discard tablespace;
alter table xxx import tablespace;