﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Carsten Hess commented on NHibernate, the database query cache and parameter sizes</title><description>Ayende,
  
  
Unfortunately there is a "catch" to it and setting "prepare_sql" to true is not truly a good solution... I'l explain:
  
  
Just a little background on executionplans: Executionplans takes time to create and fills up the DB memory, leaving less space for cached data. Therefore - what we want is the DB to reuse executionplans GLOBALLY - across all connections - old and new, as much as possible. The way to do that is to use "parameterized statements" (not prepared statements) where all parameters to the query are defined as variables of a certain type. And its true, in this respect SQL server treats a varchar(5) and a varchar(11) as two different types and creates two different executionplans.
  
  
In this sense, setting "prepare_sql" to true, kind of helps, not because NHibernate calls Prepare() on the underlying IDBCommand, but because it sets the length on the IDBCommands to a fixed size - which is what makes the ADO.NET layer create parameterized statements automatically.
  
  
The sideeffect of calling Prepare() is that a socalled "prepared statement" is created in the DB. This is essentially a statement that is precompiled in the DB and gives back a handle so future calls on THE SAME CONNECTION can be made just referencing this handle and not sending the entire sql again. If all calls to the DB were made on the same connection its true that the executionplans would be reused, but as we know - connections are drawn from the connectionpool as needed and handed back in a random manner. The prepared statements on the connection are un-prepared everytime a new session is created and the connection is soft-initialized.
  
  
So - personally I have made some local changes (which Fabio by the way is not interested in) to NHibernate (SQLClientDriver.cs) so SetParameterSizes() is ALWAYS called, but I'm leaving "prepare_sql" set to false. This gives me exactly the behaviour I want (verified with SQLProfiler and by looking at the statements in the executionplan cache in SQL server)
  
  
The change in SQLClientDriver leads to some other small changes  because the fixed sizes that are set on the parameters are not truly the column sizes defined in the mapping (known bug in NHibernate)
  
  
Hope my entry can throw some new light on the discussion.
  
  
Kind regards
  
Carsten
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment13</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment13</guid><pubDate>Mon, 04 May 2009 07:18:33 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate, the database query cache and parameter sizes</title><description>Jimmy,
  
Yes
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment12</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment12</guid><pubDate>Sun, 03 May 2009 06:09:29 GMT</pubDate></item><item><title>Jimmy Chan commented on NHibernate, the database query cache and parameter sizes</title><description>Ayende,
  
  
NHibernate "prepared" also can be used in SQLite backend?
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment11</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment11</guid><pubDate>Sun, 03 May 2009 02:27:24 GMT</pubDate></item><item><title>eledu commented on NHibernate, the database query cache and parameter sizes</title><description>There is a better explanation of the issue here
  
  
[nhforge.org/.../...-queries-with-ms-sqlserver.aspx](http://nhforge.org/wikis/howtonh/tuning-queries-with-ms-sqlserver.aspx)</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment10</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment10</guid><pubDate>Sun, 03 May 2009 01:38:20 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate, the database query cache and parameter sizes</title><description>configurator,
  
thanks fixed
  
  
josh,
  
because there are costs associated with it that you have to understand.
  
For example, consider prepared statements with dynamic-update or dynamic-insert.
  
The DB can sometimes generate a better query plan for different parameters as well.
  
  
Neil,
  
Yes,
  
cfg.Properties[Environment.UsePreparedStatements] = true;
  
  
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment9</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment9</guid><pubDate>Sat, 02 May 2009 17:25:30 GMT</pubDate></item><item><title>Neil Mosafi commented on NHibernate, the database query cache and parameter sizes</title><description>Surely there's a way to set that as default on the session factory or something?
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment8</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment8</guid><pubDate>Sat, 02 May 2009 16:39:03 GMT</pubDate></item><item><title>configurator commented on NHibernate, the database query cache and parameter sizes</title><description>You have a small typo: whether or not, not whatever or not.
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment7</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment7</guid><pubDate>Sat, 02 May 2009 16:08:00 GMT</pubDate></item><item><title>Josh commented on NHibernate, the database query cache and parameter sizes</title><description>Can it not default to true for the databases where we know it will perform better, a.k.a. sql server.?
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment6</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment6</guid><pubDate>Sat, 02 May 2009 15:48:54 GMT</pubDate></item><item><title>matt commented on NHibernate, the database query cache and parameter sizes</title><description>Certainly - reasonable advice, start with the easiest thing and fine tune as needed - thanks, will try that route.
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment5</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment5</guid><pubDate>Sat, 02 May 2009 14:18:44 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate, the database query cache and parameter sizes</title><description>Matt,
  
To be frank, I don't know.
  
I usually use FN as a drafting board, and move to the XML when I need more than that.
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment4</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment4</guid><pubDate>Sat, 02 May 2009 11:10:28 GMT</pubDate></item><item><title>matt commented on NHibernate, the database query cache and parameter sizes</title><description>With this post and the series of posts before, it seems like there is a lot of power locked away in the XML config options in NHibernate. You've mentioned Fluent NHibernate in the past - is any of this stuff possible using that framework instead of XML config? (not that it's bad, just wondering) What do you recommend someone just starting ?with NHibernate?
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment3</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment3</guid><pubDate>Sat, 02 May 2009 11:06:37 GMT</pubDate></item><item><title>Ayende Rahien commented on NHibernate, the database query cache and parameter sizes</title><description>Scott,
  
Because there are implications of preparing queries in certain databases.
  
For example, IIRC, MySQL will actually perform slower in some scenarios, because it has better cache semantics.
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment2</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment2</guid><pubDate>Sat, 02 May 2009 10:10:04 GMT</pubDate></item><item><title>Scott commented on NHibernate, the database query cache and parameter sizes</title><description>Why this is not the default behaviour?
</description><link>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment1</link><guid>http://ayende.com/3991/nhibernate-the-database-query-cache-and-parameter-sizes#comment1</guid><pubDate>Sat, 02 May 2009 08:44:55 GMT</pubDate></item></channel></rss>