MySQL 提供多种存储引擎对数据表进行处理,MySQL 5.1 (如无特殊说明, 本文所使用版本均为 5.1) 引入了新的插件式存储引擎体系结构,对于编程人员而言,插拔及修改存储引擎都是极其方便的。
纵观以上3中常用的存储引擎,InnoDB和Heap在以后新的应用中更有可用性。
以下再看看MyISAM和InnoDB的索引图示
这些引擎包括处理事务安全表的引擎和处理非事务安全表的引擎:
Feature | MyISAM | Memory | InnoDB | Archive | NDB |
---|---|---|---|---|---|
Storage limits | 256TB | RAM | 64TB | None | 384EB |
Transactions | No | No | Yes | No | Yes |
Locking granularity | Table | Table | Row | Row | 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 | Yes |
Hash indexes | No | Yes | No | No | Yes |
Full-text search indexes | Yes | No | No | 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 | No | Yes | Yes | No |
Encrypted data | Yes | Yes | Yes | Yes | Yes |
Cluster database support | No | No | No | No | Yes |
Replication support | Yes | Yes | Yes | Yes | Yes |
Foreign key support | No | No | Yes | No | No |
Backup / point-in-time recovery | Yes | Yes | Yes | Yes | Yes |
Query cache support | Yes | Yes | Yes | Yes | Yes |
Update statistics for data dictionary | Yes | Yes | Yes | Yes | Yes |
一般应用程序中常用的存储引擎主要是 MyISAM 与 InnoDB 两种.
MyISAM 是 MySQL 5.5 之前 (不含 5.5) 的默认存储引擎, MyISAM 的最大缺陷在于不支持事务处理 (transaction).
每个 MyISAM 表在磁盘上存储成三个文件 (如 /var/lib/mysql/db 下). 文件名即为表名, 扩展名指出文件类型. 表格式文件的扩展名为 .frm; 数据文件的扩展名为.MYD (MYData); 索引文件的扩展名是.MYI (MYIndex).
从 MySQL 5.5 开始, InnoDB 成为 MySQL 的默认存储引擎.
InnoDB 为 MySQL 提供了具有提交, 回滚和崩溃恢复能力的事务安全 (ACID兼容) 存储引擎. InnoDB 设计目的是为处理巨大数据量时发挥最大性能. 它的CPU效率可能是任何其它基于磁盘的关系数据库引擎所不能匹敌的.
InnoDB 存储引擎被完全与 MySQL 服务器整合, InnoDB 存储引擎为在主内存中缓存数据和索引而维持它自己的缓冲池. InnoDB 将其表 & 索引存储在一个表空间中, 表空间可以包含数个文件 (或原始磁盘分区), 这与 MyISAM 表中每个表被存在分离的文件中有所不同. InnoDB 表可以是任何尺寸, 即使在文件尺寸被限制为 2GB 的操作系统上.
InnoDB 被用在众多需要高性能的大型数据库站点上.
创建表时指定存储引擎
- mysql> CREATE TABLE t (i INT) ENGINE = MYISAM;
修改表的存储引擎
- mysql> ALTER TABLE t ENGINE = MYISAM;
显示存储引擎状态信息
- mysql> SHOW ENGINES;
- +------------+---------+----------------------------------------------------------------+--------------+------+------------+
- | Engine | Support | Comment | Transactions | XA | Savepoints |
- +------------+---------+----------------------------------------------------------------+--------------+------+------------+
- | InnoDB | YES | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
- | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
- | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
- | CSV | YES | CSV storage engine | NO | NO | NO |
- | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
- | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
- | ARCHIVE | YES | Archive storage engine | NO | NO | NO |
- | MyISAM | DEFAULT | Default engine as of MySQL 3.23 with great performance | NO | NO | NO |
- +------------+---------+----------------------------------------------------------------+--------------+------+------------+
- 8 rows in set (0.00 sec)
- 显示当前默认存储引擎
- mysql> SHOW VARIABLES LIKE '%storage_engine%';
- +----------------+--------+
- | Variable_name | Value |
- +----------------+--------+
- | storage_engine | MyISAM |
- +----------------+--------+
- 1 row in set (0.00 sec)
显示当前默认存储引擎
- $ mysqlshow -u<user> -p<password> --status <database>
- mysql> SHOW TABLE STATUS FROM trac;
MyISAM 表保存为文件方式, 很容易备份. 为保持备份一致性, 对相关表执行 LOCK TABLES 操作进行读锁定 (复制数据库目录中的文件时, 允许其它客户继续查询表), 然后对表执行 FLUSH TABLES. 你只需要读锁定; 这样当你复制数据库目录中的文件时, 允许其它客户继续查询表. 需要 FLUSH TABLES 语句来确保开始备份前将所有激活的索引页写入硬盘.
只要服务器不再进行更新, 还可以只复制所有表文件(.frm、.MYD和*.MYI文件). mysqlhotcopy 脚本使用该方法. (但请注意如果数据库包含 InnoDB 表, 这些方法不工作. InnoDB 不将表的内容保存到数据库目录中, mysqlhotcopy 只适合 MyISAM 表).
如果你在服务器上进行备份, 并且表均为MyISAM表, 应考虑使用mysqlhotcopy, 因为可以更快地进行备份和恢复:
- $ mysqlhotcopy db_name [/path/to/new_directory]
- $ mysqlhotcopy db_name_1 ... db_name_n /path/to/new_directory
- $ mysqlhotcopy db_name./regex/
对于 InnoDB 表, 可以进行在线备份, 不需要对表进行锁定:
- $ mysqldump [options] db_name [tables]
- $ mysqldump [options] ---database DB1 [DB2 DB3...]
- $ mysqldump [options] --all--database