Query duration estimation in SQL Server

雨燕双飞 提交于 2019-12-09 00:34:29

问题


I've seen in Oracle 10g a feature that estimates the remaining time for a long running query and I was wondering if this is possible too in SQL Server (at least in 2008?)?

Suppose I have a very large table with tens of millions of rows (well indexed etc. etc.) and I need to search for some particular rows. I know it will take a lot of time and I'm cool with that but I would like to present the user with some kind of a progress bar.

How can I show progress?


回答1:


I'd forget about it and just put a spinning circle!

Seriously though, to take MrTelly's idea further, there are dynamic management views that can give you average execution times for certain queries - maybe that can get you somewhere.

http://msdn.microsoft.com/en-us/library/ms188754.aspx

edit: I have seen percent complete in the sp_whoisactive procedure from Adam Machanic. Maybe that is another place to look into. SQL 2016 has the query store which persists plan cache information - a substitute for the dmv plan cache, which is cleared on reboot.




回答2:


Related:

  • Surajit Chaudhuri, Vivek Narasayya, and Ravi Ramamurthy. Estimating Progress of Execution for SQL Queries. ACM SIGMOD 2004.

Today’s database systems provide little feedback to the user/DBA on how much of a SQL query’s execution has been completed. For long running queries, such feedback can be very useful, for example, to help decide whether the query should be terminated or allowed to run to completion. Although the above requirement is easy to express, developing a robust indicator of progress for query execution is challenging. In this paper, we study the above problem and present techniques that can form the basis for effective progress estimation. The results of experimentally validating our techniques in Microsoft SQL Server are promising.




回答3:


I'm not aware of a tool that will do this automatically, but there are a couple of alternatives. Break your query into blocks ...

select blah from table where IdRange between (1 and 100000)

select blah from table where IdRange between (100001 and 200000)

as each sql completes so update the progress bar.

Or you could record the length of time taken for each of your selects, store those values maybe on a per user basis. Then use that information to return a progress bar length.

Both these approaches are pretty kludgy, hopefully someone knows a better approach.

Of course you could try to decipher the query plan and make a judgement based on that, but in code that would be hard.



来源:https://stackoverflow.com/questions/617170/query-duration-estimation-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!