简体   繁体   English

在多个 SQL 查询中测试性能

[英]Testing for Performance in Multiple SQL Queries

I'm working to improve the efficiency of some SQL Queries on SQL-Server-2008.我正在努力提高 SQL-Server-2008 上某些 SQL 查询的效率。 There are different ways of performing each query and I want to find the fastest of them.执行每个查询有不同的方法,我想找到最快的方法。

However, the issue that I'm having is that I am having trouble determining which is actually executing faster.但是,我遇到的问题是我无法确定哪个实际上执行得更快。 Ideally I could just run each query one after the other and see which runs fastest.理想情况下,我可以一个接一个地运行每个查询,看看哪个运行得最快。 Ideally...理想情况下...

Problem is, is that SQL is too smart for my liking.问题是,SQL 对我来说太聪明了。 When constructing these queries I run them multiple times.在构建这些查询时,我多次运行它们。 When I do this, the queries' efficiencies improve on their own.当我这样做时,查询的效率会自行提高。 This I would imaged is because of some behind-the-scenes stuff that SQL does.我认为这是因为 SQL 所做的一些幕后工作。 What is this?这是什么? How can I avoid it?我怎样才能避免它?

For example, I run the query once and it takes 30s.例如,我运行一次查询,需要 30 秒。 I run it again and it takes 10s.我再次运行它,需要 10 秒。 The more I run the query the faster it seems to run.我运行的查询越多,它似乎运行得越快。

So.. Is there any way of "clearing the cache" or whatever the equivalent would be in SQL?那么.. 有没有办法“清除缓存”或 SQL 中的任何等价物? I want to get an accurate indication of which query is going to actually run faster.我想准确指示哪个查询实际上运行得更快。 Alternatively, what would be the best way to do the type of testing that I want?或者,进行我想要的测试类型的最佳方法是什么?

Any information in regards to this topic would be accepted as valid input.有关此主题的任何信息都将被接受为有效输入。

When the query is run first most likely the data is still on disk, SQl Server has to fetch this data, when you run the same query the data is already in RAM and thus it will be much faster than going to disk当查询第一次运行时,数据很可能仍在磁盘上,SQL Server 必须获取这些数据,当您运行相同的查询时,数据已经在 RAM 中,因此它比去磁盘要快得多

run DBCC DROPCLEANBUFFERS and DBCC FREEPROCCACHE to clear the cache without doing a restart运行DBCC DROPCLEANBUFFERSDBCC FREEPROCCACHE以清除缓存而不重新启动

You need to look at execution plans, statistics io and statistics time to really see what is going on.您需要查看执行计划、统计 io 和统计时间才能真正了解发生了什么。 in the plan look for conversions and also for scans (you want seeks if possible).在计划中寻找转换和扫描(如果可能,你想要寻找)。

See also Client Statistics in SSMS.另请参阅SSMS 中的客户端统计信息。 Check execution times 检查执行时间

The improvement in speed that you see is a result of the database's query cache.您看到的速度提高是数据库查询缓存的结果。 Most relational DB engines have this feature, which caches the result of a query until the table(s) you read from are updated.大多数关系数据库引擎都具有此功能,它会缓存查询结果,直到您从中读取的表更新。

This post gives good pointers on how to work around this for performance tuning. 这篇文章就如何解决此问题以进行性能调优提供了很好的指导。 You should also look into Execution Plans , which show you how the database would run the query, without actually running it.您还应该查看Execution Plans ,它向您展示了数据库将如何运行查询,而无需实际运行它。 The benefit of this is that you can see if full table scans are being done where an index could be used instead.这样做的好处是您可以查看是否在可以使用索引的地方进行全表扫描。

Include the Actual Execution Plan and execute the following command:包括实际执行计划并执行以下命令:

CHECKPOINT; 
GO 
DBCC DROPCLEANBUFFERS; 
GO

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

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