search
HomeDatabaseMysql Tutorialmysql存储引擎(二)

mysql存储引擎(二)

Jun 07, 2016 pm 02:51 PM
memorymysqlstorageengine

mysql存储引擎(二) mysql存储引擎二 MEMORY MERGE BerkeleyDB存储引擎 MEMORY MEMORY存储引擎通过采用内存中的内容来创建表。每个Memory表实际上和一个磁盘文件关联起来,文件名采用”表名.frm”的格式。Memory类型的表访问速度极快,因为数据源来自内存,

mysql存储引擎(二)


      • mysql存储引擎二
        • MEMORY
        • MERGE
        • BerkeleyDB存储引擎

MEMORY

MEMORY存储引擎通过采用内存中的内容来创建表。每个Memory表实际上和一个磁盘文件关联起来,文件名采用”表名.frm”的格式。Memory类型的表访问速度极快,因为数据源来自内存,所以数据库关闭时,内存中的数据就会发生丢失。默认使用Hash索引。

<code class=" hljs asciidoc">mysql> create table memory<span class="hljs-emphasis">_table( id int primary key, name varchar(20) )engine=memory;
Query OK, 0 rows affected (0.02 sec)

</span>mysql> insert into memory<span class="hljs-emphasis">_table(id,name) values(2,'frank');
Query OK, 1 row affected (0.00 sec)

</span><span class="hljs-header">mysql> select * from memory_table;
+----+-----------+</span>
<span class="hljs-header">| id | name      |
+----+-----------+</span>
|  1 | frankstar |
<span class="hljs-header">|  2 | frank     |
+----+-----------+</span>
2 rows in set (0.00 sec)

mysql> show table status like <span class="hljs-emphasis">'memory_table'</span> \G;
<span class="hljs-bullet">*************************** </span>1. row ***************************
<span class="hljs-code">           Name: memory_table</span>
<span class="hljs-code">         Engine: MEMORY</span>
<span class="hljs-code">        Version: 10</span>
<span class="hljs-code">     Row_format: Fixed</span>
<span class="hljs-code">           Rows: 2</span>
<span class="hljs-code"> Avg_row_length: 66</span>
<span class="hljs-code">    Data_length: 127008</span>
Max<span class="hljs-emphasis">_data_</span>length: 12582900
<span class="hljs-code">   Index_length: 126992</span>
<span class="hljs-code">      Data_free: 0</span>
<span class="hljs-code"> Auto_increment: NULL</span>
<span class="hljs-code">    Create_time: 2016-05-09 22:23:47</span>
<span class="hljs-code">    Update_time: NULL</span>
<span class="hljs-code">     Check_time: NULL</span>
<span class="hljs-code">      Collation: utf8_bin</span>
<span class="hljs-code">       Checksum: NULL</span>
<span class="hljs-code"> Create_options:</span>
<span class="hljs-code">        Comment:</span>
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> show index from memory<span class="hljs-emphasis">_table \G;
*************************** 1. row ***************************
        Table: memory_</span>table
<span class="hljs-code">   Non_unique: 0</span>
<span class="hljs-code">     Key_name: PRIMARY</span>
<span class="hljs-code"> Seq_in_index: 1</span>
<span class="hljs-code">  Column_name: id</span>
<span class="hljs-code">    Collation: NULL</span>
<span class="hljs-code">  Cardinality: 2</span>
<span class="hljs-code">     Sub_part: NULL</span>
<span class="hljs-code">       Packed: NULL</span>
<span class="hljs-code">         Null:</span>
<span class="hljs-code">   Index_type: HASH</span>
<span class="hljs-code">      Comment:</span>
Index<span class="hljs-emphasis">_comment:
1 row in set (0.00 sec)

</span>ERROR:
No query specified
</code>

memory表的内存储存在内存中,如果表的数据很大,那么服务器将会自动将其转换为磁盘表,阀值由temp_table_size系统变量来确定。每个memory表的容量由max_heap_table_size变量的值控制。默认16MB。
主要用于数据内容变化不频繁的代码表及访问速度要求较高、数据量不大的场合,同时需要考虑更新操作数据不回写入到磁盘文件中。

MERGE

它实际上是一组myisam表的组合,将一组结构相同的MyISAM表组合在一起,MERGE表本身没有数据,对于该类型表的插入操作,是通过INSERT_METHOD定义完成的,取值为LAST或者为FIRST,FIRST意味着数据增加到组合表中的第一个myisam表中,同理LAST意味着添加到最后一个表中。所以MERGE表的文件有2个,一个是.frm文件,用于存放数据,还有一个MRG文件,用于存放MERGE表的名称,包括其组成表。

