-->
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.  [ 4 posts ] 
Author Message
 Post subject: Named Native query taking long time
PostPosted: Wed Apr 03, 2013 2:52 pm 
Newbie

Joined: Tue Sep 18, 2012 12:00 am
Posts: 6
Named Native query with optional list parameter taking very long time than the query dynamically formed


Named Native Query :
--------------------------

select name from person where person :tempName= 0 OR person.name in (:name))


Query query = em.createNamedQuery("namedQuery")
if(nameList== null || nameList.isEmpty()){
query.setparameter(tempName ,0)
query.setparameter(name,"")
}else{
query.setparameter("tempName", 1);
query.setparameter("name" , nameList)
}


Last edited by veebee54 on Fri Apr 05, 2013 10:43 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Named Native query taking long time
PostPosted: Thu Apr 04, 2013 3:59 am 
Newbie

Joined: Wed Mar 13, 2013 11:32 am
Posts: 16
Is this right?

Code:
query.setparameter(tempName =0)
query.setparameter(name="")


0=0 will return all rows to you


Top
 Profile  
 
 Post subject: Re: Named Native query taking long time
PostPosted: Thu Apr 04, 2013 9:49 am 
Newbie

Joined: Tue Sep 18, 2012 12:00 am
Posts: 6
orwtech@gmail.com wrote:
Is this right?

Code:
query.setparameter(tempName =0)
query.setparameter(name="")


0=0 will return all rows to you



Orwtech , Thank you for your time, Yes, get me records for list of names, if list is empty get me all the records. I'm using this condition as part of joins.


Top
 Profile  
 
 Post subject: Re: Named Native query taking long time
PostPosted: Wed Apr 24, 2013 6:01 pm 
Newbie

Joined: Tue Sep 18, 2012 12:00 am
Posts: 6
This may not apply to all Database but it applies to Oracle and Informix for sure.

Using optional parameters as given in the example will do sequential scan instead of looking at index.

Reason: When Database parses this sql it replaces binding parameters with placeholders and stores the statement in sql statement cache.
Therefore, the statement is optimized without the database server having access to the values of these variables. Hence Database is not
sure if the filter is applied all the time and resulting in sequential scan.

Refer below article which gives very good example:
http://use-the-index-luke.com/sql/where-clause/obfuscation/smart-logic


Also Beware of Parameter sniffing:

Consider a query below. Once Your query's execution plan is cached. Even if your filter is in different table, it will always use the same execution plan , because this query is parsed exactly the same for all scenarios.


Select
(****PO INFORMATIONS ACROSS JOINED TABLES *****)
FROM PURCHASE_ORDER PO
INNER JOIN ITEM ITM ON(JOIN CONDN...)
INNER JOIN ITEM_DETAILS ITDET ON(JOIN CONDN...)
INNER JOIN SKU SK ON(JOIN CONDN...)
INNER JOIN SKU_INFO SKI ON(JOIN CONDN...)
INNER JOIN MANUFACTURER MAN ON(JOIN CONDN...)
WHERE
(?=0 OR PO.ID IN(?))
AND(?=0 OR ITEM.ID IN(?))
AND(?=0 OR ITDET.ID IN(?))
AND(?=0 OR SK.ID IN(?))
AND(?=0 OR SKI.ID IN(?))
AND(?=0 OR MAN.ID IN(?))


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

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.