Mysql 存储引擎

mysql常用的存储引擎有MyISAM,InnoDB和NDB等,这里主要介绍这3个存储引擎:

(1)MyISAM:它主要用于大多数的Web、数据仓库(OLAP)和其它应用中。


MyISAM存储引擎, MyISAM特点是不支持事务,适合olap应用, MyISAM表由MYD和MYI组成。mysql-5.0版本之前, MyISAM默认支持的表大小为4G,从MySQL-5.0以后, MyISAM默认支持256T的表单数据。 MyISAM只缓存索引数据,MySQL-5.1.23版本之前无论32、64位操作系统环境下,缓存索引的缓冲区最大只能4G,在之后的版本中,64位系统可以支持大于4G的索引缓冲区。



(2)InnoDB:主要用于事务处理应用,并且支持事务的ACID特性和外键。

Innodb存储引擎,特点支持外键、行锁、非锁定读(默认情况下读取不会产生锁)、mysql-4.1开始支持每个innodb引擎的表单独放到一个表空间里。innodb通过使用MVCC来获取高并发性,并且实现sql标准的4种隔离级别,同时使用一种被称成next-key locking的策略来避免换读(phantom)现象。除此之外innodb引擎还提供了插入缓存(insert buffer)、二次写(double write)、自适应哈西索引(adaptive hash index)、预读(read ahead)等高性能技术。


(3)NDB:支持COMMIT,ROLLBACK和其它事务特性。

NDB存储引擎,2003年MySQL从索爱公司收购的NDB引擎,NDB的特点是数据放在内存中,MySQL-5.1版本开始可以将非索引数据放到磁盘上。NDB之前的缺陷是join查询是MySQL数据库层完成的,而不是存储引擎完成的,复杂的join查询需要巨大的网络开销,速度很慢。当前MySQL cluster7.2版本中已经解决此问题,join查询效率提高了70倍


-+---------------------------------------------------------------+-

修改存储引擎,有以下几种方法:

(1)修改配置文件

可以通过修改数据库配置文件my.cnf中的storage_engine选项来改变默认的存储引擎。

例如:storage_engine = InnoDB

Mysql 5.5开始,默认为InnoDB


(2)在创建表的时候指定引擎

例如:

CREATE TABLE mytable (id int, title char(20)) ENGINE = INNODB


(3)修改表

ALTER TABLE mytable ENGINE = MyISAM


上面介绍到的3个存储引擎中,NDB相对来说用的还是比较少的, 只用在Mysql集群中。MyISAM和InnoDB则是用的最多的存储引擎,下面将对这两个引擎做个对比:



MyISAM InnoDB
事务 不支持 支持
读写效率


索引 支持全文索引 不支持全文索引
外键 不支持 支持
表锁 行锁
文件存储形式 *.MYD *.MYI *.FRM *.FRM(默认为共享表空间,可修改)
适用场景 大量select语句 大量update语句
删除表后数据文件是否存在 自动清除 不自动清除


-+---------------------------------------------------------+-

【实验】测试相同环境下,Innodb和MyISAM 这两个引擎的读写性能
创造实验环境,模拟大量数据(模拟200万条记录)
===================
InnoDB(test1)
mysql> create table test1(id int)
-> partition by hash(id) partitions 5;
Query OK, 0 rows affected (0.52 sec)

mysql> insert into test1 values(1);
Query OK, 1 row affected (0.05 sec)
.
.
mysql> insert into test1 values(10);
Query OK, 1 row affected (0.07 sec)

mysql> insert into test1 select * from test1;
Query OK, 2621440 rows affected (20.57 sec)
Records: 2621440 Duplicates: 0 Warnings: 0

=================
MyISAM(test3)
mysql> create table test3(id int) engine=myisam
-> partition by hash(id) partitions 5;
Query OK, 0 rows affected (0.34 sec)

mysql> insert into test3 values(1);
Query OK, 1 row affected (0.00 sec)
.
.
mysql> insert into test3 values(10);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test3 select * from test3;
Query OK, 2621440 rows affected (1.37 sec)
Records: 2621440 Duplicates: 0 Warnings: 0


mysql> show table status like 'test1'\G;
*************************** 1. row ***************************
Name: test1
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 11369271
Avg_row_length: 31
Data_length: 359661568
Max_data_length: 0
Index_length: 35209216
Data_free: 47185920
Auto_increment: NULL
Create_time: NULL
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options: partitioned
Comment:
1 row in set (2.47 sec)

ERROR:
No query specified

mysql> show table status like 'test3'\G;
*************************** 1. row ***************************
Name: test3
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 9437184
Avg_row_length: 7
Data_length: 66060288
Max_data_length: 0
Index_length: 213734400
Data_free: 0
Auto_increment: NULL
Create_time: 2013-06-23 09:01:20
Update_time: 2013-06-23 09:01:52
Check_time: 2013-06-23 09:02:01
Collation: utf8_general_ci
Checksum: NULL
Create_options: partitioned
Comment:
1 row in set (0.04 sec)

ERROR:
No query specified

=======================
下面是增删改操作的效率对比
(1)插入数据
mysql> insert into test3 select * from test3;
Query OK, 5242880 rows affected (2.81 sec) --- MyISAM
Records: 5242880 Duplicates: 0 Warnings: 0

mysql> insert into test1 select * from test1;
Query OK, 5242880 rows affected (1 min 19.74 sec) --- InnoDB
Records: 5242880 Duplicates: 0 Warnings: 0

结论:插入数据:MyISAM 效率优于 InnoDB

(2) UPDATE
mysql> update test1 set id=1 where id=2;
Query OK, 1048576 rows affected (38.26 sec) --- InnoDB
Rows matched: 1048576 Changed: 1048576 Warnings: 0

mysql> update test3 set id=1 where id=2;
Query OK, 1048576 rows affected (8.86 sec) --- MyISAM
Rows matched: 1048576 Changed: 1048576 Warnings: 0


结论:UPDATE:MyISAM 优于 InnoDB


(3) 创建索引
mysql> create index id on test1(id);
Query OK, 0 rows affected (1 min 35.15 sec) --- InnoDB
Records: 0 Duplicates: 0 Warnings: 0

mysql> create index id on test3(id);
Query OK, 9437184 rows affected (41.02 sec) --- MyISAM
Records: 9437184 Duplicates: 0 Warnings: 0

创建索引速度:MyISAM 优于 InnoDB

(4) DELETE
mysql> delete from test1 where id='10';
Query OK, 1048576 rows affected (2 min 15.21 sec) --- InnoDB

mysql> delete from test3 where id='10';
Query OK, 1048576 rows affected (11.37 sec) --- MyISAM

DELETE:MyISAM 优于 InnoDB

可以看到,MyISAM存储引擎,在增删改方面明显比InnoDB存储引擎的效率要高,这也是为什么仍有那么多数据库使用MyISAM存储引擎的原因。建议在不需要支持事务的时候,选择MyISAM 效果会更好。




你可能感兴趣的:(mysql)