Everybody All Around the World, Let Me Tell You What I Just Learned, I Can See What an Online Index Rebuild is Doing!

I truly don’t know when it happened, but over the last while I have noticed that the percentage complete on indexes has disappeared when I run sp_whoisactive. It makes me so sad! I used that functionality often to track how things were progressing in my databases. At first I thought it was a version thing and it would come back, then I wondered if it was only when I use “ONLINE = ON”, but I am seeing it blank more and more. It has left me feeling like I am missing something and today, I finally did the digging to learn how to get that visibility back.

I started with trying this:

SELECT
r.session_id,
r.command,
r.percent_complete,
DATEADD(SECOND, r.estimated_completion_time / 1000, GETDATE()) AS EstimatedCompletion,
r.wait_type,
r.wait_time / 1000 AS WaitTimeSec,
LEFT(t.text, 100) AS QueryText
FROM sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) t
WHERE r.command LIKE '%INDEX%'
ORDER BY r.percent_complete DESC;

The results were a little disappointing. The percent_complete was zero and the EstimatedCompletion was the same time as when I ran the query:

Then I tried this:

SELECT
session_id,
physical_operator_name,
SUM(row_count) AS RowsProcessed,
SUM(estimate_row_count) AS EstimatedRows,
CAST(SUM(row_count) * 100.0 / NULLIF(SUM(estimate_row_count), 0) AS DECIMAL(5,2)) AS PctComplete
FROM sys.dm_exec_query_profiles
WHERE session_id IN (
SELECT session_id FROM sys.dm_exec_requests WHERE command LIKE '%INDEX%'
)
GROUP BY session_id, physical_operator_name
ORDER BY session_id;

These are the results! Look! PctComplete has information:

PctComplete was updating! I kept hitting refresh and it was nice to see progress happening:

Since I was rebuilding all the indexes on the table, I could tell when it moved to an index that was a little more complex:

How cool is that? It helps me to feel more calm especially when indexes are taking a long time and I have no idea if progress is being made.

The song for this post is Electric Light Orchestra – All Over the World and all over the SQL database world you can see some progress tonight!

Unknown's avatar

About andreaallred

SQL Server and helping people is my passion. If I can make someone laugh, I know I have made a difference.

8 thoughts on “Everybody All Around the World, Let Me Tell You What I Just Learned, I Can See What an Online Index Rebuild is Doing!

  1. […] Andrea Allred goes searching for the truth: […]

  2. Suggestion Box's avatar Suggestion Box says:

    Try again with a resumable index.

    • andreaallred's avatar andreaallred says:

      Thank you for the great call out. This works great if you can use it, but if you don’t have Enterprise or can’t use resumable for any other reason, this gives another solution.

  3. Arjan's avatar Arjan says:

    Nice, I wanted to see the table being worked on so added this:

    SELECT qp.session_id, OBJECT_NAME(qp.object_id, qp.database_id) AS TableName, er.command, qp.physical_operator_name, SUM(qp.row_count) AS RowsProcessed, SUM(qp.estimate_row_count) AS EstimatedRows, CAST(SUM(qp.row_count) * 100.0 / NULLIF(SUM(qp.estimate_row_count), 0) AS DECIMAL(5, 2)) AS PctComplete, SUBSTRING( st.text, (er.statement_start_offset / 2) + 1, ((CASE er.statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE er.statement_end_offset END – er.statement_start_offset ) / 2 ) + 1 ) AS ExecutingStatement FROM sys.dm_exec_query_profiles qp INNER JOIN sys.dm_exec_requests er ON qp.session_id = er.session_id CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) st WHERE er.command LIKE ‘%INDEX%’ OR er.command LIKE ‘ALTER%’ GROUP BY qp.session_id, qp.object_id, qp.database_id, er.command, qp.physical_operator_name, st.text, er.statement_start_offset, er.statement_end_offset ORDER BY qp.session_id;

    Brgds,

    Arjan

  4. Henrik Staun Poulsen's avatar Henrik Staun Poulsen says:

    I’ve found that DECIMAL(5,2) is on the smallish side when your stats is out.May I suggest an update to DECIMAL(15,2)?

  5. Henrik Staun Poulsen's avatar Henrik Staun Poulsen says:

    My stats is out of order, so I had to change decimal(5,2) to decimal(15,2). Moved it to 15 as it was the least typing.Thank you for blogging about this finding. Most useful.

Leave a comment