I need to join table from other database and sometimes other server

Viewed 9601

My project has its own database. Also, I use table of users, which is on other database. Two offices have their data on the same server, but third one has its own user table on other server.

So, in lots of queries I need to join either table some_db.users or other_server.some_db.users

What solution would you advise for this scenario?

I use MySQL.

4 Answers

Federated tables are your solution for tables on other servers. They are very slow though if you perform joins on them. If you just want to read data from another database on the same server you can use a view. This way you have all tables virtually in one database and you have to open only one connection in your application.

CREATE
    VIEW `my_db`.`table_name` 
    AS
(SELECT * FROM `other_db`.`table_name`);
Related