如何在没有连接的情况下查询多个相关表,并使用JDBC获取一致的数据?

时间:2023-02-02 11:09:45

I am creating a process that connects to a given SQL Server database via JDBC, scans every table, and reads any new data that has been inserted or updated since the last run of this process. The owner of the database will not allow this process to do joins on any of the tables due to performance concerns.

我正在创建一个通过JDBC连接到给定SQL Server数据库的进程,扫描每个表,并读取自上次运行此进程以来已插入或更新的任何新数据。由于性能问题,数据库的所有者将不允许此进程在任何表上执行连接。

The scenario in question

有问题的情景

  • Table A is a parent of table B
  • 表A是表B的父表
  • This process reads data from Table A
  • 该过程从表A中读取数据
  • Prior to the process reading Table B, a new parent record is inserted into Table A and new child records are inserted into Table B
  • 在阅读表B的过程之前,将新的父记录插入表A中,并将新的子记录插入表B中
  • When Table B is read, there will be child records present that don't have a corresponding parent record in the result set from the previous read of Table A
  • 当读取表B时,将存在子记录,这些子记录在上一次读取表A的结果集中没有相应的父记录

How can I query multiple related tables, without a join, and get consistent data using JDBC?

如何在没有连接的情况下查询多个相关表,并使用JDBC获取一致的数据?

1 个解决方案

#1


0  

If you cannot do JOINs, you probably then just have to keep track of each retrieved primary key of each parent table and then use those for querying the child rows. It will complexify the queries, and add more strain to the DB, as you're executing more queries vs doing it in a single JOINed query, but it should give the same results.

如果您不能执行JOIN,那么您可能只需要跟踪每个父表的每个检索到的主键,然后使用它们来查询子行。它将使查询复杂化,并为数据库增加更多压力,因为您在单个JOINed查询中执行更多查询,但它应该给出相同的结果。

#1


0  

If you cannot do JOINs, you probably then just have to keep track of each retrieved primary key of each parent table and then use those for querying the child rows. It will complexify the queries, and add more strain to the DB, as you're executing more queries vs doing it in a single JOINed query, but it should give the same results.

如果您不能执行JOIN,那么您可能只需要跟踪每个父表的每个检索到的主键,然后使用它们来查询子行。它将使查询复杂化,并为数据库增加更多压力,因为您在单个JOINed查询中执行更多查询,但它应该给出相同的结果。