在Mysql数据库中,Mysql Merge表有点类似于视图。下面就让我们来一起了解一下Mysql Merge表都有哪些优点,希望对您能有所帮助。
Mysql Merge表的优点:
A: 分离静态的和动态的数据
B:利用结构接近的的数据来优化查询
C: 查询时可以访问更少的数据
D:更容易维护大数据集
E: 可以通过修改.mrg文件来修改Merge表,当然也可以用alter进行修改,修改后要通过FLUSH TABLES刷新表缓存,此法可以动态增加减少子表
创建方法,例:
mysql>CREATE TABLE t1(a INT NOT NULL PRIMARY KEY)ENGINE=MyISAM;
mysql>CREATE TABLE t2(a INT NOT NULL PRIMARY KEY)ENGINE=MyISAM;
mysql>CREATE TABLE mrg(a INT NOT NULL PRIMARY KEY)ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;
下面添加测试数据
mysql>INSERT INTO t1(a) VALUES(1),(2);
mysql>INSERT INTO t2(a)VALUES(1),(2);
查询一下看看结果
mysql>SELECT a FROM mrg;
结果会显示出t1,t2两个表中的数据
+------+
| a |
+------+
| 1|
| 2|
| 1|
| 2|
+------+
对于merge表,需要注意的是
1。每个子表的结构必须一致,主表和子表的结构需要一致,
2。每个子表的索引在merge表中都会存在,所以在merge表中不能根据该索引进行唯一性检索。
3 子表需要是MyISAM引擎
4 REPLACE在merge表中不会工作
5 AUTO_INCREMENT 不会按照你所期望的方式工作。
创建Mysql Merge表的参数 INSERT_METHOD有几个参数 。
LAST 如果你执行insert 指令来操作merge表时,插入操作会把数据添加到最后一个子表中。FIRST 同理,执行插入数据时会把数据添加到第一个子表中。
比如本例,对merge表执行插入操作
mysql>INSERT INTO mrg(a)VALUES(18);
查询一下
mysql>SELECT a FROM t2;
结果你会发现18出现在t2表中。
---------------------------------------------------------------
如果你对mrg表或者子表进行了DROP操作,那将有可能会产生些不可预知的情况。
如果删除mrg表,那么各个子表间将不会有联系。但是如果删除其中的任一子表,对于GNU/LINUX来说,merge表结构及数据仍然存在。
mysql>DROP TABLE t1,t2;
mysql>SELECT a FROM mrg;
结果你会发现mrg表的查询结果不变。
他将多个表在逻辑上当作一个表来查询。他建立后有两个文件,
.frm 表结构定义
.mrg union表的名字清单
--
-- merger表的结构 `test_merge`
--
CREATE TABLE IF NOT EXISTS `test_merge` (
`id` int(5) NOT NULL auto_increment,
`names` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MRG_MyISAM DEFAULT CHARSET=gb2312 INSERT_METHOD=LAST UNION=(`test_merge_1`,`test_merge_2`,`test_merge_3`);
--
-- 导出表中的数据 `test_merge`
--
INSERT INTO `test_merge` (`id`, `names`) VALUES
(1, 'aa'),
(1, 'bb'),
(1, 'cc');
-- --------------------------------------------------------
--
-- 基本表的结构 `test_merge_1`
--
CREATE TABLE IF NOT EXISTS `test_merge_1` (
`id` int(5) NOT NULL auto_increment,
`names` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=3 ;
--
-- 导出表中的数据 `test_merge_1`
--
INSERT INTO `test_merge_1` (`id`, `names`) VALUES
(1, 'aa');
-- --------------------------------------------------------
--
--基本 表的结构 `test_merge_2`
--
CREATE TABLE IF NOT EXISTS `test_merge_2` (
`id` int(5) NOT NULL auto_increment,
`names` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=2 ;
--
-- 导出表中的数据 `test_merge_2`
--
INSERT INTO `test_merge_2` (`id`, `names`) VALUES
(1, 'bb');
-- --------------------------------------------------------
--
-- 基本表的结构 `test_merge_3`
--
CREATE TABLE IF NOT EXISTS `test_merge_3` (
`id` int(5) NOT NULL auto_increment,
`names` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=2 ;
--
-- 导出表中的数据 `test_merge_3`
--
INSERT INTO `test_merge_3` (`id`, `names`) VALUES
(1, 'cc');
1. 此表类似于SQL中的union机制。
2. 此表结构必须与基本表完全一致,包括列名、顺序。UNION表必须同属一个DATABASE。
3. 基本表类型必须是MyISAM。
4. 可以通过修改.mrg文件来修改MERGE表,每个基本表的名字占一行。注意:修改后要通过FLUSH TABLES刷新表缓存。
5. 对基本表的更改可以直接反映在此表上。
6. INSERT_METHOD的取值可以是: 0 不允许插入 FIRST 插入到UNION中的第一个表 LAST 插入到UNION中的最后一个表。(4.0之后可用)
7. 定义在它上面的约束没有任何作用,约束是由基本表控制的,例如两个基本表中存在着同样的一个Key值,那么在MERGE表中会有两个一样的Key值。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

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.

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.

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.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

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