-
SQL SERVER 2008 R2 重建索引的方法
参考sys.dm_db_index_physical_stats
检查索引碎片情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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 重新生成或重新组织索引 (来源于联机帮助) 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 |
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式