简体   繁体   中英

query on multiple databases best solution

i have 8 databases with same tables

db0
  tbluser |  id  |    name

db1
  tbluser |  id  |    name

db2
  tbluser

db3
   tbluser |  id  |    name

db4
   tbluser |  id  |    name

db5       
   tbluser |  id  |    name

db6       
   tbluser |  id  |    name

db7
   tbluser |  id  |    name

i have another table dbcommon where user messages saved

dbcommon
   message  |  id  |  sender_id  |  recipient_id

My problem is that I need to query all messages with user name in it based on sender id and recipient id

What is the best way to do this on mysql?

My approach now is to combine all db1 to db7 user data using union then inner join to message in dbcommon but i'm not sure if that would be ok, what if i have million of data on user table select and union would be a trouble.

To better understand my question. I made this but I will not use it.

select *, sender.nickname as sender_name, recipient.nickname as recipient_name
from dbcommon.message m
inner join
(
select * from db0.tbluser
union
select * from db1.tbluser
union
select * from db2.tbluser
union
select * from db3.tbluser
union
select * from db4.tbluser
union
select * from db5.tbluser
union
select * from db6.tbluser
union
select * from db7.tbluser
) as sender on m.sender_id = sender.id
inner join
(
select * from db0.tbluser
union
select * from db1.tbluser
union
select * from db2.tbluser
union
select * from db3.tbluser
union
select * from db4.tbluser
union
select * from db5.tbluser
union
select * from db6.tbluser
union
select * from db7.tbluser
) as recipient on m.recipient_id = recipient.id

Whilst I really think that you should normalise your schema such that all these tables are combined into a single one (perhaps with a suitable, indexed as desired, column that indicates the original database); I think that with the status quo, you have only two options:

  1. Join a UNION to the message table, as you are currently doing (but, as you observe, this may not scale very well): you could define a VIEW to save on having to explicitly state the UNION each time, but this won't have any performance benefit.

  2. If tbluser tables all use the MyISAM storage engine, you can define a table of their union with The MERGE Storage Engine :

     CREATE TABLE dbcommon.tblusers LIKE db0.tbluser; ALTER TABLE dbcommon.tblusers ENGINE = MERGE UNION = ( db0.tbluser, db1.tbluser, db2.tbluser, db3.tbluser, db4.tbluser, db5.tbluser, db6.tbluser, db7.tbluser ) ; 

    Joining this to your message table should yield significant performance benefit over the previous option.

you can use like this: (but this is not a good practice to have multiple database for same.

select d1.* from db1.dbcommon d1 inner join db2.tbluser d2 on d1.sender_id = d2.id 
UNION
select d1.* from db1.dbcommon d1 inner join db2.tbluser d2 on d1.sender_id = d2.id

Use this:

 SELECT NOME_USER, NOME_MAQ 
 FROM anapula.utilizador as a
    INNER JOIN cyber.maquinas as b ON a.ID_USER = b.COD_USER;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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