What I've read is that the way to implement the "exists" is getting the first register that matches the condition (in SQL Server it will be SELECT TOP(1)...), but it must be compatible with Oracle too, and I'm not so sure that in Oracle, use rownumber is so efficient. Moreover, it must be scalable, I can have from 2 or 3 registers up to 2 or 3 millions...
My current solution is like this:
int? result = this.Session
.QueryOver<Address>()
.Where(a => a.Customer.Id == customerId && a.IsMain == true)
.Select(a => a.Id)
.Take(1)
.SingleOrDefault<int?>();
return result != null;
This generates a select top (1) query on SQL Server that I think is more efficient that a count (specially in great volumes) but in Oracle, the query generated is like that:
SELECT y0_
FROM (
SELECT this_.ADD_Id AS y0_
FROM ADDRESSES this_
WHERE (
this_.CUS_Id = 3942425
AND this_.ADD_ISMAIN = 'AAAAAAAAAA'
)
)
WHERE rownum <= 1
I'm not so sure of the efficiency of that query on Oracle.
Someone could help me to get the most efficient query on both environments with the same NHibernate instruction? Something like an Any() method that I found on some posts, but I can't found it on QueryOver...