主頁(yè) > 知識(shí)庫(kù) > SQL SERVER 2008 R2 重建索引的方法

SQL SERVER 2008 R2 重建索引的方法

熱門(mén)標(biāo)簽:濟(jì)南電銷(xiāo)外呼防封卡怎么樣 南京外呼系統(tǒng)租用 高德地圖標(biāo)注生成 電銷(xiāo)外呼系統(tǒng)違規(guī) 寧夏外呼系統(tǒng)方案 怎樣在地圖標(biāo)注自己的信息 400電話(huà)怎么申請(qǐng)收費(fèi)標(biāo)準(zhǔn) 400電話(huà)辦理2273649Z空間 智能語(yǔ)音外呼系統(tǒng)打電話(huà)

參考sys.dm_db_index_physical_stats

檢查索引碎片情況

1.SELECT
2.OBJECT_NAME(object_id) as objectname,
3.object_id AS objectid,
4.index_id AS indexid,
5.partition_number AS partitionnum,
6.avg_fragmentation_in_percent AS fra
7.FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, ‘LIMITED')
8.WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0;
9. 
10.使用腳本中的 sys.dm_db_index_physical_stats 重新生成或重新組織索引 (來(lái)源于聯(lián)機(jī)幫助)
11. 
12.SET NOCOUNT ON;
13.DECLARE @objectid int;
14.DECLARE @indexid int;
15.DECLARE @partitioncount bigint;
16.DECLARE @schemaname nvarchar(130);
17.DECLARE @objectname nvarchar(130);
18.DECLARE @indexname nvarchar(130);
19.DECLARE @partitionnum bigint;
20.DECLARE @partitions bigint;
21.DECLARE @frag float;
22.DECLARE @command nvarchar(4000);
23.– Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function
24.– and convert object and index IDs to names.
25.SELECT
26.object_id AS objectid,
27.index_id AS indexid,
28.partition_number AS partitionnum,
29.avg_fragmentation_in_percent AS frag
30.INTO #work_to_do
31.FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, ‘LIMITED')
32.WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0;
33.– Declare the cursor for the list of partitions to be processed.
34.DECLARE partitions CURSOR FOR SELECT * FROM #work_to_do;
35.– Open the cursor.
36.OPEN partitions;
37.– Loop through the partitions.
38.WHILE (1=1)
39.BEGIN;
40.FETCH NEXT
41.FROM partitions
42.INTO @objectid, @indexid, @partitionnum, @frag;
43.IF @@FETCH_STATUS  0 BREAK;
44.SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name)
45.FROM sys.objects AS o
46.JOIN sys.schemas as s ON s.schema_id = o.schema_id
47.WHERE o.object_id = @objectid;
48.SELECT @indexname = QUOTENAME(name)
49.FROM sys.indexes
50.WHERE object_id = @objectid AND index_id = @indexid;
51.SELECT @partitioncount = count (*)
52.FROM sys.partitions
53.WHERE object_id = @objectid AND index_id = @indexid;
54.– 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.
55.IF @frag  30.0
56.SET @command = N‘ALTER INDEX ‘ + @indexname + N‘ ON ‘ + @schemaname + N‘.' + @objectname + N‘ REORGANIZE';
57.IF @frag >= 30.0
58.SET @command = N‘ALTER INDEX ‘ + @indexname + N‘ ON ‘ + @schemaname + N‘.' + @objectname + N‘ REBUILD';
59.IF @partitioncount > 1
60.SET @command = @command + N‘ PARTITION=' + CAST(@partitionnum AS nvarchar(10));
61.EXEC (@command);
62.PRINT N‘Executed: ‘ + @command;
63.END;
64.– Close and deallocate the cursor.
65.CLOSE partitions;
66.DEALLOCATE partitions;
67.– Drop the temporary table.
68.DROP TABLE #work_to_do;
69.GO
您可能感興趣的文章:
  • sqlserver索引的原理及索引建立的注意事項(xiàng)小結(jié)
  • SQL Server 索引介紹
  • SQL Server 聚集索引和非聚集索引的區(qū)別分析
  • SQLSERVER全文目錄全文索引的使用方法和區(qū)別講解
  • SQLSERVER 創(chuàng)建索引實(shí)現(xiàn)代碼
  • SQLSERVER聚集索引和主鍵(Primary Key)的誤區(qū)認(rèn)識(shí)
  • sqlserver 索引的一些總結(jié)
  • SQL Server全文索引服務(wù)
  • 提升SQL Server速度 整理索引碎片
  • SqlServer索引的原理與應(yīng)用詳解

標(biāo)簽:惠州 長(zhǎng)白山 平頂山 貴港 茂名 仙桃 潛江 唐山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQL SERVER 2008 R2 重建索引的方法》,本文關(guān)鍵詞  SQL,SERVER,2008,重建,索引,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQL SERVER 2008 R2 重建索引的方法》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于SQL SERVER 2008 R2 重建索引的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章