开发者

Same SQL Query Slower from NHibernate Application than SQL Studio?

开发者 https://www.devze.com 2023-03-08 20:03 出处:网络
Our application issues an NHibernate-generated SQL query.At application runtime, the query takes about 12 seconds to run against a SQL Server database.SQL Profiler shows over 500,000 reads.

Our application issues an NHibernate-generated SQL query. At application runtime, the query takes about 12 seconds to run against a SQL Server database. SQL Profiler shows over 500,000 reads.

However, if I capture the exact query text using SQL Profiler, and run it again from SQL Studio, it takes 5 seconds and shows less than 4,600 reads.

The query uses a couple of parameters whose values are supplied at the end of the 开发者_C百科SQL text, and I'd read a little about parameter sniffing and inefficient query plans, but I had thought that related to stored procedures. Maybe NHibernate holds the resultset open while it instantiates its entities, which could explain the longer duration, but what could explain the extra 494,000 "reads" for the same query as performed by NHibernate? (No additional queries appear in the SQL Profiler trace.)

The query is specified as a LINQ query using NHibernate 3.1's LINQ facility. I didn't include the query itself because it seems like a basic question of philosophy: what could explain such a dramatic difference?

In case it's pertinent, there also happens to be a varbinary(max) column in the results, but in our situation it always contains null.

Any insight is much appreciated!


Be sure to read: http://www.sommarskog.se/query-plan-mysteries.html

Same rules apply for procs and sp_executesql. A huge reason for shoddy plans can be passing in a nvarchar param for a varchar field, it causes index scans as opposed to seeks.

I very much doubt the output is affecting the perf here, it is likely to be an issue with one of the params sent in, or selectivity of underlying tables.

When testing your output from profiler, be sure to include sp_executesql and make sure your settings match (stuff like SET ARITHABORT), otherwise you will cause a new plan to be generated.

You can always dig up the shoddy plan from the execution cache via sys.dm_exec_query_stats

0

精彩评论

暂无评论...
验证码 换一张
取 消