MyISAM是Mysql的默認存儲引擎,它在磁盤上存儲爲三個文件, .MYD是數據文件 .MYI是索引文件 .frm是存儲表定義。咱們能夠考慮把數據文件和索引文件分別存到不一樣的磁盤,實現IO平均分佈。 mysql
若是對事務完整性沒有要求,或者以select 、insert 爲主的操做,能夠選擇該存儲引擎。
一、MyISAM只會對索引進行緩存,而數據文件則是使用操做系統緩存,當索引數據大於key buffer的時候,也會使用操做系統緩存。sql
二、使用count(*)的時候很快,由於行數是單獨保存的。緩存
三、不支持事務,不支持外鍵,訪問快。ide
四、使用表級鎖定。性能
五、每張表一個數據文件,備份的時候能夠直接複製,恢復的時候也能夠直接覆蓋,操做方便。測試
六、可使用myisamchk進行故障恢復。spa
MyISAM表的存儲格式:靜態表(固定長度)【默認格式】、動態表、壓縮表操作系統
一、固定格式的特色:快速、容易緩存、崩潰後容易恢復、比動態表佔用更大空間。可是靜態表裏面的字段是固定長度的,若是存儲的數據長度不夠,那麼其餘位就是用空格填充,在使用的時候,會由系統去掉空格。因此這個地方引入一個問題,若是咱們的數據裏面尾部原本就含有空格,那麼在使用數據的時候,空格會被去掉, 關於這一點咱們須要很是當心。code
二、動態表格式的特色:除了長度小於4的列,其餘字符型的列長度是可變的;比靜態固定格式使用較少空間;若是一行變得很大的時候,會進行分片,因此會形成碎片化,可使用optimize table或者myisamchk -r來改善性能,使用myisamchk -ei獲取表的統計信息;若是崩潰了,比靜態格式表的恢復困難。orm
效果截圖--------
root->/tmp# myisamchk -r /var/lib/mysql/test/test_table.MYI
- recovering (with keycache) MyISAM-table '/var/lib/mysql/test/test_table.MYI'
Data records: 23068672
root->/tmp# myisamchk -ei /var/lib/mysql/test/test_table.MYI
Checking MyISAM file: /var/lib/mysql/test/test_table.MYI
Data records: 23068672 Deleted blocks: 0
- check file-size
- check record delete-chain
- check key delete-chain
- check index reference
- check records and index references
Records: 23068672 M.recordlength: 7 Packed: -40%
Recordspace used: 100% Empty space: 0% Blocks/Record: 1.00
Record blocks: 23068672 Delete blocks: 0
Record data: 161480704 Deleted data: 0
Lost space: 0 Linkdata: 0
User time 1.25, System time 0.12
Maximum resident set size 1684, Integral resident set size 0
Non-physical pagefaults 516, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 25, Involuntary context switches 2746
root->/tmp#
三、壓縮表的特色:佔用很是少的磁盤空間;
root->/var/lib/mysql/test# ls -lh
total 155M
-rw-rw---- 1 mysql mysql 8.5K Sep 29 14:05 err_table.frm
-rw-rw---- 1 mysql mysql 612 Sep 29 14:05 err_table.MYD
-rw-rw---- 1 mysql mysql 1.0K Nov 26 16:16 err_table.MYI
-rw-rw---- 1 mysql mysql 8.4K Jan 19 23:34 test_table.frm
-rw-rw---- 1 mysql mysql 154M Jan 22 11:19 test_table.MYD
-rw-rw---- 1 mysql mysql 1.0K Jan 22 11:19 test_table.MYI
root->/var/lib/mysql/test# myisampack test_table.MYI
Compressing test_table.MYD: (23068672 records)
- Calculating statistics
- Compressing file
71.43%
root->/var/lib/mysql/test# ls -lh
total 45M
-rw-rw---- 1 mysql mysql 8.5K Sep 29 14:05 err_table.frm
-rw-rw---- 1 mysql mysql 612 Sep 29 14:05 err_table.MYD
-rw-rw---- 1 mysql mysql 1.0K Nov 26 16:16 err_table.MYI
-rw-rw---- 1 mysql mysql 8.4K Jan 19 23:34 test_table.frm
-rw-rw---- 1 mysql mysql 45M Jan 22 11:19 test_table.MYD
-rw-rw---- 1 mysql mysql 1.0K Jan 22 11:24 test_table.MYI
測試MyISAM的事務性
----------------------
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into err_table(errsql) values('ppyy');
Query OK, 1 row affected (0.03 sec)
mysql> select * from err_table;
+---------+--------+--------+
| errcode | errsql | retime |
+---------+--------+--------+
| NULL | ppyy | NULL |
+---------+--------+--------+
1 row in set (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select * from err_table;
+---------+--------+--------+
| errcode | errsql | retime |
+---------+--------+--------+
| NULL | ppyy | NULL |
+---------+--------+--------+
1 row in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from err_table;
+---------+--------+--------+
| errcode | errsql | retime |
+---------+--------+--------+
| NULL | ppyy | NULL |
+---------+--------+--------+
1 row in set (0.00 sec)
mysql> alter table err_table engine=innodb;
Query OK, 1 row affected (0.19 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into err_table(errsql) values('zzqqq');
Query OK, 1 row affected (0.00 sec)
mysql> select * from err_table;
+---------+--------+--------+
| errcode | errsql | retime |
+---------+--------+--------+
| NULL | ppyy | NULL |
| NULL | zzqqq | NULL |
+---------+--------+--------+
2 rows in set (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.05 sec)
mysql> select * from err_table;
+---------+--------+--------+
| errcode | errsql | retime |
+---------+--------+--------+
| NULL | ppyy | NULL |
+---------+--------+--------+
1 row in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from err_table;
+---------+--------+--------+
| errcode | errsql | retime |
+---------+--------+--------+
| NULL | ppyy | NULL |
+---------+--------+--------+
1 row in set (0.00 sec)
----------------------
查看錶狀體
----------------------
mysql> show table status like 'test_table'\G;
*************************** 1. row ***************************
Name: test_table
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 46137344
Avg_row_length: 7
Data_length: 322961408
Max_data_length: 1970324836974591
Index_length: 1024
Data_free: 0
Auto_increment: NULL
Create_time: 2015-01-19 23:34:54
Update_time: 2015-01-22 11:19:17
Check_time: 2015-01-22 11:19:17
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
ERROR:
No query specified
----------------------
MyISAM表常見損壞狀況:
一、在寫過程當中Mysql的進程被殺掉;
二、主機宕機(例如硬件故障)