您的位置:首页 > 数据库 > MySQL

Mysql分表查询引擎Merge技术总结

2014-10-15 16:33 288 查看
首先说一下Merge这个关键字在Oracle中是怎么运用的,废话不多说,先上一个例子

MERGE INTO t_agent_tem2 s
USING(SELECT * FROM t_agent_info) d
ON(S.id=d.id)
WHEN MATCHED THEN
UPDATE SET s.info_source=1
DELETE WHERE s.status=1
WHEN NOT MATCHED THEN
INSERT (s.id,s.organization_no,s.agent_name,s.info_source,s.status)
VALUES (d.id,d.organization_no,d.agent_name,d.info_source,d.status);


在这里,我做一下简单的说明,这里用表t_agent_info表中查询出来的数据更新t_agent_tem2表,如果这个数据已经在该表中存在,则执行相应的update,如果不存在则执行insert。

接下来进入我这篇文章的主题,在Mysql中,Merge是一个分表引擎。同样,我先上一个例子

create table test_1(id int primary key auto_increment,name varchar(36),type int(2) not null default 1) engine = MyISAM charset=UTF8;
create table test_2(id int primary key auto_increment,name varchar(36),type int(2) not null default 2) engine = MyISAM charset=UTF8;
create table test_3(id int primary key auto_increment,name varchar(36),type int(2) not null default 3) engine = MyISAM charset=UTF8;
create table test(id int primary key auto_increment,name varchar(36),type int(2) not null default 0) engine = MRG_MyISAM  union =(test_1,test_2,test_3) insert_method last charset UTF8;

insert into test_1(name) values('孙悟空');
insert into test_2(name) values('猪八戒');
insert into test_3(name) values('沙悟净');
insert into test(name) values('陈玄奘');

select * from test_1;
select * from test_2;
select * from test_3;
select * from test;



看一下查询结果,想必大家已经看出一些原因了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: