目录
1、存储引擎简介
1.1、查询建表语句,默认存储引擎:InnoDB
1.2、查看当前数据库支持的存储引擎
1.3、创建表,并指定存储引擎
2、 存储引擎-InnoDB介绍
2.1、存储引擎特点
3、MyISAM存储引擎
4、Memory存储引擎
5、InnoDB、MyISAM、Memory的区别
6、存储引擎的选择
1、存储引擎简介
1.1、查询建表语句,默认存储引擎:InnoDB
-- 查询建表语句 show create table emp;
1.2、查看当前数据库支持的存储引擎
-- 查看当前数据库支持的存储引擎 show engines;
1.3、创建表,并指定存储引擎
-- 创建表 my_myisam,并指定MYISAM存储引擎 create table my_myisam ( id int, name varchar(10) ) engine = MyISAM; -- 创建表 my_memory,指定MEMORY存储引擎 create table my_memory ( id int, name varchar(10) ) engine = MEMORY;
2、 存储引擎-InnoDB介绍
2.1、存储引擎特点
- 介绍:InnoDB是一种兼顾高可靠性和高性能的通用存储引擎,在MySQL 5.5之后,InnoDB是默认的MySQL存储引擎。
- 特点:DML操作遵循ACID 原子性(Atomicity)、一致性(Consistency)、隔离性(Isolation)和持久性(Durability)模型,支持 事务 ;行级锁,提高并发访问性能;支持 外键 FOREIGN KEY约束,保证数据的完整性和正确性;
- 文件:xxx.ibd:xxx代表的是表名,InnoDB引擎的每张表都会对应这样一个表空间文件,存储该表的表结构(frm、sdi)、数据和索引。参数:innodb_file_per_table
在InnoDB存储引擎中,
.ibd
文件是表空间文件,用于存储表的数据和索引。每个InnoDB表都有一个与之关联的.ibd
文件(除非该表被配置为使用共享表空间)。.ibd
文件通常包含表的元数据、数据和索引信息。当InnoDB存储引擎的
innodb_file_per_table
配置选项被启用时,每个InnoDB表都会有自己独立的.ibd
文件。这样做的好处是,可以更容易地管理单个表的大小和存储位置,例如,可以通过移动或删除.ibd
文件来移动或删除表。与
.ibd
文件相对应的是.frm
文件,它存储了表的元数据(即表结构)。而InnoDB的系统表空间文件通常命名为ibdata1
、ibdata2
等,用于存储undo日志、插入缓冲区、锁信息等。在某些情况下,例如当表损坏或需要优化时,可能需要直接操作
.ibd
文件。但是,直接操作这些文件是危险的,应该谨慎进行,并在操作前备份相关数据。show variables like 'innodb_file_per_table';
C:\ProgramData\MySQL\MySQL Server 8.0\Data
Microsoft Windows [版本 10.0.19045.3996] (c) Microsoft Corporation。保留所有权利。 C:\ProgramData\MySQL\MySQL Server 8.0\Data\mybatis@002dexample>ibd2sdi t_book.ibd ["ibd2sdi" , { "type": 1, "id": 441, "object": { "mysqld_version_id": 80026, "dd_version": 80023, "sdi_version": 80019, "dd_object_type": "Table", "dd_object": { "name": "t_book", "mysql_version_id": 80026, "created": 20231209065441, "last_altered": 20231209065441, "hidden": 1, "options": "avg_row_length=0;encrypt_type=N;key_block_size=0;keys_disabled=0;pack_record=1;stats_auto_recalc=0;stats_sample_pages=0;", "columns": [ { "name": "bid", "type": 16, "is_nullable": false, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 1, "ordinal_position": 1, "char_length": 80, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": true, "default_value_null": false, "srs_id_null": true, "srs_id": 0, "default_value": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "interval_count=0;", "se_private_data": "table_id=1162;", "engine_attribute": "", "secondary_engine_attribute": "", "column_key": 2, "column_type_utf8": "varchar(20)", "elements": [], "collation_id": 255, "is_explicit_collation": false }, { "name": "bname", "type": 16, "is_nullable": true, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 1, "ordinal_position": 2, "char_length": 80, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "interval_count=0;", "se_private_data": "table_id=1162;", "engine_attribute": "", "secondary_engine_attribute": "", "column_key": 1, "column_type_utf8": "varchar(20)", "elements": [], "collation_id": 255, "is_explicit_collation": false }, { "name": "stuid", "type": 16, "is_nullable": true, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 1, "ordinal_position": 3, "char_length": 80, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "interval_count=0;", "se_private_data": "table_id=1162;", "engine_attribute": "", "secondary_engine_attribute": "", "column_key": 4, "column_type_utf8": "varchar(20)", "elements": [], "collation_id": 255, "is_explicit_collation": false }, { "name": "DB_TRX_ID", "type": 10, "is_nullable": false, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 2, "ordinal_position": 4, "char_length": 6, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "", "se_private_data": "table_id=1162;", "engine_attribute": "", "secondary_engine_attribute": "", "column_key": 1, "column_type_utf8": "", "elements": [], "collation_id": 63, "is_explicit_collation": false }, { "name": "DB_ROLL_PTR", "type": 9, "is_nullable": false, "is_zerofill": false, "is_unsigned": false, "is_auto_increment": false, "is_virtual": false, "hidden": 2, "ordinal_position": 5, "char_length": 7, "numeric_precision": 0, "numeric_scale": 0, "numeric_scale_null": true, "datetime_precision": 0, "datetime_precision_null": 1, "has_no_default": false, "default_value_null": true, "srs_id_null": true, "srs_id": 0, "default_value": "", "default_value_utf8_null": true, "default_value_utf8": "", "default_option": "", "update_option": "", "comment": "", "generation_expression": "", "generation_expression_utf8": "", "options": "", "se_private_data": "table_id=1162;", "engine_attribute": "", "secondary_engine_attribute": "", "column_key": 1, "column_type_utf8": "", "elements": [], "collation_id": 63, "is_explicit_collation": false } ], "schema_ref": "mybatis-example", "se_private_id": 1162, "engine": "InnoDB", "last_checked_for_upgrade_version_id": 0, "comment": "", "se_private_data": "", "engine_attribute": "", "secondary_engine_attribute": "", "row_format": 2, "partition_type": 0, "partition_expression": "", "partition_expression_utf8": "", "default_partitioning": 0, "subpartition_type": 0, "subpartition_expression": "", "subpartition_expression_utf8": "", "default_subpartitioning": 0, "indexes": [ { "name": "PRIMARY", "hidden": false, "is_generated": false, "ordinal_position": 1, "comment": "", "options": "flags=0;", "se_private_data": "id=289;root=4;space_id=101;table_id=1162;trx_id=10390;", "type": 1, "algorithm": 2, "is_algorithm_explicit": false, "is_visible": true, "engine": "InnoDB", "engine_attribute": "", "secondary_engine_attribute": "", "elements": [ { "ordinal_position": 1, "length": 80, "order": 2, "hidden": false, "column_opx": 0 }, { "ordinal_position": 2, "length": 4294967295, "order": 2, "hidden": true, "column_opx": 3 }, { "ordinal_position": 3, "length": 4294967295, "order": 2, "hidden": true, "column_opx": 4 }, { "ordinal_position": 4, "length": 4294967295, "order": 2, "hidden": true, "column_opx": 1 }, { "ordinal_position": 5, "length": 4294967295, "order": 2, "hidden": true, "column_opx": 2 } ], "tablespace_ref": "mybatis-example/t_book" }, { "name": "fk_book_stuid", "hidden": false, "is_generated": true, "ordinal_position": 2, "comment": "", "options": "flags=0;", "se_private_data": "id=290;root=5;space_id=101;table_id=1162;trx_id=10390;", "type": 3, "algorithm": 2, "is_algorithm_explicit": false, "is_visible": true, "engine": "InnoDB", "engine_attribute": "", "secondary_engine_attribute": "", "elements": [ { "ordinal_position": 1, "length": 80, "order": 2, "hidden": false, "column_opx": 2 }, { "ordinal_position": 2, "length": 4294967295, "order": 2, "hidden": true, "column_opx": 0 } ], "tablespace_ref": "mybatis-example/t_book" } ], "foreign_keys": [ { "name": "fk_book_stuid", "match_option": 1, "update_rule": 1, "delete_rule": 1, "unique_constraint_name": "PRIMARY", "referenced_table_catalog_name": "def", "referenced_table_schema_name": "mybatis-example", "referenced_table_name": "t_stu", "elements": [ { "column_opx": 2, "ordinal_position": 1, "referenced_column_name": "sid" } ] } ], "check_constraints": [], "partitions": [], "collation_id": 255 } } } , { "type": 2, "id": 106, "object": { "mysqld_version_id": 80026, "dd_version": 80023, "sdi_version": 80019, "dd_object_type": "Tablespace", "dd_object": { "name": "mybatis-example/t_book", "comment": "", "options": "autoextend_size=0;encryption=N;", "se_private_data": "flags=16417;id=101;server_version=80026;space_version=1;state=normal;", "engine": "InnoDB", "engine_attribute": "", "files": [ { "ordinal_position": 1, "filename": ".\\mybatis@002dexample\\t_book.ibd", "se_private_data": "id=101;" } ] } } } ] C:\ProgramData\MySQL\MySQL Server 8.0\Data\mybatis@002dexample>
3、MyISAM存储引擎
- 介绍:MyISAM是MySQL早期的默认存储引擎
- 特点:不支持事务,不支持外键;支持表锁,不支持行锁;访问速度快
- 文件:xxx.sdi:存储表结构信息;xxx.MYD:存储数据;xxx.MYI:存储索引
https://www.json.cn/
4、Memory存储引擎
- 介绍:Memory引擎的表数据时存储在内存中的,由于受到硬件问题、或断电问题的影响,只能将这些表作为临时表或缓存使用。
- 特点:内存存放;hash索引(默认)
- 文件:xxx.sdi:存储表结构信息
5、InnoDB、MyISAM、Memory的区别
6、存储引擎的选择
- InnoDB用于大多数业务场景
- MyISAM用于业务系统中的日志相关的数据,电商当中足迹和评论相关的数据,已经被 MongoDB 取代
- MEMORY用于缓存,已经被 redis 取代