Is checking no.of rows before fetching them more efficient than directly fetching all data in hibernate?
I have below scenario
- Got n as minimum number of rows required for the transaction.
- Issue
select count(*)
query to find out number of rows satisfying the criteria. - if the result of step 2 is greater than or equal to n, then proceed further or throw exception.开发者_开发知识库
- Fetch actual objects by limit to n in hibernate query.
- do the logic and save the objects.
Is the above more efficient or directly fetching the objects and checking the size of the result set would be more efficient?
I don't need the data if there are not at least 'n' rows satisfying the criteria?
P.S.: Better still is there a way I set like a MinSize() similar to a MaxSize() ?
Is the above more efficient or directly fetching the objects and checking the size of the result set would be more efficient?
I'd say that it depends on the likelihood of the less than n results even:
- If this event is very likely, then performing a
count
first will be more efficient. - If this event is exceptional, then fetching the objects directly will be more efficient.
精彩评论