forked from sergiisyrovatchenko/SQLIndexManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.cs
More file actions
204 lines (176 loc) · 8.67 KB
/
Copy pathIndex.cs
File metadata and controls
204 lines (176 loc) · 8.67 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Drawing;
namespace SQLIndexManager {
public class Index {
public string DatabaseName { get; set; }
public int ObjectId { get; set; }
public int IndexId { get; set; }
public string IndexName { get; set; }
public string ObjectName { get; set; }
public string SchemaName { get; set; }
public IndexType IndexType { get; set; }
public double? Fragmentation { get; set; }
public double? PageSpaceUsed { get; set; }
public long PagesCount { get; set; }
public long? PagesCountBefore { get; set; }
public long UnusedPagesCount { get; set; }
public int PartitionNumber { get; set; }
public long RowsCount { get; set; }
public long? TotalWrites { get; set; }
public long? TotalReads { get; set; }
public long? TotalScans { get; set; }
public long? TotalSeeks { get; set; }
public long? TotalLookups { get; set; }
public DateTime? LastUsage { get; set; }
public DataCompression DataCompression { get; set; }
public int FillFactor { get; set; }
public DateTime? IndexStats { get; set; }
public string FileGroupName { get; set; }
public string IndexColumns { get; set; }
public string IncludedColumns { get; set; }
public bool IsPartitioned { get; set; }
public bool IsUnique { get; set; }
public bool IsPK { get; set; }
public bool IsFiltered { get; set; }
public bool IsAllowReorganize { get; set; }
public bool IsAllowOnlineRebuild { get; set; }
public bool IsAllowCompression { get; set; }
public bool IsColumnstore => (IndexType == IndexType.ColumnstoreClustered || IndexType == IndexType.ColumnstoreNonClustered);
public string Error { get; set; }
public Image Progress { get; set; }
public DateTime? Duration { get; set; }
public IndexOp FixType { get; set; }
public WarningType? Warning { get; set; }
public bool IsSelected { get; set; }
public string GetQuery() {
string sql = string.Empty;
string indexName = IndexName.ToQuota();
string schemaName = SchemaName.ToQuota();
string objectName = ObjectName.ToQuota();
string partition = IsPartitioned ? PartitionNumber.ToString() : "ALL";
DataCompression compression = DataCompression;
if (IsColumnstore) {
if (FixType == IndexOp.RebuildColumnstore)
compression = DataCompression.Columnstore;
if (FixType == IndexOp.RebuildColumnstoreArchive)
compression = DataCompression.ColumnstoreArchive;
switch (FixType) {
case IndexOp.Rebuild:
case IndexOp.RebuildColumnstore:
case IndexOp.RebuildColumnstoreArchive:
sql = $"ALTER INDEX [{indexName}]\n " +
$"ON [{schemaName}].[{objectName}] REBUILD PARTITION = {partition}\n " +
$"WITH (DATA_COMPRESSION = {compression.ToDescription()}, MAXDOP = {Settings.Options.MaxDop});";
break;
case IndexOp.Reorganize:
sql = $"ALTER INDEX [{indexName}]\n " +
$"ON [{schemaName}].[{objectName}] REORGANIZE PARTITION = {partition};";
break;
case IndexOp.ReorganizeCompressAllRowGroup:
sql = $"ALTER INDEX [{indexName}]\n " +
$"ON [{schemaName}].[{objectName}] REORGANIZE PARTITION = {partition}\n " +
"WITH (COMPRESS_ALL_ROW_GROUPS = ON);";
break;
}
}
else {
if (FixType == IndexOp.RebuildPage)
compression = DataCompression.Page;
else if (FixType == IndexOp.RebuildRow)
compression = DataCompression.Row;
else if (FixType == IndexOp.RebuildNone)
compression = DataCompression.None;
switch (FixType) {
case IndexOp.CreateIndex:
bool isCreateOnline = Settings.ServerInfo.MajorVersion > Server.Sql2008
&& Settings.ServerInfo.IsOnlineRebuildAvailable
&& Settings.Options.Online;
sql = $"CREATE NONCLUSTERED INDEX [{indexName}]\n" +
$"ON [{schemaName}].[{objectName}] ({IndexColumns})\n" +
(string.IsNullOrEmpty(IncludedColumns) ? "" : $"INCLUDE({IncludedColumns})\n") +
$"WITH (SORT_IN_TEMPDB = {(Settings.Options.SortInTempDb ? Options.ON : Options.OFF)}, " +
$"ONLINE = {(isCreateOnline ? Options.ON : Options.OFF)}, " +
(Settings.Options.FillFactor.IsBetween(1, 100)
? $"FILLFACTOR = {Settings.Options.FillFactor}, "
: ""
) +
(Settings.Options.DataCompression == Options.DEFAULT
? ""
: $"DATA_COMPRESSION = {Settings.Options.DataCompression}, "
) +
(Settings.Options.NoRecompute == Options.DEFAULT
? ""
: $"STATISTICS_NORECOMPUTE = {Settings.Options.NoRecompute}, "
) +
$"MAXDOP = {Settings.Options.MaxDop});";
break;
case IndexOp.Rebuild:
case IndexOp.RebuildPage:
case IndexOp.RebuildRow:
case IndexOp.RebuildNone:
case IndexOp.RebuildOnline:
case IndexOp.RebuildFillFactorZero:
if (IndexType == IndexType.Heap) {
sql = $"ALTER TABLE [{schemaName}].[{objectName}] REBUILD PARTITION = {partition}\n " +
$"WITH (DATA_COMPRESSION = {compression.ToDescription()}, MAXDOP = {Settings.Options.MaxDop});";
}
else {
string onlineRebuild = "OFF";
if (FixType == IndexOp.RebuildOnline) {
if (Settings.Options.WaitAtLowPriority && Settings.ServerInfo.MajorVersion >= Server.Sql2014)
onlineRebuild = $"ON (" +
$"WAIT_AT_LOW_PRIORITY(MAX_DURATION = {Settings.Options.MaxDuration} MINUTES, " +
$"ABORT_AFTER_WAIT = {Settings.Options.AbortAfterWait}))";
else
onlineRebuild = Options.ON;
}
sql = $"ALTER INDEX [{indexName}]\n " +
$"ON [{schemaName}].[{objectName}] REBUILD PARTITION = {partition}\n " +
$"WITH (SORT_IN_TEMPDB = {(Settings.Options.SortInTempDb ? Options.ON : Options.OFF)}, " +
$"ONLINE = {onlineRebuild}, " +
(Settings.Options.NoRecompute == Options.DEFAULT
? ""
: $"STATISTICS_NORECOMPUTE = {Settings.Options.NoRecompute}, "
) +
(FixType == IndexOp.RebuildFillFactorZero
? "FILLFACTOR = 100, "
: (Settings.Options.FillFactor.IsBetween(1, 100)
? $"FILLFACTOR = {Settings.Options.FillFactor}, "
: ""
)
) +
$"DATA_COMPRESSION = {compression.ToDescription()}, " +
$"MAXDOP = {Settings.Options.MaxDop});";
}
break;
case IndexOp.Reorganize:
sql = $"ALTER INDEX [{indexName}]\n " +
$"ON [{schemaName}].[{objectName}] REORGANIZE PARTITION = {partition}\n " +
$"WITH (LOB_COMPACTION = {(Settings.Options.LobCompaction ? Options.ON : Options.OFF)});";
break;
case IndexOp.Disable:
sql = $"ALTER INDEX [{indexName}] ON [{schemaName}].[{objectName}] DISABLE;";
break;
case IndexOp.Drop:
sql = $"DROP INDEX [{indexName}] ON [{schemaName}].[{objectName}];";
break;
case IndexOp.UpdateStatsSample:
case IndexOp.UpdateStatsResample:
case IndexOp.UpdateStatsFull:
sql = $"UPDATE STATISTICS [{schemaName}].[{objectName}] [{indexName}]\n " + (
FixType == IndexOp.UpdateStatsSample
? $"WITH SAMPLE {Settings.Options.SampleStatsPercent} PERCENT;"
: (FixType == IndexOp.UpdateStatsFull ? "WITH FULLSCAN;" : "WITH RESAMPLE;")
);
break;
}
}
return sql;
}
public override string ToString() {
return $"{DatabaseName} | {SchemaName}.{ObjectName} " +
$"| {(string.IsNullOrEmpty(IndexName) ? "Heap" : $"{IndexName}")} {(IsPartitioned ? "[ " + PartitionNumber + " ] " : string.Empty)}" +
$"| {(Convert.ToDecimal(PagesCount) * 8).FormatSize()}".Replace("'", string.Empty);
}
}
}