当前位置:   article > 正文

MySQL高级篇(存储引擎InnoDB、MyISAM、Memory)

MySQL高级篇(存储引擎InnoDB、MyISAM、Memory)

目录

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

  1. -- 查询建表语句
  2. show create table emp;

1.2、查看当前数据库支持的存储引擎

  1. -- 查看当前数据库支持的存储引擎
  2. show engines;

1.3、创建表,并指定存储引擎

  1. -- 创建表 my_myisam,并指定MYISAM存储引擎
  2. create table my_myisam
  3. (
  4. id int,
  5. name varchar(10)
  6. ) engine = MyISAM;
  7. -- 创建表 my_memory,指定MEMORY存储引擎
  8. create table my_memory
  9. (
  10. id int,
  11. name varchar(10)
  12. ) 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的系统表空间文件通常命名为ibdata1ibdata2等,用于存储undo日志、插入缓冲区、锁信息等。

    在某些情况下,例如当表损坏或需要优化时,可能需要直接操作.ibd文件。但是,直接操作这些文件是危险的,应该谨慎进行,并在操作前备份相关数据。

show variables like 'innodb_file_per_table';

C:\ProgramData\MySQL\MySQL Server 8.0\Data

  1. Microsoft Windows [版本 10.0.19045.3996]
  2. (c) Microsoft Corporation。保留所有权利。
  3. C:\ProgramData\MySQL\MySQL Server 8.0\Data\mybatis@002dexample>ibd2sdi t_book.ibd
  4. ["ibd2sdi"
  5. ,
  6. {
  7. "type": 1,
  8. "id": 441,
  9. "object":
  10. {
  11. "mysqld_version_id": 80026,
  12. "dd_version": 80023,
  13. "sdi_version": 80019,
  14. "dd_object_type": "Table",
  15. "dd_object": {
  16. "name": "t_book",
  17. "mysql_version_id": 80026,
  18. "created": 20231209065441,
  19. "last_altered": 20231209065441,
  20. "hidden": 1,
  21. "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;",
  22. "columns": [
  23. {
  24. "name": "bid",
  25. "type": 16,
  26. "is_nullable": false,
  27. "is_zerofill": false,
  28. "is_unsigned": false,
  29. "is_auto_increment": false,
  30. "is_virtual": false,
  31. "hidden": 1,
  32. "ordinal_position": 1,
  33. "char_length": 80,
  34. "numeric_precision": 0,
  35. "numeric_scale": 0,
  36. "numeric_scale_null": true,
  37. "datetime_precision": 0,
  38. "datetime_precision_null": 1,
  39. "has_no_default": true,
  40. "default_value_null": false,
  41. "srs_id_null": true,
  42. "srs_id": 0,
  43. "default_value": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
  44. "default_value_utf8_null": true,
  45. "default_value_utf8": "",
  46. "default_option": "",
  47. "update_option": "",
  48. "comment": "",
  49. "generation_expression": "",
  50. "generation_expression_utf8": "",
  51. "options": "interval_count=0;",
  52. "se_private_data": "table_id=1162;",
  53. "engine_attribute": "",
  54. "secondary_engine_attribute": "",
  55. "column_key": 2,
  56. "column_type_utf8": "varchar(20)",
  57. "elements": [],
  58. "collation_id": 255,
  59. "is_explicit_collation": false
  60. },
  61. {
  62. "name": "bname",
  63. "type": 16,
  64. "is_nullable": true,
  65. "is_zerofill": false,
  66. "is_unsigned": false,
  67. "is_auto_increment": false,
  68. "is_virtual": false,
  69. "hidden": 1,
  70. "ordinal_position": 2,
  71. "char_length": 80,
  72. "numeric_precision": 0,
  73. "numeric_scale": 0,
  74. "numeric_scale_null": true,
  75. "datetime_precision": 0,
  76. "datetime_precision_null": 1,
  77. "has_no_default": false,
  78. "default_value_null": true,
  79. "srs_id_null": true,
  80. "srs_id": 0,
  81. "default_value": "",
  82. "default_value_utf8_null": true,
  83. "default_value_utf8": "",
  84. "default_option": "",
  85. "update_option": "",
  86. "comment": "",
  87. "generation_expression": "",
  88. "generation_expression_utf8": "",
  89. "options": "interval_count=0;",
  90. "se_private_data": "table_id=1162;",
  91. "engine_attribute": "",
  92. "secondary_engine_attribute": "",
  93. "column_key": 1,
  94. "column_type_utf8": "varchar(20)",
  95. "elements": [],
  96. "collation_id": 255,
  97. "is_explicit_collation": false
  98. },
  99. {
  100. "name": "stuid",
  101. "type": 16,
  102. "is_nullable": true,
  103. "is_zerofill": false,
  104. "is_unsigned": false,
  105. "is_auto_increment": false,
  106. "is_virtual": false,
  107. "hidden": 1,
  108. "ordinal_position": 3,
  109. "char_length": 80,
  110. "numeric_precision": 0,
  111. "numeric_scale": 0,
  112. "numeric_scale_null": true,
  113. "datetime_precision": 0,
  114. "datetime_precision_null": 1,
  115. "has_no_default": false,
  116. "default_value_null": true,
  117. "srs_id_null": true,
  118. "srs_id": 0,
  119. "default_value": "",
  120. "default_value_utf8_null": true,
  121. "default_value_utf8": "",
  122. "default_option": "",
  123. "update_option": "",
  124. "comment": "",
  125. "generation_expression": "",
  126. "generation_expression_utf8": "",
  127. "options": "interval_count=0;",
  128. "se_private_data": "table_id=1162;",
  129. "engine_attribute": "",
  130. "secondary_engine_attribute": "",
  131. "column_key": 4,
  132. "column_type_utf8": "varchar(20)",
  133. "elements": [],
  134. "collation_id": 255,
  135. "is_explicit_collation": false
  136. },
  137. {
  138. "name": "DB_TRX_ID",
  139. "type": 10,
  140. "is_nullable": false,
  141. "is_zerofill": false,
  142. "is_unsigned": false,
  143. "is_auto_increment": false,
  144. "is_virtual": false,
  145. "hidden": 2,
  146. "ordinal_position": 4,
  147. "char_length": 6,
  148. "numeric_precision": 0,
  149. "numeric_scale": 0,
  150. "numeric_scale_null": true,
  151. "datetime_precision": 0,
  152. "datetime_precision_null": 1,
  153. "has_no_default": false,
  154. "default_value_null": true,
  155. "srs_id_null": true,
  156. "srs_id": 0,
  157. "default_value": "",
  158. "default_value_utf8_null": true,
  159. "default_value_utf8": "",
  160. "default_option": "",
  161. "update_option": "",
  162. "comment": "",
  163. "generation_expression": "",
  164. "generation_expression_utf8": "",
  165. "options": "",
  166. "se_private_data": "table_id=1162;",
  167. "engine_attribute": "",
  168. "secondary_engine_attribute": "",
  169. "column_key": 1,
  170. "column_type_utf8": "",
  171. "elements": [],
  172. "collation_id": 63,
  173. "is_explicit_collation": false
  174. },
  175. {
  176. "name": "DB_ROLL_PTR",
  177. "type": 9,
  178. "is_nullable": false,
  179. "is_zerofill": false,
  180. "is_unsigned": false,
  181. "is_auto_increment": false,
  182. "is_virtual": false,
  183. "hidden": 2,
  184. "ordinal_position": 5,
  185. "char_length": 7,
  186. "numeric_precision": 0,
  187. "numeric_scale": 0,
  188. "numeric_scale_null": true,
  189. "datetime_precision": 0,
  190. "datetime_precision_null": 1,
  191. "has_no_default": false,
  192. "default_value_null": true,
  193. "srs_id_null": true,
  194. "srs_id": 0,
  195. "default_value": "",
  196. "default_value_utf8_null": true,
  197. "default_value_utf8": "",
  198. "default_option": "",
  199. "update_option": "",
  200. "comment": "",
  201. "generation_expression": "",
  202. "generation_expression_utf8": "",
  203. "options": "",
  204. "se_private_data": "table_id=1162;",
  205. "engine_attribute": "",
  206. "secondary_engine_attribute": "",
  207. "column_key": 1,
  208. "column_type_utf8": "",
  209. "elements": [],
  210. "collation_id": 63,
  211. "is_explicit_collation": false
  212. }
  213. ],
  214. "schema_ref": "mybatis-example",
  215. "se_private_id": 1162,
  216. "engine": "InnoDB",
  217. "last_checked_for_upgrade_version_id": 0,
  218. "comment": "",
  219. "se_private_data": "",
  220. "engine_attribute": "",
  221. "secondary_engine_attribute": "",
  222. "row_format": 2,
  223. "partition_type": 0,
  224. "partition_expression": "",
  225. "partition_expression_utf8": "",
  226. "default_partitioning": 0,
  227. "subpartition_type": 0,
  228. "subpartition_expression": "",
  229. "subpartition_expression_utf8": "",
  230. "default_subpartitioning": 0,
  231. "indexes": [
  232. {
  233. "name": "PRIMARY",
  234. "hidden": false,
  235. "is_generated": false,
  236. "ordinal_position": 1,
  237. "comment": "",
  238. "options": "flags=0;",
  239. "se_private_data": "id=289;root=4;space_id=101;table_id=1162;trx_id=10390;",
  240. "type": 1,
  241. "algorithm": 2,
  242. "is_algorithm_explicit": false,
  243. "is_visible": true,
  244. "engine": "InnoDB",
  245. "engine_attribute": "",
  246. "secondary_engine_attribute": "",
  247. "elements": [
  248. {
  249. "ordinal_position": 1,
  250. "length": 80,
  251. "order": 2,
  252. "hidden": false,
  253. "column_opx": 0
  254. },
  255. {
  256. "ordinal_position": 2,
  257. "length": 4294967295,
  258. "order": 2,
  259. "hidden": true,
  260. "column_opx": 3
  261. },
  262. {
  263. "ordinal_position": 3,
  264. "length": 4294967295,
  265. "order": 2,
  266. "hidden": true,
  267. "column_opx": 4
  268. },
  269. {
  270. "ordinal_position": 4,
  271. "length": 4294967295,
  272. "order": 2,
  273. "hidden": true,
  274. "column_opx": 1
  275. },
  276. {
  277. "ordinal_position": 5,
  278. "length": 4294967295,
  279. "order": 2,
  280. "hidden": true,
  281. "column_opx": 2
  282. }
  283. ],
  284. "tablespace_ref": "mybatis-example/t_book"
  285. },
  286. {
  287. "name": "fk_book_stuid",
  288. "hidden": false,
  289. "is_generated": true,
  290. "ordinal_position": 2,
  291. "comment": "",
  292. "options": "flags=0;",
  293. "se_private_data": "id=290;root=5;space_id=101;table_id=1162;trx_id=10390;",
  294. "type": 3,
  295. "algorithm": 2,
  296. "is_algorithm_explicit": false,
  297. "is_visible": true,
  298. "engine": "InnoDB",
  299. "engine_attribute": "",
  300. "secondary_engine_attribute": "",
  301. "elements": [
  302. {
  303. "ordinal_position": 1,
  304. "length": 80,
  305. "order": 2,
  306. "hidden": false,
  307. "column_opx": 2
  308. },
  309. {
  310. "ordinal_position": 2,
  311. "length": 4294967295,
  312. "order": 2,
  313. "hidden": true,
  314. "column_opx": 0
  315. }
  316. ],
  317. "tablespace_ref": "mybatis-example/t_book"
  318. }
  319. ],
  320. "foreign_keys": [
  321. {
  322. "name": "fk_book_stuid",
  323. "match_option": 1,
  324. "update_rule": 1,
  325. "delete_rule": 1,
  326. "unique_constraint_name": "PRIMARY",
  327. "referenced_table_catalog_name": "def",
  328. "referenced_table_schema_name": "mybatis-example",
  329. "referenced_table_name": "t_stu",
  330. "elements": [
  331. {
  332. "column_opx": 2,
  333. "ordinal_position": 1,
  334. "referenced_column_name": "sid"
  335. }
  336. ]
  337. }
  338. ],
  339. "check_constraints": [],
  340. "partitions": [],
  341. "collation_id": 255
  342. }
  343. }
  344. }
  345. ,
  346. {
  347. "type": 2,
  348. "id": 106,
  349. "object":
  350. {
  351. "mysqld_version_id": 80026,
  352. "dd_version": 80023,
  353. "sdi_version": 80019,
  354. "dd_object_type": "Tablespace",
  355. "dd_object": {
  356. "name": "mybatis-example/t_book",
  357. "comment": "",
  358. "options": "autoextend_size=0;encryption=N;",
  359. "se_private_data": "flags=16417;id=101;server_version=80026;space_version=1;state=normal;",
  360. "engine": "InnoDB",
  361. "engine_attribute": "",
  362. "files": [
  363. {
  364. "ordinal_position": 1,
  365. "filename": ".\\mybatis@002dexample\\t_book.ibd",
  366. "se_private_data": "id=101;"
  367. }
  368. ]
  369. }
  370. }
  371. }
  372. ]
  373. 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 取代
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/441499
推荐阅读
相关标签
  

闽ICP备14008679号