Java Hibernate:加入标准
嗨,我需要使用标准执行以下操作Java Hibernate:加入标准,java,oracle,hibernate,Java,Oracle,Hibernate,嗨,我需要使用标准执行以下操作 Select * from product pd, (Select productID pd1 from ... where ...) tpd1, (Select productID pd2 from ... where ...) tpd2 where pd.productID = tpd1.pd1 and pd.productID = tpd1.pd2 我可以知道这是否可能吗 原始SQL正在条件中使用 Select * from product pd whe
Select * from product pd, (Select productID pd1 from ... where ...) tpd1,
(Select productID pd2 from ... where ...) tpd2
where pd.productID = tpd1.pd1 and pd.productID = tpd1.pd2
我可以知道这是否可能吗
原始SQL正在条件中使用
Select * from product pd where productID in (Select productID pd1 from ... where ...) and
productID in (Select productID pd2 from ... where ...)
但是获得结果需要花费太长时间,使用join SQL语句我能够更快地获得结果
有什么帮助吗?如果您使用的是Hibernate,您可能需要执行类似的操作,如果预期的匹配数相对较低,则应该可以:
select *
from product pd
where pd.productID in
(select productID
from product pd2
join tpd1 on (pd2.productID = tpd1.pd1)
join tpd2 on (pd2.productID = tpd2.pd2)
where tpd1....
and tpd2....
);
我假设product.productID
上有一个唯一的索引
或者,您可以尝试EXISTS公式,该公式可能比原始查询更好,也可能不比原始查询更好:
select *
from product pd
where EXISTS
(select null from tpd1 where pd.productID = tpd1.pd1 and ...)
and EXISTS
(select null from tpd2 where pd.productID = tpd2.pd2 and ...)
;
请发布原始查询的解释计划。我目前是您推荐的具有hibernate条件的解决方案,但由于select语句中的某些语句已经过时,因此非常滞后。我尝试使用SQL优化器,它建议使用联接表。不会加载建议解决方案的大纲视图输入,但会加载联接表查询的大纲视图输入。其中一个原因是因为返回的数据量很大。您确实需要为您的查询发布解释计划-更好的是,对于所有建议的查询。没有计划,我们只能猜测。