日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql查看索引创建进度_SQL Server查看索引重建、重组索引进度

發(fā)布時間:2023/12/2 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql查看索引创建进度_SQL Server查看索引重建、重组索引进度 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

相信很多SQL Server DBA或開發(fā)人員在重建或重組大表索引時,都會相當(dāng)郁悶,不知道索引重建的進(jìn)度,這個對于DBA完全是一個黑盒子,對于系統(tǒng)負(fù)載非常大的系統(tǒng)或維護(hù)窗口較短的系統(tǒng),你會遇到一些挑戰(zhàn)。例如,你創(chuàng)建索引的時候,很多會話被阻塞,你只能取消創(chuàng)建索引的任務(wù)。查看這些索引維護(hù)操作的進(jìn)度、預(yù)估時間對于我們有較大的意義,需要根據(jù)這個做一些決策。下面我們來看看看看如何獲取CREATE INDEX、ALTER INDEX REBUILD、ALTER INDEX ORGANIZE的進(jìn)度。

索引重組

從SQL Server 2008開始,有個DMV視圖sys.dm_exec_requests,里面有個字段percent_complete表示以下命令完成的工作的百分比,這里面就包括索引重組(ALTER INDEX REORGANIZE),這其中不包括ALTER INDEX REBUILD,可以查看索引重組(ALTER INDEX ORGANIZE)完成的百分比。也就是說在SQL Server 2008之前是無法獲取索引重組的進(jìn)度情況的。

percent_complete real Percentage of work completed for the following commands:

ALTER INDEX REORGANIZE

AUTO_SHRINK option with ALTER DATABASE

BACKUP DATABASE

DBCC CHECKDB

DBCC CHECKFILEGROUP

DBCC CHECKTABLE

DBCC INDEXDEFRAG

DBCC SHRINKDATABASE

DBCC SHRINKFILE

RECOVERY

RESTORE DATABASE

ROLLBACK

TDE ENCRYPTION

Is not nullable.

測試環(huán)境:SQL Server 2008、2017 RTM CU13

SELECT? er.session_id ,er.blocking_session_id ,er.status ,er.command ,DB_NAME(er.database_id) DB_name ,er.wait_type ,et.text SQLText ,er.percent_completeFROM??? sys.dm_exec_requests erCROSS APPLY sys.dm_exec_sql_text(er.sql_handle) etWHERE?? er.session_id = 57AND er.session_id <> @@SPID;

索引重建

上面DMV視圖sys.dm_exec_requests是否也可以查看索引重建的進(jìn)度呢?答案是不行,測試發(fā)現(xiàn)percent_complete這個進(jìn)度一直為0,那么要如何查看索引重建(INDEX REBUILD)的進(jìn)度呢?

不過自SQL Server 2014開始,SQL Server提供了一個新特性:sys.dm_exec_query_profiles,它可以實時監(jiān)控正在執(zhí)行的查詢的進(jìn)度情況(Monitors real time query progress while the query is in execution)。當(dāng)然,需要啟用實時查詢監(jiān)控才行。一般只需啟用會話級別的實時查詢監(jiān)控,可以通過啟用SET STATISTICS XML ON;或SET STATISTICS PROFILE ON;開啟。而從SQL Server 2016 (13.x)SP1開始,您可以或者開啟跟蹤標(biāo)志7412或使用query_thread_profile擴(kuò)展的事件。下面是官方文檔的描述:

In SQL Server 2014 (12.x) SP2 and later use SET STATISTICS PROFILE ON or SET STATISTICS XML ON together with the query under investigation. This enables the profiling infrastructure and produces results in the DMV for the session where the SET command was executed. If you are investigating a query running from an application and cannot enable SET options with it, you can create an Extended Event using the query_post_execution_showplan event which will turn on the profiling infrastructure.

In SQL Server 2016 (13.x) SP1, you can either turn ontrace flag 7412or use the query_thread_profile extended event.

--Configure query for profiling with sys.dm_exec_query_profiles

SET STATISTICS PROFILE ON;

GO

--Or enable query profiling globally under SQL Server 2016 SP1 or above

DBCC TRACEON (7412, -1);

GO

ALTERINDEXYour_Index_NameONYour_Table_NameREBUILD;

GO

