سؤال

I want to replicate certain table from one database into another database in the same server. This tables contain exactly the same fields.

I was considering to use MySQL Replication to replicate that table but some people said that it will increase IO so i find another way to create 3 Trigger (Insert, update and Delete) that will perform exactly the same thing like what i expect.

My Question is, which way is better? Is it using MySQL replication is better even though it's in the same server or using Trigger to replicate the data is better.

Thanks.

هل كانت مفيدة؟

المحلول 2

Assuming you have two databases on the same server i.e DB1 and DB2 and the table is called tbl1 and it is sitting in DB1 you can query the table like this:

USE DB1;
SELECT * FROM tbl1;

USE DB2;
SELECT * FROM DB1.tbl1;

This way you wont need to copy the data and worry about extra space and extra code. You can query a table in another database on the same server. Replication and triggers are not your answer here. You could also create a view to encapsulate the SQL statement.

نصائح أخرى

I don't know what is your goal, but I got mine getting use of the VIEW functionality.

I had two different applications with separate databases but in the same Mysql server. Application2 needed to get a few data from Application1. In general, this is a trivial situation that you can handle with USE DB1; or USE DB2; as your needing, but my programming framework does not work very well with multiple DBs.

So, lets see my solution...

Here is my select query to retrieve this data:

SELECT id, name FROM DB1.customers;

So, using DB2 as default schema, I've created a VIEW:

USE DB2;
CREATE VIEW app1_customers AS SELECT id, name FROM DB1.customers;

Now I can retrieve this data in DB2 as a regular table with a regular SELECT statement.

SELECT * FROM DB2.app1_customers;

Hope ts useful. BR

Definitely triggers is the way to go. Having another server (slave) will need to spare several MB for installation, logs, cpu and memory usage.

I'd use triggers to keep both tables equal. If you want to create a table with the same columns definition and data use:

USE db2;
CREATE TABLE t1 AS SELECT * FROM db1.t1;

After that, go ahead and create the triggers for Update, Insert and Delete statemetns.

Also you could ALTER the new table to a different engine like MEMORY or add indexes to see if you can improve something.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top