您现在的位置是:网站首页> 编程资料编程资料
MySQL表类型 存储引擎 的选择_Mysql_
2023-05-27
764人已围观
简介 MySQL表类型 存储引擎 的选择_Mysql_
1、查看当前数据库支出的存储引擎
方法1:
mysql> show engines \G; *************************** 1. row *************************** Engine: InnoDB Support: YES Comment: Supports transactions, row-level locking, and foreign keys Transactions: YES XA: YES Savepoints: YES *************************** 2. row *************************** Engine: MRG_MYISAM Support: YES Comment: Collection of identical MyISAM tables Transactions: NO XA: NO Savepoints: NO *************************** 3. row *************************** Engine: MEMORY Support: YES Comment: Hash based, stored in memory, useful for temporary tables Transactions: 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: MyISAM Support: DEFAULT Comment: MyISAM storage engine Transactions: NO XA: NO Savepoints: NO *************************** 6. row *************************** Engine: CSV Support: YES Comment: CSV storage engine Transactions: NO XA: NO Savepoints: NO *************************** 7. row *************************** Engine: ARCHIVE Support: YES Comment: Archive storage engine Transactions: NO XA: NO Savepoints: NO *************************** 8. row *************************** Engine: PERFORMANCE_SCHEMA Support: YES Comment: Performance Schema Transactions: NO XA: NO Savepoints: NO *************************** 9. row *************************** Engine: FEDERATED Support: NO Comment: Federated MySQL storage engine Transactions: NULL XA: NULL Savepoints: NULL 9 rows in set (0.00 sec) ERROR: No query specified
方法2:
(Value 显示为“DISABLED”的记录表示支持该存储引擎,但是数据库启动的时候被禁用。)
mysql> show variables like 'have%'; +------------------------+----------+ | Variable_name | Value | +------------------------+----------+ | have_compress | YES | | have_crypt | NO | | have_dynamic_loading | YES | | have_geometry | YES | | have_openssl | DISABLED | | have_profiling | YES | | have_query_cache | YES | | have_rtree_keys | YES | | have_ssl | DISABLED | | have_statement_timeout | YES | | have_symlink | YES | +------------------------+----------+ 11 rows in set, 1 warning (0.00 sec)
2、ENGINE={存储引起类型} 创建表的时候,设置存储引擎
mysql> create table a( -> i bigint(20) not null auto_increment, -> primary key (i) -> ) engine=myisam default charset=gbk; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 3 Current database: test Query OK, 0 rows affected (1.33 sec)
3、alter able tablename engine={存储引起类型} 修改表为其他存储引擎
mysql> alter table a engine=innodb; Query OK, 0 rows affected (1.70 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table a \G; *************************** 1. row *************************** Table: a Create Table: CREATE TABLE `a` ( `i` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`i`) ) ENGINE=InnoDB DEFAULT CHARSET=gbk 1 row in set (0.14 sec)
3.1 常用存储引擎的对比
| 特点 | MyISAM | InnoDB | MEMORY | MERGE | NDB |
| 存储限制 | 有 | 64TB | 有 | 没有 | 有 |
| 事务安全 | 支持 | ||||
| 锁机制 | 表锁 | 行锁 | 表锁 | 表锁 | 表锁 |
| B 树索引 | 支持 | 支持 | 支持 | 支持 | 支持 |
| 哈希索引 | 支持 | 支持 | |||
| 全文索引 | 支持 | ||||
| 集群索引 | 支持 | ||||
| 数据缓存 | 支持 | 支持 | 支持 | ||
| 索引缓存 | 支持 | 支持 | 支持 | 支持 | 支持 |
| 数据可压缩 | 支持 | ||||
| 空间使用 | 低 | 高 | N/A | 低 | 低 |
| 内存使用 | 低 | 高 | 中等 | 低 | 高 |
| 批量插入的速度 | 高 | 低 | 高 | 高 | 高 |
| 支持外键 | 支持 |
3.2 常用存储引擎学习(MyISAM、InnoDB、MEMORY 和 MERGE)
MyISAM:
默认的MySQL存储引擎,不支持事务和外键
优点:访问速度快
每个MyISAM在磁盘上存储成3个文件,其文件名和表名都相同。扩展名分别是:
.frm (存储表定义)
.MYD (MYData,存储数据)
.MYI (MYIndex,存储索引)
(数据文件和索引文件可以放置在不同的目录,平均分布 IO,获得更快的速度。)
InnoDB:
处理效率较差,占用较多的空间用来保留数据和索引
优点:具有提交、回滚、奔溃恢复能力的事务安全、唯一支持外键的存储引擎
自动增长列:InnoDB 表的自动增长列可以手工插入,但是插入的值如果是空或者 0,则实际插入的将是自动增长后的值
mysql> create table autoincre_demo( -> i smallint not null auto_increment, -> name varchar(10),primary key(i) -> )engine=innodb; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 5 Current database: test Query OK, 0 rows affected (1.19 sec) mysql> insert into autoincre_demo values(1,"121"),(0,"dddf"),(null,"fdf"); Query OK, 3 rows affected (0.59 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from autoincre_demo; +---+------+ | i | name | +---+------+ | 1 | 121 | | 2 | dddf | | 3 | fdf | +---+------+ 3 rows in set (0.00 sec)
alter table tabename auto_increment=n 设置自动增长列的初始值(此值默认从1开始)
可以使用 LAST_INSERT_ID()查询当前线程最后插入记录使用的值。如果一次插入了多条记录,那么返回的是第一条记录使用的自动增长值。
下面的例子演示了使用 LAST_INSERT_ID()的情况:
mysql> insert into autoincre_demo(name) values('3'); Query OK, 1 row affected (0.36 sec) mysql> select LAST_INSERT_ID(); +------------------+ | LAST_INSERT_ID() | +------------------+ | 15 | +------------------+ 1 row in set (0.00 sec) mysql> insert into autoincre_demo(name) values('3'),('6'),('323'),('21'); Query OK, 4 rows affected (0.09 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> select LAST_INSERT_ID(); +------------------+ | LAST_INSERT_ID() | +------------------+ | 16 | +------------------+ 1 row in set (0.00 sec) 外键约束:
在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引。
下面是样例数据库中的两个表,country 表是父表,country_id 为主键索引,city 表是子表,country_id 字段对 country 表的 country_id 有外键。
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.86 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 `fk_city_country` FOREIGN KEY (country_id) REFERENCES country (country_id) ON -> DELETE RESTRICT ON UPDATE CASCADE -> )ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (3.22 sec)
在创建索引的时候,可以指定在删除、更新父表时,对子表进行的相应操作,包 RESTRICT、CASCADE、SET NULL 和 NO ACTION
