Finding a SQL Server process percentage complete with DMVs

Problem

Some tasks that are run in SQL Sever take a long time to run and it is sometimes difficult to tell whether these tasks are progressing or not.  One common way of determining that status is to look at the data returned from sp_who2 or sp_lock to ensure that things are still working and the process is not hung.  With SQL Server several dynamic management views have been added, so let’s take a look at some of these and how they can assist.

Solution

As mentioned above using sp_who2 or using the GUI tools you can get an idea of what is running and the current status of these tasks.  With SQL Server several dynamic management views have been added and we will take a look at two of these so you can gain some additional insight into your processing.

The two DMVs that we will look at are:

  • dm_exec_requests
  • dm_exec_sessions

Data can be selected from these DMVs directly using a SELECT statement.

Let’s take a look at running a DBCC CHECKDB which often takes a lot of time to process and see what information we get back from sp_who2 as well as these new DMVs.  We will run this against the AdventureWorks database.

To do this we open a new query window and then run the following:

use AdventureWorks
GO 
DBCC CHECKDB

When looking at the DMVs and the output from sp_who2 we get the results listed below.  When I ran this my session_id was 68, so you will need to use the session_id that is being used to run the DBCC command.  This can be found on the bottom left of the query window.

messages
SELECT * FROM sys.dm_exec_sessions WHERE session_id = 68

Note: I pivoted the data so it was easier to read.

session_id68
login_time9/26/2007 9:16:49 AM
host_nameEDGEWOOD-NB3
program_nameMicrosoft SQL Server Management Studio – Query
host_process_id1516
client_version5
client_interface_name.Net SqlClient Data Provider
security_id0x010500000000000515000000F094C85F782E9D1307E53B2BEB030000
login_nameEDGEWOOD-NB3\DBA
nt_domainEDGEWOOD-NB3
nt_user_nameDBA
statusrunning
context_info0x
cpu_time68986
memory_usage2
total_scheduled_time169019
total_elapsed_time214482
endpoint_id2
last_request_start_time9/26/2007 10:14:56 AM
last_request_end_time9/26/2007 10:14:46 AM
reads13870
writes278
logical_reads367884
is_user_process1
text_size2147483647
languageus_english
date_formatmdy
date_first7
quoted_identifier1
arithabort1
ansi_null_dflt_on1
ansi_defaults0
ansi_warnings1
ansi_padding1
ansi_nulls1
concat_null_yields_null1
transaction_isolation_level2
lock_timeout-1
deadlock_priority0
row_count248
prev_error0
original_security_id0x010500000000000515000000F094C85F782E9D1307E53B2BEB030000
original_login_nameEDGEWOOD-NB3\DBA
last_successful_logonNULL
last_unsuccessful_logonNULL
unsuccessful_logonsNULL
SELECT * FROM sys.dm_exec_requests WHERE session_id = 68

Note: I pivoted the data so it was easier to read.

session_id68
request_id00:00.0
start_time14:55.5
statussuspended
commandDBCC TABLE CHECK
sql_handle0x020000002E9F9D2A305AB0BB397742F3F884C999A41E7826
statement_start_offset0
statement_end_offset-1
plan_handle0x06000C002E9F9D2AB881CA05000000000000000000000000
database_id12
user_id1
connection_id16A82EE3-CB5F-4977-AF2A-304E5DCB49E1
blocking_session_id0
wait_typeCXPACKET
wait_time4125
last_wait_typeCXPACKET
wait_resource 
open_transaction_count1
open_resultset_count00:00.0
transaction_id00:00.0
context_info0x
percent_complete84.66557
estimated_completion_time1318
cpu_time3829
total_elapsed_time19627
scheduler_id1
task_address0x00989018
reads1532
writes5
logical_reads30948
text_size2147483647
languageus_english
date_formatmdy
date_first7
quoted_identifier1
arithabort1
ansi_null_dflt_on1
ansi_defaults0
ansi_warnings1
ansi_padding1
ansi_nulls1
concat_null_yields_null1
transaction_isolation_level2
lock_timeout-1
deadlock_priority0
row_count248
prev_error0
nest_level1
granted_query_memory2140
executing_managed_code0
sp_who2 68

Note: I pivoted the data so it was easier to read.

SPID6868686868
StatusSUSPENDEDRUNNABLESUSPENDEDSUSPENDEDSUSPENDED
LoginEDGEWOOD-NB3\DBA    
HostNameEDGEWOOD-NB3EDGEWOOD-NB3EDGEWOOD-NB3EDGEWOOD-NB3EDGEWOOD-NB3
BlkBy  .  .  .  .  .
DBNameAdventureWorksAdventureWorksAdventureWorksAdventureWorksAdventureWorks
CommandDBCC TABLE CHECKDBCC TABLE CHECKDBCC TABLE CHECKDBCC TABLE CHECKDBCC TABLE CHECK
CPUTime72815164723592171
DiskIO1568500578648
LastBatch9/26/2007 10:149/26/2007 10:149/26/2007 10:149/26/2007 10:149/26/2007 10:14
ProgramNameMicrosoft SQL Server Management Studio – QueryMicrosoft SQL Server Management Studio – QueryMicrosoft SQL Server Management Studio – QueryMicrosoft SQL Server Management Studio – QueryMicrosoft SQL Server Management Studio – Query
SPID6868686868
REQUESTID00000

From the above output we can get a handle on the different things that are occurring.  The data from sp_who2 is helpful, but it is very limited.  The one thing we can see is that the DBCC has spawned multiple threads to run the command, but not a lot more.

One thing to note from the sys.dm_exec_requests is the percent_complete column (this is highlighted above).  This gives you an idea where things are as well as an idea that things are progressing as long as this value continues to increase.

Next Steps

Leave a Reply

Your email address will not be published. Required fields are marked *