MySQL 数据库存储引擎_MySQL
简单介绍
存储引擎就是指表的类型。数据库的存储引擎决定了表在计算机中的存储方式。存储引擎的概念是MySQl的特点,而且是一个插入式的存储引擎概念。这 就决定了MySQl数据库中的表可以使用不同的存储方式存储。用户可以根据自己的不同要求,选择不同的存储方式、是否进行事务处理等。
查询方式及内容解析
使用SHOW ENGINES语句可以查看MySQL数据库支持的存储引擎类型。查询方法如下:
SHOW ENGINES;
SHOW ENGUNES语句可以使用“;”结束,也可以使用“\g”或者“\G”结束。“\g”与“;”的作用相同,“\G”可以让结果显示的更加美观。
mysql> SHOW ENGINES\G *************************** 1. row *************************** Engine: MRG_MYISAM Support: YES Comment: Collection of identical MyISAM tables Transactions: NO XA: NO Savepoints: NO *************************** 2. row *************************** Engine: InnoDB Support: DEFAULT Comment: Supports transactions, row-level locking, and foreign keys Transactions: YES XA: YES Savepoints: YES *************************** 3. row *************************** Engine: MyISAM Support: YES Comment: MyISAM storage engine Transactions: NO XA: NO Savepoints: NO ###############中间已省略################### *************************** 8. row *************************** Engine: MEMORY Support: YES Comment: Hash based, stored in memory, useful for temporary tables Transactions: NO XA: NO Savepoints: NO 8 rows in set (0.11 sec)
解析:查询结果中,Engine参数指存储引擎名称;Support参数说明MySQL是否支持该类引擎,YES表示支持;Comment参数指对 该引擎的评论;Transactions 参数表示是否支持事务处理,YES表示支持;XA参数表示是否分布式交易处理XA规范,YES表示支持;Savepoints参数表示是否支持保存点,以 便事务回滚到保存点,YES表示支持。
从查询结果中可以看出,MySQL支持的引擎参数包括MyISAM、MEMORY、InnoDB、ARCHIVE和MRG_MYISAM等。其中InnoDB为默认的存储引擎。可使用语句查询默认的存储引擎此代码如下:
SHOW VARIABLES LIKE 'storage_engine';
代码执行的结果如下:
mysql> SHOW VARIABLES LIKE 'storage_engine';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| storage_engine | InnoDB |
+----------------+--------+
1 row in set (0.10 sec)
解析:结果显示默认的存储引擎为InnoDB。如果想修改默认的存储引擎可以在配置文件my.ini中修改。将”default-storage- engine=InnoDB”更改为”default-storage-engine=MyISAM”。然后重启服务,修改生效。
使用SHOW TABLESTATUS可以查看某个库中所有表支持的存储引擎类型查询方法如下:
mysql> USE hellodb Database changed mysql> SHOW TABLE STATUS\G *************************** 7. row *************************** Name: toc Engine: MyISAM Version: 10 Row_format: Fixed Rows: 0 Avg_row_length: 0 Data_length: 0 Max_data_length: 2533274790395903 Index_length: 1024 Data_free: 0 Auto_increment: 1 Create_time: 2013-08-12 16:17:23 Update_time: 2013-08-12 16:17:23 Check_time: NULL Collation: utf8_general_ci Checksum: NULL Create_options: Comment:
解析:结果显示对于MySQL来说所有表默认都是使用MyISAM存储引擎,其中Name:表名;Engine:使用的存储引擎;Version: 所用存储引擎的版本;Row_format:行格式,对于MyISAM存储引擎来说常见的有Dynamic(变长记录), Fixed(定长记录), Compressed(压缩以后存放的行), compact(InnoDB中常见的);Rows: 表中的行数(对于其他存储引擎来说这个值是估算的);Avg_row_length: 行的平均字节数;Data_length:表的数据量,单位为字节;Max_data_length:表的容量上限(不同的存储引擎容量上限不 同);Index_length:索引数据量,单位字节;Data_free:已经分配出去,但未存储数据的存储空间;Auto_increment:具 有自动增长属性的字段上,下一个自动增长的值;Create_time:表的创建时间;Update_time: 数据最近一次的更新时间;Check_time: 使用CHECK命令最近一次检查表的时间;myisamchk; Checksum: 表的校验和;Create_options:创建表时指定的其它选项;Comment: 对于MyISAM表,存储的是创建表时的comment表选项指定的信息;对InnoDB表来讲,存储对应的表空间剩余的表空间信息。
各种存储引擎特性比较:
在实际工作中,选择一个合适的存储引擎是一个很复杂的问题。每种存储引擎都有各自的优势,不能笼统的说谁比谁更好。下面将详解不同环境经常用到的存储引擎和针对各个存储引擎的特点进行对比,给出不同的选择建议。
InnoDB存储引擎
InnoDB是Mysql数据库的一种存储引擎。InnoDB给Mysql的表提供了 事务、回滚、崩溃修复能力、多版本并发控制的事务安全、间隙锁(可以有效的防止幻读的出现)、支持辅助索引、聚簇索引、自适应hash索引、支持热备、行 级锁。还有InnoDB是Mysql上唯一一个提供了外键约束的引擎。
InnoDB存储引擎中,创建的表的表结构是单独存储的并且存储在.frm文件中。数据和索引存储在一起的并且存储在表空间中。但是默认情况下 mysql会将数据库的所有InnoDB表存储在一个表空间中的。其实这种方式管理起来非常的不方便而且还不支持高级功能所以建议每个表存储为一个表空间 实现方式为:使用服务器变量innodb_file_per_table = 1。
如果需要频繁的进行更新、删除操作的数据库也可选择InnoDB存储引擎。因为该存储引擎可以实现事务提交和回滚。
MyISAM存储引擎
MyISAM存储引擎是Mysql中常见的存储引擎,MyISAM存储引擎是基于ISAM存储引擎发展起来的。MyISAM支持全文索引、压缩存 放、空间索引(空间函数)、表级锁、延迟更新索引键。但是MyISAM不支持事务、行级锁、更无法忍受的是崩溃后不能保证完全恢复(只能手动修复)。
MyISAM存储引擎的表存储成3个文件。文件的名字和表的名字相同。扩展名包含frm、MYD、MYI。其中frm为扩展名的文件存储表的结构;MYD为扩展名的文件存储数据,其是MYData的缩写;MYI为扩展名的文件存储索引,其为MYIndex的缩写。
MyISAM存储引擎的插入数据很快,空间和内存使用比较低。如果表主要是用于插入新记录和读出记录,那么选择MyISAM存储引擎能够实现处理的高效率。如果应用的完整性、并发性要求很低,也可以选择MyISAM存储引擎。
ARCHIVE
ARCHIVE,见名之意可看出是归档,所以归档之后很多的高级功能就不再支持了仅支持插入(insert)和查询(select)两种功能, ARCHIVE存储引擎之前还不支持索引(在Mysql5.5以后开始支持索引了),但是它拥有很好的压缩机制。通常用于做仓库使用。
ARCHIVE存储引擎适用于存储日志信息或其他按时间序列实现的数据采集类的应用场景中。
CSV
CSV是将数据文件保存为CSV格式的的文件的,可以方便的导入到其他数据库中去(例如:excel表格,SQLserver等等),由此需要在数据库间自由共享数据时才偶尔建议使用此存储引擎。并且它也不支持索引;个人认为仅适用于数据交换。
BLACKHOLE
BLACKHOLE叫做黑洞,也就是说没有存储机制,任何数据都会被丢弃,但是会记录二进制日志。一般在Mysql复制(中继服务器)中经常用到,这个在Mysql复制博客中将详细介绍,敬请关注。
FEDERATED
FEDERATED可以实现跨服务器整理表,简单说就是它可以访问远程服务器上数据的存储引擎,所以说它不再本地创建数据只会自动的建立一个连接到其他服务器上链接,有点类似于代理的功能,默认都是禁用的。
MEMORY存储引擎
MEMORY存储引擎是Mysql中的一类特殊的存储引擎。其使用存储在内存中的内存来创建表,而且所有数据保存在内存中。数据安全性很低,但是查 找和插入速度很快。如果内存出现异常就会影响到数据的完整性,如果重启或关机,表中的所有数据就会丢失,因此基于MEMORY存储引擎的表的生命周期很 短,一般都是一次性的。适用于某些特殊场景像查找和映射,缓存周期性的聚合数据等等。
MRG_MYISAM
MRG_MYISAM存储引擎是合并MyISAM表的,就是将多个MyISAM合并为一个(在用户看来是一个进行工作,其实是多个底层物理文件在运行工作)。
扩展一些第三方存储引擎
(1)、OLTP类:
XtraDB:InnoDB的改进版
PBXT:支持复制,外键约束,而且支持在固态存储(SSD硬盘)上快速存取数据,是一个不错的支持事务的存储引擎,但是此存在的bug已不再修复,被弃用。
TokuDB:图库数据库,在存储海量数据的方面有着mysql无法比拟的优势,也有mysql版的,其最大优势支持分形树索引结构,这个结构导致 它和缓存无关也就直接导致了就算索引在数据库文件中放不下也不会影响性能。一般只适用于大量插入数据的分析型场景。(注释:这里的图不是照片等,而是复杂 数据连接的数据结构。)
(2)、列式存储引擎
列式数据库:此种数据库最适合存储大数据,在数据检索上也很好但是在一定程度上需要反关系存储,因此可能无法满足我们关系型数据库范式的概念所以被称为Nosql。
下面介绍几个列式存储引擎(都有两个版本:社区版、商业版):
Infobright:适合于数十TB的大环境中、支持数据压缩,默认情况下mysql不支持列式存储功能需要定制。使用者众多,名气很高。
MonetDB:首先,它的存储模型是对数据从垂直方向进行切分;其次,MonetDB是第一个利用CPU缓存对数据的查询进行优化的数据库系统;此外,MonetDB会自动管理和协调索引机制,优化查询效率。目前使用者不是很多。
InfiniDB:InfiniDB Community Edition (社区版)提供一个可伸缩的分析型数据库引擎,主要为数据仓库、商业智能、以及对实时性要求不严格的应用而开发。基于 MySQL 搭建。包括对查询、事务处理以及大数据量加载的支持。目前使用者不是很多。
LucidDB:是唯一一款专注于数据仓库和商务智能的开源RDBMS,它使用了列存储架构,支持位图索引,哈希连接/聚合和页面级多版本,大部分 数据库最初都注重事务处理能力,而分析功能都是后来才加上去的。相反,LucidDB中的所有组件从一开始就是为满足灵活的需求,高性能数据集成和大规模 数据查询而设计的,此外,其架构设计彻底从用户出发,操作简单。目前使用者不是很多。
(3)、社区存储引擎(了解):
Aria:Maria的下一代版本。
Groona:可以精确的实现全文索引引擎,可以替代MyISAM在索引上的特性。
QQGraph:支持图操作,由Open query研发
Sphinx:外在的服务器能够在Mysql基础上为Mysql构建一个高效的全文索引,通过C++研发,Mysql支持一个社区引擎叫SphinxSE,就是让Sphinx直接支持Mysql接口,在MariaDB5.5.32上被编译支持了。
Spider:支持分片,每一个独立的分片可以实现独立的查询。
VPForMySQL:支持垂直分区,支持更大级别的数据操作更大级别的数据存储。
选择标准:
1、是否需要支持事务??
2、是否需要使用热备??
3、崩溃恢复:能否接受崩溃??
个人建议:
存储日志或按时间增长的数据:MyISAM、ARCHIVE
论坛应用:InnoDB
电商订单:InnoDB
数据量大:Infobright、NoSQL、Sphinx

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
