If we use the WHERE clause, the database may have to scan the entire table to count records matching our criteria, but we can limit the search to only one record, just say nothing.
If there werenβt any search filters, the previous query would be valid, since the database would do some optimization using the index.
therefore, I assume that the following query will increase some performance over the previous one:
boolean exists = session.createQuery("from PersistentEntity where ...").setMaxResults(1).uniqueResult() != null;
source share