views:

14

answers:

1

I have below scenario

  1. Got n as minimum number of database required for the transaction.
  2. Issue select count(*) query to find out number of rows satisfying the criteria.
  3. if the result of step 2 is greater than or equal to n, then proceed further or throw exception.
  4. Fetch actual objects by limit to n in hibernate query.
  5. 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() ?

+1  A: 

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.
Pascal Thivent
Thanks Pascal, the event is exceptional only, I would go with fetching objects directly.
Reddy