我需要处理一张非常大的表。我想检查是否存在某些记录的parent_id等于我传递的值。 目前,我使用的方法是使用"select count(*) from mytable where parent_id = :id",如果结果> 0,则意味着它们存在。
因为这是一个非常大的表,而且我不在乎存在确切数量的记录,我只想知道它是否存在,所以我认为count(*)效率有点低。
如何以最快的方式实现此要求? 我正在使用Oracle 10。
#根据hibernate Tips&Tricks https://www.hibernate.org/118.html#A2
建议像这样编写:
Integer count = (Integer) session.createQuery("select count(*) from ....").uniqueResult();
我不知道uniqueResult()的魔力在哪里? 它为什么会快?
与"select 1 from mytable where parent_id = passingId and rowrum < 2 "相比,哪个更有效?
select 'Y' from mytable where parent_id = :id AND ROWNUM = 1
可以得到相同的结果。 - Jeffrey Kemp