Mysql的存储引擎之:MyISAM存储引擎

来源:互联网 发布:网络贷款骗局 编辑:程序博客网 时间:2024/05/19 20:23

MyISAM存储引擎是mysql5.5版本之前的的默认存储引擎。


1、建立MyISAM存储引擎的表


创建一个基于MyISAM存储引擎的表table_myisam:

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

查看下table_ myisam表的数据文件:

注:. MYD是存储数据的文件; .MYI是存储表索引信息的文件; .frm是表结构定义文件

[mysql@localhost test]$ ll table_myisam.*-rw-rw---- 1 mysql mysql 8556 Sep  4 12:25 table_myisam.frm-rw-rw---- 1 mysql mysql    0 Sep 4 12:25 table_myisam.MYD-rw-rw---- 1 mysql mysql 1024 Sep 4 12:25 table_myisam.MYI

2、MyISAM存储引擎的存储格式


三种存储格式:静态、动态、压缩


2.1、静态格式

静态:FIXED, 定长的列(每一列都是固定的字节数),不包含变长类型的列(例如:varchar)

 

创建一个静态的表:

mysql> create table table_myisam_fix(idint, name char(20)) engine=myisam;Query OK, 0 rows affected (0.04 sec)

查看表table_myisam_fix的状态,为Fixed静态格式:

mysql> show table status like'table_myisam_fix';| Name             | Engine  | Version | Row_format | Rows| table_myisam_fix | MyISAM  |    10   | Fixed      |   0……

2.2、动态格式

创建一个动态的表:

mysql> create table table_myisam_dynamic(id int, name varchar(20)) engine=myisam;Query OK, 0 rows affected (0.00 sec)

查看表table_myisam_dynamic的状态,为Dynamic动态格式:

mysql> show table status like'table_myisam_dynamic';| Name               |  Engine | Version | Row_format | Rows| table_myisam_dynamic | MyISAM |    10 | Dynamic    |   0……


2.3、Row_format的作用

在create table 或alter table的时候,可通过Row_format来强制是静态还是动态格式

 

下面通Row_format来强制创建一个只有不定长列的静态表:

mysql> create table table_myisam_dynamic_format_fix(name varchar(10))row_format=fixed engine=myisam;Query OK, 0 rows affected (0.36 sec)mysql> show table status like 'table_myisam_dynamic_format_fix';| Name                            | Engine  |  Version| Row_format | Rows| table_myisam_dynamic_format_fix | MyISAM  |      10 | Fixed      |  0……

1:虽然只有不定长的varchar列,但由于限制了row_format=fixed,所以创建的表是静态表,而且列是固定长度,当不足位数时会补位以到达定义的列长度;

2:当表中含有BLOBTEXT类型,强制的转换row_format=fixed将无效,只能是动态表;

 

2.4、压缩格式

压缩格式比较特殊,用myisampack工具创建,压缩后是只读的表,不能添加或者修改记录,但空间占用会很小

 

创建一个用于压缩的测试表table_myisam_compressed:

mysql> create table table_myisam_compressed engine=myisam as select * from information_schema.columns;Query OK, 1732 rows affected (1.09 sec)Records: 1732  Duplicates: 0 Warnings: 0

插入测试数据,多插入几次:

mysql> insert into table_myisam_compressed select * from table_myisam_compressed;Query OK, 1732 rows affected (0.06 sec)Records: 1732  Duplicates: 0 Warnings: 0

查看数据文件的大小,为7.9M:

[mysql@localhost test]$ ll -h  table_myisam_compressed.*-rw-rw---- 1 mysql mysql  14K Sep 7 14:45 table_myisam_compressed.frm-rw-rw---- 1 mysql mysql 7.9M Sep  7 14:46 table_myisam_compressed.MYD-rw-rw---- 1 mysql mysql 1.0K Sep 7 14:46 table_myisam_compressed.MYI

使用myisampack工具压缩表:

[mysql@localhost test]$ myisampack ./table_myisam_compressed.MYICompressing ./table_myisam_compressed.MYD:(55424 records)- Calculating statistics- Compressing file55.73%  

再次查看数据文件的大小,变为3.5M了:

[mysql@localhost test]$ ll -h  table_myisam_compressed.*-rw-rw---- 1 mysql mysql  14K Sep 7 14:45 table_myisam_compressed.frm-rw-rw---- 1 mysql mysql 3.5M Sep  7 14:46 table_myisam_compressed.MYD-rw-rw---- 1 mysql mysql 1.0K Sep 7 14:52 table_myisam_compressed.MYI

运行myisamchk以重新创建index

[mysql@localhost test]$ myisamchk -rq --sort-index --analyze table_myisam_compressed.MYI- check record delete-chain- recovering (with keycache) MyISAM-table'table_myisam_compressed.MYI'Data records: 55424- Sorting index for MyISAM-table 'table_myisam_compressed.MYI'

运行mysqladminflush-tables刷新表:

[root@localhost ~]# mysqladmin flush-tables


解压缩使用—unpack选项,可自己实验。

0 0