-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Determine whether ANY row matches criteria (criteria API)
PostPosted: Thu May 07, 2009 11:50 am 
Newbie

Joined: Thu May 07, 2009 11:33 am
Posts: 1
Hi,

I really searched for this for quite some time, so please bear with me if this question has been answered many times before.

What I want is actually pretty simple: I need an efficient way to determine whether there are ANY entities in the DB that match given criteria. I don't need the entities themselves, nor the count. Only an answer as simple as a yes or a now.

The corresponding (T-)SQL would look like this:
Code:
-- Idea: select 1 for each row that matches, but select only the first result, returns empty result otherwise
SELECT TOP 1 1 FROM Design this_ WHERE this_.Number = 1 and this_.Status = 0;

-- Idea: return 1 if subquery has any match, otherwise empty result
SELECT 1 WHERE EXISTS (SELECT * FROM Design this_ WHERE this_.Number = 1 and this_.Status = 0);

-- Idea: similar to previous one, but return 0 if no results found
SELECT CASE WHEN EXISTS (SELECT * FROM Design this_ WHERE this_.Number = 1 and this_.Status = 0) THEN 1 ELSE 0 END;


These three options should be more or less equally fast. Now comes the question: how do I map this most efficiently to the NH criteria API?

I tried this (resembling the first SQL option):
Code:
var crit = session
  .CreateCriteria<Design>()
  .Add(Restrictions.Eq("Number", designNumber))
  .Add(Restrictions.Eq("Status", DesignStatus.Active))
  .SetProjection(Projections.Constant(1))
  .SetMaxResults(1);

var found = crit.UniqueResult() != null;

This works perfectly. But is there a better way to do this? Is this causing the least amount of overhead within NH?

BTW, the generated SQL is:
Code:
SELECT TOP 1 y0_ FROM
( SELECT @p0 as y0_, ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row
  FROM [Design] this_
  WHERE this_.Number = @p1 and this_.Status = @p2) as query
WHERE query.__hibernate_sort_row > 0
ORDER BY query.__hibernate_sort_row;

@p0 = '1', @p1 = '1', @p2 = '0'


While the query plan is slightly more complex than the one of the SQL version posted above it is not considerably more expensive.

Thanks in advance for suggestions on this topc.

Peter


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.