简体   繁体   English

在 Java 中,多个 select 查询或连接,哪一个是更好的做法?

[英]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:例外情况可能包括:

  • privilege separation - mysql users have different table level access.权限分离 - mysql 用户具有不同的表级别访问权限。
  • locking separation, one table as a particularly high updated field than for a userid that needs to be separate from another table of high userid updates.锁定分离,一个表作为一个特别高的更新字段,而不是一个用户 ID 需要与另一个高用户 ID 更新的表分开。
  • performance - row size - one table has large number of columns that isn't frequently needed, so keep it out of the innodb buffer pool by keeping those columns in a different table.性能 - 行大小 - 一个表有大量不经常需要的列,因此通过将这些列保存在不同的表中,将其排除在 innodb 缓冲池之外。

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.

相关问题 在Java中这是更好的做法 - Which is a better practice in java 两者之间哪一个更好? - which one would be better between these two? 以下哪一项是 java 中更好的编程实践来调用用户定义的 class 中存在的方法? - Which one of the below is a better programming practice in java to call methods present in a user defined class? 哪种更好的Java编程实践:堆叠枚举和枚举构造函数或子类化? - Which is better Java programming practice: stacking enums and enum constructors, or subclassing? 在性能/加载时间上哪个更好? (Java字符串) - Which would be better performance/loading time wise? (Java strings) java中的iterate和for循环中哪一个更好? - which one is better between iterate and for loop in java? 在Java中哪一个更好用于HashMap的getOrDefault()或putIfAbsent() - Which one is better getOrDefault() or putIfAbsent() of HashMap in Java Java中泛型中哪个更好? - which one is better Generics or not Generics in Java? Java Builder模式实现,哪个更好? - Java Builder Pattern implementation, which one is better? MySQL的最佳实践,使用Java Hibernate,将一个大表或更好地划分为多个表 - MySQL better practice, one big table or better divide to separate tables, using Java hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM