简体   繁体   English

查询多个数据库的最佳解决方案

[英]query on multiple databases best solution

i have 8 databases with same tables 我有8个具有相同表的数据库

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 我有another table dbcommon ,其中保存了用户消息

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 我的问题是我需要根据发件人ID和收件人ID查询所有带有用户名的邮件

What is the best way to do this on mysql? 在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. 我现在的方法是使用联合将所有db1到db7用户数据组合在一起,然后内部联接到dbcommon中的消息,但是我不确定这是否可以,如果我在选择用户表和联合时有上百万的数据该怎么办? 。

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. 像现在一样,将UNION加入到message表中(但是,正如您观察到的,这可能无法很好地扩展):您可以定义一个VIEW以节省每次都必须显式声明UNION ,但这不会有任何性能上的好处。

  2. If tbluser tables all use the MyISAM storage engine, you can define a table of their union with The MERGE Storage Engine : 如果tbluser表都使用MyISAM存储引擎,则可以使用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. 将其连接到您的message表应该比以前的选项产生明显的性能优势。

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;

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

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