1. 程式人生 > >mysql資料庫引擎MyISAM和InnoDB的比較和使用場合

mysql資料庫引擎MyISAM和InnoDB的比較和使用場合

資料庫中資料管理的一種方式,MySQL支援插入式的儲存引擎,也就是說你同一資料庫中的資料表可以選擇不同儲存引擎

檢視mysql版本

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.25a   |
+-----------+
1 row in set

檢視mysql引擎

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | 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         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

mysql5.5版本預設的資料庫引擎是innoDB

最常見的資料庫引擎是InnoDB和MyISAM

MyISAM

ISAM執行讀取操作的速度很快,而且不佔用大量的記憶體和儲存資源。ISAM的兩個主要不足之處在於,它不 支援事務處理,也不能夠容錯,MyISAM除了提供ISAM裡所沒有的索引和欄位管理的大量功能,MyISAM還使用一種表格鎖定的機制,來優化多個併發的讀寫操作,其代價是你需要經常執行OPTIMIZE TABLE命令,來恢復被更新機制所浪費的空間。MYISAM強調了快速讀取操作

InnoDB

在使用MYSQL的時候,你所面對的每一個挑戰幾乎都源於ISAM和MyISAM資料庫引擎不支援事務處理(transaction process)也不支援外來鍵。儘管要比ISAM和 MyISAM引擎慢很多,但是InnoDB包括了對事務處理和外來鍵的支援,這兩點都是前兩個引擎所沒有的。

兩種引擎的比較與使用場合

MyISAM型別不支援事務處理等高階處理,而InnoDB型別支援。

MyISAM型別的表強調的是效能,其執行數度比InnoDB型別更快,但是不提供事務支援,而InnoDB提供事務支援已經外部鍵等高階資料庫功能。

MyISAM格式的一個重要缺陷就是不能在表損壞後恢復資料

InnoDB 中不儲存表的具體行數,也就是說,執行select count(*) from table時,InnoDB要掃描一遍整個表來計算有多少行,但是MyISAM只要簡單的讀出儲存好的行數即可。注意的是,當count(*)語句包含where條件時,兩種表的操作是一樣的

MyISAM的索引和資料是分開的,並且索引是有壓縮的,記憶體使用率就對應提高了不少。能載入更多索引,而Innodb是索引和資料是緊密捆綁的,沒有使用壓縮從而會造成Innodb比MyISAM體積龐大

DELETE FROM table時,InnoDB不會重新建立表,而是一行一行的刪除

MyISAM適合:

(1)做很多count 的計算;

(2)插入不頻繁,查詢非常頻繁;

(3)沒有事務。

InnoDB適合:

(1)可靠性要求比較高,或者要求事務;

(2)表更新和查詢都相當的頻繁,並且表鎖定的機會比較大的情況指定資料引擎的建立

更改一個表的資料庫引擎

查看錶結構

mysql> describe cds;
+-----------+---------------------+------+-----+---------+----------------+
| Field     | Type                | Null | Key | Default | Extra          |
+-----------+---------------------+------+-----+---------+----------------+
| titel     | varchar(200)        | YES  |     | NULL    |                |
| interpret | varchar(200)        | YES  |     | NULL    |                |
| jahr      | int(11)             | YES  |     | NULL    |                |
| id        | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
+-----------+---------------------+------+-----+---------+----------------+
4 rows in set

檢視建表語句

mysql> show create table cds;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                               |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| cds   | CREATE TABLE `cds` (
  `titel` varchar(200) COLLATE latin1_general_ci DEFAULT NULL,
  `interpret` varchar(200) COLLATE latin1_general_ci DEFAULT NULL,
  `jahr` int(11) DEFAULT NULL,
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

查看錶的詳細資訊

mysql> show table status like 'cds';
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-------------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation         | Checksum | Create_options | Comment |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-------------------+----------+----------------+---------+
| cds  | MyISAM |      10 | Dynamic    |    3 |             49 |         176 | 281474976710655 |         2048 |        28 |              7 | 2005-10-25 15:47:49 | 2012-04-16 23:30:18 | NULL       | latin1_general_ci | NULL     |                |         |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-------------------+----------+----------------+---------+
1 row in set

修改資料庫引擎

mysql> alter table cds engine=InnoDB;
Query OK, 3 rows affected
Records: 3  Duplicates: 0  Warnings: 0

再次查看錶的詳細資訊

mysql> show table status like 'cds';
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation         | Checksum | Create_options | Comment |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| cds  | InnoDB |      10 | Compact    |    3 |           5461 |       16384 |               0 |            0 |   6291456 |              7 | 2012-04-16 23:30:18 | NULL        | NULL       | latin1_general_ci | NULL     |                |         |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+