MySQL數據庫關於存儲引擎那些事

若是想要修改MySQL數據庫的存儲引擎,那麼必需要了解這兩種引擎,而且清楚的明白這兩種引擎的區別。mysql

MySQL數據庫支持兩種常見的存儲引擎:sql

InnoDB引擎:提供了對數據庫ACID事務的支持,而且實現了SQL標準的四種隔離級別。該引擎還提供了行級鎖和外鍵約束,它的設計目標是處理大容量數據庫系統,它自己其實就是基於MySQL後臺的完整數據庫系統,MySQL運行時Innodb會在內存中創建緩衝池,用於緩衝數據和索引。可是該引擎不支持FULLTEXT類型的索引,並且它沒有保存表的行數,當SELECT COUNT(*) FROM TABLE時須要掃描全表。當須要使用數據庫事務時,該引擎固然是首選。因爲鎖的粒度更小,寫操做不會鎖定全表,因此在併發較高時,使用Innodb引擎會提高效率。可是使用行級鎖也不是絕對的,若是在執行一個SQL語句時MySQL不能肯定要掃描的範圍,InnoDB表一樣會鎖全表。數據庫

MyIASM引擎:是MySQL默認的引擎,可是它沒有提供對數據庫事務的支持,也不支持行級鎖和外鍵,所以當INSERT(插入)或UPDATE(更新)數據時即寫操做須要鎖定整個表,效率便會低一些。不過和Innodb不一樣,MyIASM中存儲了表的行數,因而SELECT COUNT(*) FROM TABLE時只須要直接讀取已經保存好的值而不須要進行全表掃描。若是表的讀操做遠遠多於寫操做且不須要數據庫事務的支持,那麼MyIASM也是很好的選擇。vim

主要區別:
一、MyIASM是非事務安全的,而InnoDB是事務安全的;
二、MyIASM鎖的粒度是表級的,而InnoDB支持行級鎖;
三、MyIASM支持全文類型索引,而InnoDB不支持全文索引;
四、MyIASM相對簡單,效率上要優於InnoDB,小型應用能夠考慮使用MyIASM;
五、MyIASM表保存成文件形式,跨平臺使用更加方便。安全

應用場景:
一、MyIASM管理非事務表,提供高速存儲和檢索以及全文搜索能力,若是再應用中執行大量select操做,應該選擇MyIASM
二、InnoDB用於事務處理,具備ACID事務支持等特性,若是在應用中執行大量insert和update操做,應該選擇InnoDB併發

查看mysql數據庫的引擎信息app

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         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
//InnoDB引擎是默認的存儲引擎。

Support列, YES表示當前版本支持這個存儲引擎, DEFAULT表示該引擎是默認的引擎。NO表示不支持該存儲引擎。
查看系統變量default_storage_engine或storage_engine ide

mysql> show variables like '%storage_engine%';
+----------------------------+--------+
| Variable_name              | Value  |
+----------------------------+--------+
| default_storage_engine     | InnoDB |
| default_tmp_storage_engine | InnoDB |
| storage_engine             | InnoDB |
+----------------------------+--------+
3 rows in set (0.00 sec)

default_storage_engine 表示永久表(permanent tables)的默認存儲引擎。;
default_tmp_storage_engine 表示臨時表的默認存儲引擎;
storage_engine這個系統變量不推薦使用,它已經被系統變量default_storage_engine替代了。設計

修改MySQL數據庫的默認存儲引擎rest

[root@localhost ~]# vim /etc/my.cnf                       //編寫mysql服務的主配置文件
               ………………         //省略部份內容,添加以下內容
default-storage-engine=MyISAM
[root@localhost ~]# systemctl restart mysqld            //從新啓動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             | DEFAULT | 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             | YES     | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
//再次查看發現MyISAM引擎已經成爲默認引擎

在mysql數據庫中直接修改存儲默認引擎

mysql> set default_storage_engine=InnoDB;
Query OK, 0 rows affected (0.00 sec)

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         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
//再次查看發現InnoDB引擎已經成爲默認引擎

注意,這個系統變量default_storage_engine是BOTH(全局和臨時),並且能夠動態修改。可是要注意,即便你修改了系統變量default_storage_engine,重啓事後就會失效,若是你要永久修改,最好在my.cnf配置文件裏面也設置default-storage-engine的值。

查看錶使用的默認引擎

mysql> show create table t1 \G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec
//能夠看出t1表默認使用的引擎是MyISAM。

修改表的默認存儲引擎

mysql> ALTER TABLE t1 ENGINE = InnoDB;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t1 \G;
*************************** 1. row ***************************
       Table: t1
Create Table: CREATE TABLE `t1` (
  `id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
//能夠看出t1表默認使用的引擎是InnoDB。

建立表的時候指定存儲引擎
建立表的時候,若是要指定存儲引擎,只須要設置參數ENGINE便可。

mysql> create table t2 (id int) engine=InnoDB;
Query OK, 0 rows affected (0.00 sec)

mysql> create table t3 (id int) engine=MyISAM;
Query OK, 0 rows affected (0.00 sec)
相關文章
相關標籤/搜索