April 10, 2011 at 10:39 pm
Comments posted to this topic are about the item An nHibernate Head Scratcher
April 11, 2011 at 2:30 am
Nice Article David, it certainly highlights a potential problem when using ORMs against SQL Databases (and dynamic SQL in general!).
I think many people though, especially junior developers/DBAs, could make more use of this article as a fine example of iterative problem solving - i.e. not taking a sledgehammer to the issue but working through it logically one step at a time.
This was both an enjoyable and interesting read for a Monday morning. Thanks.
James
MCM [@TheSQLPimp]
April 11, 2011 at 2:51 am
Good drills, thanks for the tips!
Glen Parker 🙂
April 11, 2011 at 5:53 am
In relation to the: "The answer lies in a bug in the cluster installer for SQL2008".
Does this bug exist in R2 aswell?
Carlton.
April 11, 2011 at 7:58 am
In my experience, use of so-called ORM (Object-Relational Management) tools create as many or more problems than they solve. There are many times we haven't got a choice because a shop's methods & tools team have drank the ORM Kool-Aid. However I always recommend against using ORM for one primary reason: Every SQL call deserves to be deliberately designed.
As most readers here know, when an ORM is used, code runs to dynamically generate SQL. This code is designed at the meta-level, so it is difficult if not impossible to optimize the SQL out of the box.
It remains my considered opinion that ORM tools are nothing but an OO developers excuse to not to have to deal with SQL specifically and the relational model in general. The wisest OO developers know that the very best domain models are compliant with, if not based upon, a fully-normalized relational database models.
April 11, 2011 at 8:21 am
Oh wow. I'd have never thought of checking the collation. Thanks for the article and the good reminder. Headed off now to check the collation on my prod. databases to make sure it's what I think it should be. 🙂
April 11, 2011 at 9:19 am
Our developers use nHibernate here and we also use a collation of SQL_Latin1_General_CP1_CI_AS, so I was curious if I would be able to reproduce this same issue. We actually have a very similiar scenario, a table that holds person information with a non-clustered index on the LastName field, which is a dataype of nvarchar(100). Everytime I tried to reproduce your issue, it always did a clustered index seek, not a scan. This is good, however I was kind of hoping I would be able to reproduce it to convince our developers to move away from nvarchar :). I've tried clearing the query cache, dropping/recreating indexes, etc, and it always seems to perform a scan. We are using SQL Server 2005 - any ideas?
Adam
April 11, 2011 at 9:36 am
If your field is an NVARCHAR and you use VARCHAR parameters you will get a seek because SQL is happier to turn a VARCHAR into an NVARCHAR than the other way around.
It's worth noting that anything that involved converting a string value into another value can also have the problem.
SQL Server can take a statement such as
WHERE DateCreated BETWEEN '2011-04-01' and '2011-04-11'
and get exactly the same type of implicit conversion.
April 11, 2011 at 9:39 am
Right - I was still getting the seek even when I explicity declare a parameter as a nvarchar and used that to query my nvarchar field, like he did in his article. I still received a seek even after I cleared out the query cache and rebuilt the non-clustered index.
April 11, 2011 at 9:54 am
Not a bug in SQL Server because you can choose a different collation when you are setting up SQL Server. It is more of a human bug for not checking how to do a proper setup.
To change a server collation:
export all the data and script all the users.
drop the databases and rebuild the master using the correct collation
recreate the users and databases and import the data
Collations are hierarchical. By that I mean they can be easily over-ridden.
A database collation can be set to operate differently from the server and a column can be set to operate differently from its database. A piece of code can be set to operate according to the collation of the object from which it is pulling data.
MS example: SELECT name FROM customer ORDER BY name COLLATE Latin1_General_CI_AS. (I changed the collation name from the MS example to make it more relevant)
The problem here was PBCK.DLL; not SQL Server.
Sorry to be bobby badnews but I would like to see articles that are consistent with MS documentation when it is correct and consistent with the behavior of SQL Server when it is administered correctly.
Don
April 11, 2011 at 9:55 am
nvarchar has a higher precedence, so the varchar -> nvarchar works fine, as noted by Mr. Poole.
http://msdn.microsoft.com/en-us/library/ms190309.aspx
Converting down causes the index issues. Lots of ORMs and some frameworks will submit N'' values and if you have varchar fields and indexes, they don't get used.
April 11, 2011 at 10:34 am
dg81328 (4/11/2011)
Not a bug in SQL Server because you can choose a different collation when you are setting up SQL Server. It is more of a human bug for not checking how to do a proper setup.
You should read the bit where the author said the installer ignored his selected collation before you say snooty things like this.
April 11, 2011 at 10:42 am
FYI, a short time ago I started a forum post asking for discussion on the usage of ORM tools:
http://www.sqlservercentral.com/Forums/Topic1087686-391-1.aspx
If you have experience (good or bad) in working with ORM tools with SQL, please post your experience and thoughts there.
Thanks!
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
April 11, 2011 at 12:44 pm
Except, in the analysis, it isn't really a problem related to or caused by the ORM itself or the SQL it produces, it's a subtle issue invoked by implicit casting of types and collations, that could sneak in unobtrusively and unintentionally from anywhere.
And, throw in a subtle bug for a non-US English installs...
I think I've dealt with a similar issue with just straight up data loaded from different data sources...
VERY subtle bug.
April 11, 2011 at 3:04 pm
We discovered this same problem with nHibernate about 6 months ago. It was running index scans instead of seeks. The datbase columns were defined ad VARCHR but Hibernate was passing NVARCHAR. After we brought it to the attention of the people who developed the offending application they were able to make some modifications to the configuration parameters for nHibernate and corrected the problem. I am not familier enough with nHibernate to know exactly what they did.
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply