当前位置:Gxlcms > mysql > MySQL存储引擎简介

MySQL存储引擎简介

时间:2021-07-01 10:21:17 帮助过:31人阅读

存储引擎(Storage Engine)是处理不同类型的表的SQL操作的MySQL部件,也被称为表类型。 MySQL支持多种的存储引擎,比如最新的MySQL 5.6就支持InnoDB、MyISAM、Memory、CSV、 Archive、Blackhole、Merge、Federated、Example等9种存储引擎;不同存储引擎的各自

存储引擎(Storage Engine)是处理不同类型的表的SQL操作的MySQL部件,也被称为表类型。
MySQL支持多种的存储引擎,比如最新的MySQL 5.6就支持InnoDB、MyISAM、Memory、CSV、 Archive、Blackhole、Merge、Federated、Example等9种存储引擎;不同存储引擎的各自功能、性能和适用场景都各不相同。
5.5.5版本之前的MySQL是使用MyISAM作为默认的存储引擎的;而从5.5.5版本开始,MySQL使用InnoDB作为默认的存储引擎。

查看MySQL支持的存储引擎:
使用“show engines;”SQL语句可以查询当前MySQL支持的存储引擎,如下是在RHEL中的MySQL 5.1.61中的信息:

mysql> show engines;
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
| InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
+------------+---------+------------------------------------------------------------+--------------+------+------------+
5 rows in set (0.00 sec)

其中Support字段为DEFAULT的引擎是默认的存储引擎。

查看数据库中各个表使用的存储引擎:
mysql> SHOW TABLE STATUS FROM my-db-name;

指定存储引擎(配置):
1. 在mysql配置文件(/etc/my.conf)中指定默认引擎,如用“default-storage-engine=innodb”设置就指定了InnoDB为默认的存储引擎。
2. 在启动mysqld的命令行时添加“–default-storage-engine=innodb”或“–default-table-type=myisam”这样的参数(PS:对于RHEL系统中mysqld服务,可以修改/etc/init.d/mysqld文件中的 $exec 这一行).
3. 创建表时指定存储引擎的类型,如: CREATE TABLE mytable (id int, title char(20)) ENGINE=innodb
4. 改变一个表使用的存储引擎,SQL语句为:ALTER TABLE mytable ENGINE=MyISAM

不同存储引擎的区别:
InnoDB是支持事务的,具有ACID(A: atomicity. C: consistency. I:isolation.D: durability.原子性、一致性、隔离性、持久性)特性;InnoDB也是支持外键(foreign key)的,并且支持细粒度的行级别的锁(row-level locking)。
而MyISAM不支持事务、不支持外键(但其访问速度较快),支持表级别的锁(而不是细粒度的行级别锁)。
Memory存储引擎在较老的MySQL中也叫HEAP,是将所有的数据都存放在内存中的,它具有快速读写的优势,一般存放一些不重要的但需要快速查找的数据(如临时表)。
如下表格来自MySQL官方网站,是一些主流存储引擎的特性比较:

Table 14.1. Storage Engines Feature Summary

Feature MyISAM Memory InnoDB Archive NDB
Storage limits 256TB RAM 64TB None 384EB
Transactions No No Yes No Yes
Locking granularity Table Table Row Table Row
MVCC No No Yes No No
Geospatial data type support Yes No Yes Yes Yes
Geospatial indexing support Yes No No No No
B-tree indexes Yes Yes Yes No No
T-tree indexes No No No No Yes
Hash indexes No Yes No[a] No Yes
Full-text search indexes Yes No Yes[b] No No
Clustered indexes No No Yes No No
Data caches No N/A Yes No Yes
Index caches Yes N/A Yes No Yes
Compressed data Yes[c] No Yes[d] Yes No
Encrypted data[e] Yes Yes Yes Yes Yes
Cluster database support No No No No Yes
Replication support[f] Yes Yes Yes Yes Yes
Foreign key support No No Yes No No
Backup / point-in-time recovery[g] Yes Yes Yes Yes Yes
Query cache support Yes Yes Yes Yes Yes
Update statistics for data dictionary Yes Yes Yes Yes Yes

[a] InnoDB utilizes hash indexes internally for its Adaptive Hash Index feature.

[b] InnoDB support for FULLTEXT indexes is available in MySQL 5.6.4 and higher.

[c] Compressed MyISAM tables are supported only when using the compressed row format. Tables using the compressed row format with MyISAM are read only.

[d] Compressed InnoDB tables require the InnoDB Barracuda file format.

[e] Implemented in the server (via encryption functions), rather than in the storage engine.

[f] Implemented in the server, rather than in the storage engine.

[g] Implemented in the server, rather than in the storage engine.

参考资料:
storage engines: http://dev.mysql.com/doc/refman/5.6/en/storage-engines.html
MyISAM: http://dev.mysql.com/doc/refman/5.6/en/myisam-storage-engine.html
InnoDB: http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html

人气教程排行