I want to optimize a certain query on my database, if results are > 500 then don't run the query.
What is the fastest way from these 2:
1)
list = createNativeQuery("select count(0) from (select * from table where rownum <= 501)")
if(list.size() > 500) throw error;
else runQuery();
or 2)
list = createNativeQuery("select * from table where rownum <= 501")
if(list.size() > 500) throw error;
Are count queries faster in general and optimized to run faster than actually bringing all the rows and counting the size of the result?
Edit: In the 1st case, if count(0) returns size < 500, then I have to re-run the query, and in my case I have a complex where clause. If my sub-query takes ~10s, it will need ~20s in scenario 1). My question was if the subquery needs ~10s, will the select count(0) from sub-query need e.g. ~1s because of indexes and optimizations by oracle?