<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://nhforge.org/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver.aspx</link><description>Quick starts, tutorials, code snippets, custom user types, application blocks and more...</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver.aspx</link><pubDate>Fri, 01 Apr 2011 20:43:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:54</guid><dc:creator>Groxx</dc:creator><comments>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/comments.aspx</comments><description>Current revision posted to How to by Groxx on 01/04/2011 05:43:54 p.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;&lt;br /&gt;&lt;br /&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%&lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Term&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Research&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Paper&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Thesis&lt;/span&gt;
&lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Essay&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;AND&lt;/span&gt;
&lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="text-decoration: line-through; color: red;"&gt;Dissertation&lt;/span&gt; &lt;span style="text-decoration: line-through; color: red;"&gt;Help&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;
&lt;p&gt;The things you should care about are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; the length of the
parameter must always be the correct one in order to reuse the cached execution plans&lt;/li&gt;
&lt;li&gt;the type ot the parameter must be the same of the table&amp;#39;s column &lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/6.aspx</link><pubDate>Wed, 30 Mar 2011 11:25:54 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:388</guid><dc:creator>markweee</dc:creator><comments>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/comments.aspx</comments><description>Revision 6 posted to How to by markweee on 30/03/2011 08:25:54 a.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;&lt;br /&gt;&lt;br /&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone% &lt;a href="http://www.termpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Term&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.researchpaperdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Research&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Paper&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?  &lt;a href="http://www.thesisdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Thesis&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt; &lt;a href="http://www.essaydom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Essay&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt; &lt;span style="background: SpringGreen;"&gt;AND&lt;/span&gt;
&lt;a href="http://www.dissertationdom.com/"&gt;&lt;span style="background: SpringGreen;"&gt;Dissertation&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;Help&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;
&lt;p&gt;The things you should care about are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; the length of the
parameter must always be the correct one in order to reuse the cached execution plans&lt;/li&gt;
&lt;li&gt;the type ot the parameter must be the same of the table&amp;#39;s column &lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/5.aspx</link><pubDate>Tue, 17 Mar 2009 13:27:44 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:375</guid><dc:creator>Claudio Maccari</dc:creator><comments>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/comments.aspx</comments><description>Revision 5 posted to How to by Claudio Maccari on 17/03/2009 10:27:44 a.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;&lt;br /&gt;&lt;br /&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;
&lt;p&gt;The things you should care about are:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; the length of the
parameter must always be the correct one in order to reuse the cached execution plans&lt;/li&gt;
&lt;li&gt;the type ot the parameter must be the same of the table&amp;#39;s column &lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/4.aspx</link><pubDate>Tue, 17 Mar 2009 13:16:40 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:252</guid><dc:creator>Claudio Maccari</dc:creator><comments>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/comments.aspx</comments><description>Revision 4 posted to How to by Claudio Maccari on 17/03/2009 10:16:40 a.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;&lt;br /&gt;&lt;br /&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%&lt;br /&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;
&lt;p&gt;&lt;span style="background: SpringGreen;"&gt;The&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;things&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;you&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;should&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;care&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;about&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;are&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;:&lt;/span&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;length&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt;
&lt;span style="background: SpringGreen;"&gt;parameter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;always&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;correct&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;one&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;in&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;order&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;to&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;reuse&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;cached&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;execution&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;plans&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;type&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;ot&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;parameter&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;must&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;be&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;same&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;of&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;the&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;table&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background: SpringGreen;"&gt;s&lt;/span&gt; &lt;span style="background: SpringGreen;"&gt;column&lt;/span&gt; &lt;/li&gt;
&lt;/ol&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/3.aspx</link><pubDate>Mon, 16 Mar 2009 20:10:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:251</guid><dc:creator>Claudio Maccari</dc:creator><comments>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/comments.aspx</comments><description>Revision 3 posted to How to by Claudio Maccari on 16/03/2009 05:10:22 p.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)
		.List();
}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;

exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%
&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();
}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/2.aspx</link><pubDate>Mon, 16 Mar 2009 20:10:22 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:250</guid><dc:creator>Claudio Maccari</dc:creator><comments>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/comments.aspx</comments><description>Revision 2 posted to How to by Claudio Maccari on 16/03/2009 05:10:22 p.m.&lt;br /&gt;
&lt;h2&gt;Tuning queries with MS SQLServer&lt;/h2&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)
		.List();
}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;

exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as &lt;br /&gt;RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,&lt;br /&gt;N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%
&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();
}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;</description></item><item><title>Tuning queries with MS SQLServer</title><link>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/revision/1.aspx</link><pubDate>Mon, 16 Mar 2009 20:09:12 GMT</pubDate><guid isPermaLink="false">45f813f2-f1c4-4eda-a619-288e3cadc793:249</guid><dc:creator>Claudio Maccari</dc:creator><comments>http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver/comments.aspx</comments><description>Revision 1 posted to How to by Claudio Maccari on 16/03/2009 05:09:12 p.m.&lt;br /&gt;
&lt;p&gt;While writing this simple query using NHibernate API&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zon%&lt;/span&gt;&amp;quot;)
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetString(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zone%&lt;/span&gt;&amp;quot;)
		.List();
}&lt;/pre&gt;
&lt;p&gt;you may notice a strange behavior of NHibernate 2.0. The code shown above generates this two SQL statements:&lt;/p&gt;
&lt;pre&gt;exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,N&amp;#39;@p0 nvarchar(4)&amp;#39;,@p0=N&amp;#39;zon%&amp;#39;

exec sp_executesql N&amp;#39;select region0_.RegionId as RegionId0_, region0_.RegionDescription as RegionDe2_0_ from Region region0_ where (region0_.RegionDescription like @p0 )&amp;#39;,N&amp;#39;@p0 nvarchar(5)&amp;#39;,@p0=N&amp;#39;zone%
&lt;/pre&gt;
&lt;p&gt;
As you can see the parameter Size is different in the two statements and this cause SqlServer to generate two different execution plans. If you need the best performace you should avoid this behaviour. If you send to the database some queries using the same parameter (type &amp;amp; size) SqlServer use his internal cache. You have already pay for it so why not to use it ?&lt;/p&gt;
&lt;p&gt;If you add this line in your NHibernate .config file&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;name&lt;/span&gt;=&lt;span style="color:#0000ff;"&gt;&amp;quot;prepare_sql&amp;quot;&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;/span&gt;true&lt;span style="color:#0000ff;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000;"&gt;property&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;and write this code the database engine will use his internal cache:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())&lt;br /&gt;{&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;zoneh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;&lt;br /&gt;	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)&lt;br /&gt;		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;neh%&lt;/span&gt;&amp;quot;, TypeFactory.GetStringType(10))&lt;br /&gt;		.List();&lt;br /&gt;}&lt;/pre&gt;
&lt;p&gt;This code is better cause the parameters Size is always the same. Now your
database will thank you for this! But wait you can still do something
better.&lt;/p&gt;
&lt;p&gt;If your database column type is a varchar and not
nvarchar you can improve your code using a different SqlDbType. How ? With his code:&lt;/p&gt;
&lt;pre&gt;&lt;span style="color:#0000ff;"&gt;using&lt;/span&gt; (ISession session = factory.OpenSession())
{
	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;z%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();

	session.CreateQuery(&amp;quot;&lt;span style="color:#8b0000;"&gt;from Region r where r.Description like :desc&lt;/span&gt;&amp;quot;)
		.SetParameter(&amp;quot;&lt;span style="color:#8b0000;"&gt;desc&lt;/span&gt;&amp;quot;, &amp;quot;&lt;span style="color:#8b0000;"&gt;za%&lt;/span&gt;&amp;quot;, TypeFactory.GetAnsiStringType(15))
		.List();
}&lt;/pre&gt;
&lt;p&gt;Now the parameter type is a varchar and the size is always 15. With this latest improvement you remove a CONVER_IMPLICIT operation from your query execution plan. This cast operation cost something so if you don&amp;rsquo;t need it why to remove it ?&lt;/p&gt;</description></item></channel></rss>
