0

I'm having insane simple query: It pulls an ID from one table. The implementation is done using EF 3.5.

This query is repeated in a loop, where I collected a ID from a file and do the search in the database. When running this program, the SQL server is stressed like crazy (the processor utilization soars to 100% for all 16 cores).

It looks like the table of this query is completely locked and nobody gets in anymore. I've read about the necessity to use DbTransaction (begin transaction, commit) or TransactionScope, but the thing is I'm only selecting/reading. Also it's one query, which is atomic in itself, so the use of Transaction(Scope) is shady at best. I did try an implementation, but that doesn't seem to do it.

My (LINQ) query: Image image = context.Images.First(i => i.ImageUid == identifier)

Any thoughts on why this is happening? Again I'd like to stress that I'm only selecting/reading records. I don't delete or update records in the database. This is so insanely straight forward that it is frustrating!

For sake of being complete (my attempt at a fix):

 // This defaults the isolation level to 'READ COMMITTED' which
 // doesn't lock the table when querying.
 DbTransaction trx = context.Connection.BeginTransaction();
 string isolationLevel = trx.IsolationLevel.ToString();
 Image image = context.Images.First(i => i.ImageUid == identifier);
 trx.Commit();

NEW: The profiler shows that the Entity framework is doing a SELECT TOP(1) in the image table. This amounts to a MASSIVE amount of reads, hundreds of thousands! That would suggest that there is no index, but I've looked it up (see comments) and there is one! Also very weird, on the logout, again hundreds of thousands of reads.

I decided to throw out the Entity Framework and do this query using SqlConnection and SqlCommand, but the result is the same!

Next we copied the sp_executesql in the management console and found it took an amazing 4 seconds to execute. Doing the query 'direct' gives an instant result.

Something in the sp_executesql appears to slow things to a crawl. Any ideas?

4

1 回答 1

0

我想我明白了......在发现 sp_executesql 是罪魁祸首之后,它变得很清楚。见http://yasirbam.blogspot.nl/2009/06/spexecutesql-may-cause-slow-perfomance.html

由于愚蠢的转换,表上的索引没有被使用!这解释了 SQL Profiler 中可见的所有内容。

现在该工具正在测试中,它和照明一样快!!

于 2013-09-16T13:43:01.047 回答