如下:

<code class=" hljs haml">mysql> create table myisam_table1(
    -<span class="ruby">> id int primary key,
</span>    -<span class="ruby">> data datetime
</span>    -<span class="ruby">> )engine=myisam;
</span>Query OK, 0 rows affected (0.02 sec)

create table myisam_table2( id int primary key, data datetime )engine=myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> create table table1_merge_table2(
    -<span class="ruby">> id int primary key,
</span>    -<span class="ruby">> data datetime
</span>    -<span class="ruby">> )engine=merge union=(myisam_table1,myisam_table2) insert_method=first;
</span>Query OK, 0 rows affected (0.01 sec)</code>

向2个字表分别添加数据,如下:

<code class=" hljs cs">mysql> insert <span class="hljs-keyword">into</span> myisam_table1 values(<span class="hljs-number">1</span>,<span class="hljs-string">'2016-5-7'</span>);
Query OK, <span class="hljs-number">1</span> row affected (<span class="hljs-number">0.00</span> sec)

mysql> insert <span class="hljs-keyword">into</span> myisam_table1 values(<span class="hljs-number">2</span>,<span class="hljs-string">'2016-5-6'</span>);
Query OK, <span class="hljs-number">1</span> row affected (<span class="hljs-number">0.00</span> sec)

mysql> insert <span class="hljs-keyword">into</span> myisam_table2 values(<span class="hljs-number">1</span>,<span class="hljs-string">'2016-5-7'</span>);
Query OK, <span class="hljs-number">1</span> row affected (<span class="hljs-number">0.00</span> sec)

mysql> insert <span class="hljs-keyword">into</span> myisam_table2 values(<span class="hljs-number">2</span>,<span class="hljs-string">'2016-5-6'</span>);
Query OK, <span class="hljs-number">1</span> row affected (<span class="hljs-number">0.00</span> sec)
</code>

查询merge表,如下:

<code class=" hljs asciidoc"><span class="hljs-header">mysql> select * from table1_merge_table2;
+----+---------------------+</span>
<span class="hljs-header">| id | data                |
+----+---------------------+</span>
|  1 | 2016-05-07 00:00:00 |
|  2 | 2016-05-06 00:00:00 |
|  1 | 2016-05-07 00:00:00 |
<span class="hljs-header">|  2 | 2016-05-06 00:00:00 |
+----+---------------------+</span>
4 rows in set (0.01 sec)</code>

向merge表中添加一条数据,如下:

<code class=" hljs asciidoc">mysql> insert into table1<span class="hljs-emphasis">_merge_</span>table2 values(3,<span class="hljs-emphasis">'2016-5-8'</span>);
Query OK, 1 row affected (0.00 sec)

<span class="hljs-header">mysql> select * from table1_merge_table2;
+----+---------------------+</span>
<span class="hljs-header">| id | data                |
+----+---------------------+</span>
|  1 | 2016-05-07 00:00:00 |
|  2 | 2016-05-06 00:00:00 |
|  3 | 2016-05-08 00:00:00 |
|  1 | 2016-05-07 00:00:00 |
<span class="hljs-header">|  2 | 2016-05-06 00:00:00 |
+----+---------------------+</span>
5 rows in set (0.00 sec)


<span class="hljs-header">mysql> select * from myisam_table1;
+----+---------------------+</span>
<span class="hljs-header">| id | data                |
+----+---------------------+</span>
|  1 | 2016-05-07 00:00:00 |
|  2 | 2016-05-06 00:00:00 |
<span class="hljs-header">|  3 | 2016-05-08 00:00:00 |
+----+---------------------+</span>
3 rows in set (0.00 sec)

<span class="hljs-header">mysql> select * from myisam_table2;
+----+---------------------+</span>
<span class="hljs-header">| id | data                |
+----+---------------------+</span>
|  1 | 2016-05-07 00:00:00 |
<span class="hljs-header">|  2 | 2016-05-06 00:00:00 |
+----+---------------------+</span>
2 rows in set (0.00 sec)</code>

INSERT_METHOD的指定起作用了,如果没有指定,那么当试图往Merge表中insert数据时,都会发生错误。通常使用merge表来透明的对多个表进行查询和更新。

BerkeleyDB存储引擎

简称BDB,创建该类型的表时,会有2个数据文件,一个.frm文件存储表元数据,另一个.db文件存储数据和索引文件,类似innodb。它的实现事务安全有redo日志。在每次启动的时候,都会做一次检查操作,将所有的redo日志清空。它和Memory引擎一样,都是页级锁定。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software