简体   繁体   English

识别MySQL MERGE存储引擎中的成员表

[英]Identify member table in MySQL MERGE storage engine

We have MYSQL table of MERGE Storage Engine which merges data from 40 tables (" table01 " through " table40 "). 我们有MERGE Storage Engine的MYSQL表,该表合并了40个表(“ table01 ”至“ table40 ”)中的数据。 Once we run a SELECT query we get all the entries from all the merged MySQL tables. 运行SELECT查询后,我们将从所有合并的MySQL表中获取所有条目。 The structure and behaviour of MERGE table is as expected. MERGE表的结构和行为符合预期。

However it happens that we need to identify which merged table row entry came from or corresponds to which merged table, ie 但是,碰巧我们需要确定哪个合并表行条目来自或对应于哪个合并表,即

  • rowA => " table01 " rowA =>“ table01
  • rowB => " table12 " rowB =>“ table12
  • rowC => " table12 " rowC =>“ table12
  • rowD => " table35 " rowD =>“ table35
  • rowE => " table39 " rowE =>“ table39
  • row... => " table... " 行... =>“ 表...

Is there a way to do this with MySQL MERGE storage engine? 有没有办法使用MySQL MERGE存储引擎来做到这一点?

Not that elegant but you could add an identifying integer column on each of your tables, specify that column with a default value of 1 ,2 (depending on which table it is - you could even chuck in a lookup table (tableId --> tableName) and then you should be able to tell from which table a given row came. 不太好,但是您可以在每个表上添加一个标识整数列,指定该列的默认值为1(2)(取决于它是哪个表-您甚至可以查表(tableId-> tableName ),那么您应该能够知道给定行来自哪个表。

You'd have to make the same changes to the MERGE table too of course... 当然,您还必须对MERGE表进行相同的更改...

Lovingly ripped off the MERGE MySQL docs : 亲切地撕下MERGE MySQL文档

CREATE TABLE t1 (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,message CHAR(20)) ENGINE=MyISAM;

CREATE TABLE t2 (a INT NOT NULL AUTO_INCREMENT PRIMARY KEY,message CHAR(20)) ENGINE=MyISAM;

INSERT INTO t1 (message) VALUES ('Testing'),('table'),('t1');
INSERT INTO t2 (message) VALUES ('Testing'),('table'),('t2');

CREATE TABLE total (a INT NOT NULL AUTO_INCREMENT,message CHAR(20),INDEX(a)) ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;

alter table t1 add column identifier int unsigned not null default 1;
alter table t2 add column identifier int unsigned not null default 2;

drop table total;

CREATE TABLE total (a INT NOT NULL AUTO_INCREMENT,message CHAR(20),identifier int unsigned,INDEX(a)) ENGINE=MERGE UNION=(t1,t2) INSERT_METHOD=LAST;

You will have the rather dull task of altering all 40 tables though... 您将有一个相当乏味的任务,尽管要更改所有40个表...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM