I have two similar large tables with below table structure
CREATE TABLE tableA (
col1 varchar(20) NOT NULL,
col2 varchar(40) NOT NULL,
date date NOT NULL,
time time NOT NULL,
PRIMARY KEY (col1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE tableB (
col1 varchar(20) NOT NULL,
col2 varchar(40) NOT NULL,
date date NOT NULL,
time time NOT NULL,
PRIMARY KEY (col1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to create one large table merging above two tables with the latest record
CREATE TABLE combine_table (
col1 varchar(20) NOT NULL,
col2 varchar(40) NOT NULL,
date date NOT NULL,
time time NOT NULL,
PRIMARY KEY (col1)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Some data may duplicated in table A and B.
How can I achieve this.
Note that these tables are very large
Best Answer
Since you have very much data at hand, I suggest you merge your date and time columns first. Then you can use an index efficiently. If you don't, you will have to do something like
So, first do this for both tables.
Then you can insert both tables into your combine_table (Note, left this for completeness, the second option is much better).
Nonetheless, this will be a heavy operation, if you really have that much data.
Now that I think of it, there's a better way of doing it.
First insert tableA
Then do an
insert on duplicate key update
.