DECLARE @SPID INT = 53;;WITH agg AS(SELECT?SUM(qp.[row_count]) AS [RowsProcessed],SUM(qp.[estimate_row_count]) AS [TotalRows],MAX(qp.last_active_time) - MIN(qp.first_active_time) AS [ElapsedMS],MAX(IIF(qp.[close_time] = 0 AND qp.[first_row_time] > 0,[physical_operator_name],N'')) AS [CurrentStep]FROM sys.dm_exec_query_profiles qpWHERE qp.[physical_operator_name] IN (N'Table Scan', N'Clustered Index Scan', N'Sort' , N'Index Scan')AND?? qp.[session_id] = @SPID), comp AS(SELECT *,([TotalRows] - [RowsProcessed]) AS [RowsLeft],([ElapsedMS] / 1000.0) AS [ElapsedSeconds]FROM?? agg)SELECT [CurrentStep],[TotalRows],[RowsProcessed],[RowsLeft],CONVERT(DECIMAL(5, 2),(([RowsProcessed] * 1.0) / [TotalRows]) * 100) AS [PercentComplete],[ElapsedSeconds],(([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]) AS [EstimatedSecondsLeft],DATEADD(SECOND,(([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]),GETDATE()) AS [EstimatedCompletionTime]FROM?? comp;

注意事項:在SQL Server 2016 SP1之前,如果要使用sys.dm_exec_query_profiles查看索引重建的進(jìn)度,那么就必須在索引重建之前設(shè)置SET STATISTICS PROFILE ON or SET STATISTICS XML ON。 而自

SQL Server 2016 SP1之后,可以使用DBCC TRACEON (7412, -1);開啟全局會話的跟蹤標(biāo)記,或者開啟某個會話的跟蹤標(biāo)記,當(dāng)然如果要使用sys.dm_exec_query_profiles查看索引重建的進(jìn)度,也必須開啟7412跟蹤標(biāo)記

,然后重建索引,否則也沒有值。

注意事項::索引重組時,sys.dm_exec_query_profiles中沒有數(shù)據(jù)。所以sys.dm_exec_query_profiles不能用來查看索引重組的進(jìn)度。

新建索引

新建索引進(jìn)度的查詢,也可以使用下面SQL語句。這里不做展開。

DECLARE @SPID INT = 56;;WITH agg AS(SELECT?SUM(qp.[row_count]) AS [RowsProcessed],SUM(qp.[estimate_row_count]) AS [TotalRows],MAX(qp.last_active_time) - MIN(qp.first_active_time) AS [ElapsedMS],MAX(IIF(qp.[close_time] = 0 AND qp.[first_row_time] > 0,[physical_operator_name],N'')) AS [CurrentStep]FROM sys.dm_exec_query_profiles qpWHERE qp.[physical_operator_name] IN (N'Table Scan', N'Clustered Index Scan', N'Sort' , N'Index Scan')ANDqp.[session_id] = @SPID), comp AS(SELECT *,([TotalRows] - [RowsProcessed]) AS [RowsLeft],([ElapsedMS] / 1000.0) AS [ElapsedSeconds]FROM?? agg)SELECT [CurrentStep],[TotalRows],[RowsProcessed],[RowsLeft],CONVERT(DECIMAL(5, 2),(([RowsProcessed] * 1.0) / [TotalRows]) * 100) AS [PercentComplete],[ElapsedSeconds],(([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]) AS [EstimatedSecondsLeft],DATEADD(SECOND,(([ElapsedSeconds] / [RowsProcessed]) * [RowsLeft]),GETDATE()) AS [EstimatedCompletionTime]FROM?? comp;SELECTnode_id,physical_operator_name,SUM(row_count) row_count,SUM(estimate_row_count) AS estimate_row_count,CAST(SUM(row_count)*100 AS?float)/SUM(estimate_row_count)? as estimate_percent_completeFROM sys.dm_exec_query_profilesWHERE session_id=@SPIDGROUP?BY node_id,physical_operator_nameORDER?BY node_id desc;

參考資料:

https://docs.microsoft.com/zh-cn/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-query-profiles-transact-sql?view=sql-server-2017

https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/sys-dm-exec-requests-transact-sql?view=sql-server-2017

https://dba.stackexchange.com/questions/139191/sql-server-how-to-track-progress-of-create-index-command

https://support.microsoft.com/zh-cn/help/4053291/fix-sys-dm-exec-query-profiles-dmv-returns-wrong-estimate-row-count-in

https://blogs.msdn.microsoft.com/sql_pfe_blog/2016/12/22/create-index-monitoring-progress/

總結(jié)

以上是生活随笔為你收集整理的mysql查看索引创建进度_SQL Server查看索引重建、重组索引进度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。