-->
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.  [ 3 posts ] 
Author Message
 Post subject: Parameterised queries & cost-based optimisation in SqlSe
PostPosted: Tue Feb 13, 2007 9:20 am 
Newbie

Joined: Mon Oct 06, 2003 4:41 pm
Posts: 18
Hibernate version:

3.2.0.ga

Name and version of the database you are using:

MS SqlServer 2000

My DBAs are reporting the following issue with use of parameterised queries as produced by Hibernate. Sorry that isn't directly relevant to Hibernate but I know of no other forum where I can find a community with same skills and experience. Unfortunately if I can't get around this problem our adoption of Hibernate will be bought into question.

A fairly complex Criteria query in Hibernate is producing SQL something like the following:

Code:
declare @P1 int

exec sp_prepare @P1 output, N'@P1 varchar(8000),@P2 varchar(8000),@P3 varchar(8000),@P4 datetime', N'

select top 50
... {various column names deleted} ...
      from ftxFnclTxn this_
                  left outer join clsClasn clasn2_ on ...
... {various joins deleted} ...
      where (this_.ftxOrigContraRplcInd is null or not this_.ftxOrigContraRplcInd in (@P1,@P2))
      and this_.ftxOrigSigningNmbr like @P3  and this_.ftxOrigSigningDate=@P4  order by this_.ftxSID
      desc'

exec sp_execute @P1, 'C', 'X', '5%', 'Sep 22 2006 12:00:00:000AM'

exec sp_unprepare @p1



What they're telling me is that due to the way the SqlServer cost-based optimiser is working:

1. The sp_prepare creates an execution plan best suited to execute the query based upon the statistics present for the tables but the plan will not be optimal for all combinations of parameters
2. sp_execute executes the sql with the supplied parameters but the plan is not optimal and is not recompiled.

This is due to un-even distribution of data in the parameterised columns.

Execution times are varying between 3 seconds to 3 minutes compared to the stored-proc equivalents.

Any ideas on how I can influence SqlServer to do the right thing? I.e. reconstruct the query plan based on the parameters that are passed without having to resort to throwing out Hibernate and using Stored Procs instead?

Thanks!


Top
 Profile  
 
 Post subject: Partial solution
PostPosted: Wed Feb 14, 2007 7:57 am 
Newbie

Joined: Mon Oct 06, 2003 4:41 pm
Posts: 18
If anyone is interested it seems that we were suffering from a widely known (in the SqlServer community) problem called the "Parameter Sniffing Problem" - Google for more info.

Our not particularly elegant work-around is to use Expression.sql() to construct the affected bits of the where clause using String concatentation instead of the normal method which results in parameterised queries.

PM me for further info if you have this problem.


Top
 Profile  
 
 Post subject: Further problems
PostPosted: Tue Jun 12, 2007 5:56 am 
Newbie

Joined: Mon Oct 06, 2003 4:41 pm
Posts: 18
The issue I originally posted about is occurring more frequently and it's looking like we need to find a way to default to un-parameterised parameter bindings.

E.g. rather than:

Restrictions.eq("myProperties", someValue)

we need:

Restrictions.sqlRestriction("{alias}.myPropCol = '" + someValue + "'")

Does anyone know a way of implementing this as default behaviour when Hibernate constructs the SQL?

Even as I type this I realise that equality is only the simple case and "in" clauses would be more complicated and datatype specific.

Another solution seems to be use a MSSQLServer hint "option(recompile)" to force reevaluation of the query plan for each query. In our case the performance benefits would outweigh the costs as counter-intuitive as this sounds. However I can't see how to use sql query hints in the generated SQL, any ideas?

Looking forward to any responses!

Matt.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.