집 >데이터 베이스 >MySQL 튜토리얼 >MySQL管理与优化(5)_MySQL
mysql> SHOW ENGINES/G*************************** 1. row *************************** Engine: FEDERATED Support: NO Comment: Federated MySQL storage engineTransactions: NULL XA: NULL Savepoints: NULL*************************** 2. row *************************** Engine: MRG_MYISAM Support: YES Comment: Collection of identical MyISAM tablesTransactions: NO XA: NO Savepoints: NO*************************** 3. row *************************** Engine: MyISAM Support: YES Comment: MyISAM storage engineTransactions: NO XA: NO Savepoints: NO*************************** 4. row *************************** Engine: BLACKHOLE Support: YES Comment: /dev/null storage engine (anything you write to it disappears)Transactions: NO XA: NO Savepoints: NO*************************** 5. row *************************** Engine: CSV Support: YES Comment: CSV storage engineTransactions: NO XA: NO Savepoints: NO*************************** 6. row *************************** Engine: MEMORY Support: YES Comment: Hash based, stored in memory, useful for temporary tablesTransactions: NO XA: NO Savepoints: NO*************************** 7. row *************************** Engine: ARCHIVE Support: YES Comment: Archive storage engineTransactions: NO XA: NO Savepoints: NO*************************** 8. row *************************** Engine: InnoDB Support: DEFAULT Comment: Supports transactions, row-level locking, and foreign keysTransactions: YES XA: YES Savepoints: YES*************************** 9. row *************************** Engine: PERFORMANCE_SCHEMA Support: YES Comment: Performance SchemaTransactions: NO XA: NO Savepoints: NO
mysql> CREATE TABLE ai( -> id bigint(20) NOT NULL AUTO_INCREMENT, -> PRIMARY KEY(id)) ENGINE=MyISAM DEFAULT CHARSET=utf8;Query OK, 0 rows affected (0.51 sec)mysql> ALTER TABLE ai ENGINE=InnoDB;Query OK, 0 rows affected (0.23 sec)Records: 0 Duplicates: 0 Warnings: 0
Feature | MyISAM | Memory | InnoDB | Archive | NDB |
---|---|---|---|---|---|
Storage limits | 256TB | RAM | 64TB | None | 384EB |
Transactions | No | No | Yes | No | Yes |
Locking granularity | Table | Table | Row | Table | 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 | No |
T-tree indexes | No | No | No | No | Yes |
Hash indexes | No | Yes | No[a] | No | Yes |
Full-text search indexes | Yes | No | Yes[b] | 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[c] | No | Yes[d] | Yes | No |
Encrypted data[e] | Yes | Yes | Yes | Yes | Yes |
Cluster database support | No | No | No | No | Yes |
Replication support[f] | Yes | Yes | Yes | Yes | Yes |
Foreign key support | No | No | Yes | No | No |
Backup / point-in-time recovery[g] | Yes | Yes | Yes | Yes | Yes |
Query cache support | Yes | Yes | Yes | Yes | Yes |
Update statistics for data dictionary | Yes | Yes | Yes | Yes | Yes |
[a] InnoDB utilizes hash indexes internally for its Adaptive Hash Index feature. [b] InnoDB support for FULLTEXT indexes is available in MySQL 5.6.4 and higher. [c] Compressed MyISAM tables are supported only when using the compressed row format. Tables using the compressed row format with MyISAM are read only. [d] Compressed InnoDB tables require the InnoDB Barracuda file format. [e] Implemented in the server (via encryption functions), rather than in the storage engine. [f] Implemented in the server, rather than in the storage engine. [g] Implemented in the server, rather than in the storage engine. |
1. frm(存储表定义);
2. MYD(MYData, 存储数据);
3. MYI(MYIndex,存储索引)
1. 静态表:字段都非变长,存取快,占用空间多,返回数据会丢失尾部的空格。如,
mysql> CREATE TABLE test_myisam(name CHAR(10)) ENGINE=MYISAM;Query OK, 0 rows affected (0.10 sec)mysql> INSERT INTO test_myisam values('abc'),('abc '), (' ahc');mysql> SELECT name, length(name) FROM test_myisam;+-------+--------------+| name | length(name) |+-------+--------------+| abc | 3 || abc | 3 || ahc | 5 |+-------+--------------+3 rows in set (0.06 sec)
2. 动态表:字段可变长,记录不是固定的长度,占用空间较少,碎片多,可通过myisamchk -r或OPTIMIZE TABLE来优化。
3. 压缩表:可通过myisampack来创建,占用空间少,访问开销小。
范例:
-- 主表mysql> CREATE TABLE country ( -> country_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, -> country VARCHAR(50) NOT NULL, -> last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -> PRIMARY KEY (country_id))ENGINE=InnoDB DEFAULT CHARSET=utf8;Query OK, 0 rows affected (0.09 sec)-- 从表mysql> CREATE TABLE city( -> city_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, -> city VARCHAR(50) NOT NULL, -> country_id SMALLINT UNSIGNED NOT NULL, -> last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -> PRIMARY KEY (city_id), -> KEY idx_fk_country_id (country_id), -> CONSTRAINT FOREIGN KEY (country_id) REFERENCES country(country_id) ON DELETE RESTRICT ON UPDATE CASCADE)ENGINE=InnoDB DEFAULT CHARSET=utf8;Query OK, 0 rows affected (0.07 sec)
1. 共享表空间存储。.frm文件保存表结构信息,数据和索引保存在innodb_data_home_dir和innodb_data_file_path定义的表空间里,可以是多个文件。
2. 多表空间存储。.frm文件保存表结构信息,每个表的数据和索引单独保存在.ibd的文件中,如果是分区表,每个分区对应单独的.ibd文件,文件名为“表名+分区名”。若要使用多表存储,需要设置参数innodb_file_per_table,再重启服务器。
-- 初始化表mysql> CREATE TABLE payment_2006( -> country_id smallint, -> payment_date datetime, -> amount decimal(15, 2), -> KEY idx_fk_country_id (country_id))engine=myisam;Query OK, 0 rows affected (0.13 sec)mysql> CREATE TABLE payment_2007( -> country_id smallint, -> payment_date datetime, -> amount decimal(15, 2), -> KEY idx_fk_country_id (country_id))engine=myisam;Query OK, 0 rows affected (0.05 sec)mysql> CREATE TABLE payment_all( -> country_id smallint, -> payment_date datetime, -> amount decimal(15, 2), -> INDEX(country_id) -> )engine=merge union(payment_2006, payment_2007) INSERT_METHOD=LAST;Query OK, 0 rows affected (0.05 sec)-- 插入数据mysql> INSERT INTO payment_2006 VALUES (1, '2006-05-01', 100000), (2, '2006-08-15', 150000);Query OK, 2 rows affected (0.04 sec)Records: 2 Duplicates: 0 Warnings: 0mysql> INSERT INTO payment_2007 VALUES (1, '2007-02-20', 35000), (2, '2007-07-15', 220000);Query OK, 2 rows affected (0.00 sec)Records: 2 Duplicates: 0 Warnings: 0-- 查询3张表的数据mysql> SELECT * FROM payment_2006;+------------+---------------------+-----------+| country_id | payment_date | amount |+------------+---------------------+-----------+| 1 | 2006-05-01 00:00:00 | 100000.00 || 2 | 2006-08-15 00:00:00 | 150000.00 |+------------+---------------------+-----------+2 rows in set (0.00 sec)mysql> SELECT * FROM payment_2007;+------------+---------------------+-----------+| country_id | payment_date | amount |+------------+---------------------+-----------+| 1 | 2007-02-20 00:00:00 | 35000.00 || 2 | 2007-07-15 00:00:00 | 220000.00 |+------------+---------------------+-----------+2 rows in set (0.00 sec)mysql> SELECT * FROM payment_all;+------------+---------------------+-----------+| country_id | payment_date | amount |+------------+---------------------+-----------+| 1 | 2006-05-01 00:00:00 | 100000.00 || 2 | 2006-08-15 00:00:00 | 150000.00 || 1 | 2007-02-20 00:00:00 | 35000.00 || 2 | 2007-07-15 00:00:00 | 220000.00 |+------------+---------------------+-----------+-- 向MERGE表插入数据,仅仅会作用于最后一张表,即payment_2007mysql> INSERT INTO payment_all VALUES(3, '2014-06-15', 10000);Query OK, 1 row affected (0.00 sec)mysql> SELECT * FROM payment_2006;+------------+---------------------+-----------+| country_id | payment_date | amount |+------------+---------------------+-----------+| 1 | 2006-05-01 00:00:00 | 100000.00 || 2 | 2006-08-15 00:00:00 | 150000.00 |+------------+---------------------+-----------+2 rows in set (0.00 sec)mysql> SELECT * FROM payment_2007;+------------+---------------------+-----------+| country_id | payment_date | amount |+------------+---------------------+-----------+| 1 | 2007-02-20 00:00:00 | 35000.00 || 2 | 2007-07-15 00:00:00 | 220000.00 || 3 | 2014-06-15 00:00:00 | 10000.00 |+------------+---------------------+-----------+3 rows in set (0.00 sec)mysql> SELECT * FROM payment_all;+------------+---------------------+-----------+| country_id | payment_date | amount |+------------+---------------------+-----------+| 1 | 2006-05-01 00:00:00 | 100000.00 || 2 | 2006-08-15 00:00:00 | 150000.00 || 1 | 2007-02-20 00:00:00 | 35000.00 || 2 | 2007-07-15 00:00:00 | 220000.00 || 3 | 2014-06-15 00:00:00 | 10000.00 |+------------+---------------------+-----------+
具体存储引擎细节可参考:
http://dev.mysql.com/doc/refman/5.7/en/storage-engines.html
不吝指正。