[英]In Java, mutiple select queries or join, which one would be a better practice?
I have five tables in MySQL database, each of them has the one-to-one relationship with the User table that stores the username and password, there is a user_id field in User table as the foreign key for all the other tables.我在 MySQL 数据库中有五个表,每个表都与存储用户名和密码的 User 表具有一对一的关系,User 表中有一个 user_id 字段作为所有其他表的外键。
If I need to implement a "Login" function with Java, to get all data of a certain user from all these five tables, would it be a good practice to use JOIN to query these five tables at once?如果我需要用 Java 实现“登录”function,以从所有这五个表中获取某个用户的所有数据,那么使用 JOIN 一次查询这五个表是一个好习惯吗?
I also have another question, would it be a better choice to combine these five tables together as one single table as they have one-to-one relationship?我还有一个问题,将这五个表合并为一个表会更好吗,因为它们是一对一的关系?
As general principle, 1 to 1 relationships should be in the same table.作为一般原则,一对一的关系应该在同一个表中。
Exceptions can include:例外情况可能包括:
Idea behind creating multiple tables with foreign key reference is to support one-to-many relationship.使用外键引用创建多个表背后的想法是支持一对多关系。 Since your table data has one-to-one relationship, there is no point creating multiple table if number of columns including all tables, having one-to-one relationship, is less than Mysql table column count limit.
由于您的表数据具有一对一关系,因此如果包含所有表的列数(具有一对一关系)小于 Mysql 表列数限制,则创建多个表没有意义。
Incase your business requirement doesn't allow to change the current schema, you can go for join to get all the data in single network call rather than making multiple network call to get data as network call latency is not predictable.如果您的业务需求不允许更改当前架构,您可以 go 加入以在单个网络调用中获取所有数据,而不是通过多次网络调用来获取数据,因为网络调用延迟是不可预测的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.