【发布时间】:2010-01-25 09:03:42
【问题描述】:
我有一张大桌子可以使用。我想检查是否有一些记录的 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
【问题讨论】: