简体   繁体   English

SQL Server 2005的奇怪查询性能

[英]Strange query performance of SQL Server 2005

I noticed some performance problems with my DB. 我注意到数据库存在一些性能问题。 Such query (just for a example): 这样的查询(仅作为示例):

SELECT * 
  FROM ActionHistory 
 WHERE ObjectId = @id"  

...executes at random with different reads and duration. ...以不同的读取次数和持续时间随机执行。 ObjectId is Foreign Key, with index on it. ObjectId是外键,上面带有索引。

With SQL Profiler I found, that sometimes the results are: 5 reads, 0 duration, but in another case: 5 reads, 200 duration. 使用SQL事件探查器,我发现有时结果是:5次读取,持续时间为0,但在另一种情况下:5次读取,持续时间为200。 Such big durations occurs accidentally. 如此长的持续时间是偶然发生的。

I use distributed transaction with WCF. 我在WCF中使用分布式事务。 Such results I got when I was the only user at that time, so it likely not to be a locks or something else. 当我当时是唯一的用户时,我得到了这样的结果,因此它可能不是锁或其他东西。

What is the reason of such behaviour: low reads, but high query duration ? 这种行为的原因是什么:读取次数少,但查询持续时间长?

In general, distributed transactions are extremely expensive. 通常,分布式事务非常昂贵。 Try disabling distributed transactions in your environment to see if that changes anything. 尝试在您的环境中禁用分布式事务,以查看是否有任何改变。

Since the query is exactly the same each time and the reads are the same, then it's most likely due to locking. 由于每次查询都完全相同,并且读取次数相同,因此很可能是由于锁定。 Sometimes another query is executing and may have a lock on the records that need to be accessed. 有时另一个查询正在执行,并且可能在需要访问的记录上具有锁定。 Waiting for the lock to be released would cause a slowdown. 等待释放锁会导致速度变慢。

Using SQL Profiler to compare start/stop times for queries you can identify overlapping queries that may cause locking. 使用SQL Profiler比较查询的开始/停止时间,可以识别可能导致锁定的重叠查询。

This is not an indication of a problem, just an explanation of the differences you're seeing. 这并不是问题的征兆,只是对您所看到的差异的解释。

Enable read committed snapshot in the database: 在数据库中启用读取的已提交快照:

ALTER DATABASE ... SET READ_COMMITTED_SNAPSHOT ON;

This will miraculously change your reads that occur under the default read-committed isolation into snapshot reads, which are not hindered by locks. 这将奇迹般地将在默认读取提交隔离下发生的读取更改为快照读取,而不受锁定的影响。 See Choosing Row Versioning-based Isolation Levels for details, including the runtime resource usage caused by enabling snapshot reads. 有关详细信息,请参阅选择基于行版本控制的隔离级别 ,包括启用快照读取所导致的运行时资源使用情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM