Skip to content

Commit 4d7958d

Browse files
author
Sergey Syrovatchenko
committed
Bugfix during columnstore filtering, unused indexes and changes in UI
1 parent a3d73a4 commit 4d7958d

File tree

8 files changed

+133
-195
lines changed

8 files changed

+133
-195
lines changed

Forms/MainBox.Designer.cs

Lines changed: 66 additions & 112 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Forms/MainBox.resx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,7 @@
147147
<metadata name="IndexStats.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
148148
<value>False</value>
149149
</metadata>
150-
<metadata name="TotalWrites.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
151-
<value>False</value>
152-
</metadata>
153-
<metadata name="TotalReads.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
150+
<metadata name="TotalUpdates.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
154151
<value>False</value>
155152
</metadata>
156153
<metadata name="LastUsage.GenerateMember" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
[assembly: AssemblyCopyright("Sergii Syrovatchenko")]
88
[assembly: AssemblyTrademark("")]
99
[assembly: AssemblyCulture("")]
10-
[assembly: AssemblyVersion("1.0.0.61")]
11-
[assembly: AssemblyFileVersion("1.0.0.61")]
10+
[assembly: AssemblyVersion("1.0.0.62")]
11+
[assembly: AssemblyFileVersion("1.0.0.62")]

Properties/Resources.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Resources.resx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@
226226
<data name="TotalLookups" xml:space="preserve">
227227
<value>TotalLookups</value>
228228
</data>
229-
<data name="TotalWrites" xml:space="preserve">
230-
<value>TotalWrites</value>
229+
<data name="TotalUpdates" xml:space="preserve">
230+
<value>TotalUpdates</value>
231231
</data>
232232
<data name="IndexColumns" xml:space="preserve">
233233
<value>IndexColumns</value>

Server/Index.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public class Index {
2121
public int PartitionNumber { get; set; }
2222
public long RowsCount { get; set; }
2323

24-
public long? TotalWrites { get; set; }
25-
public long? TotalReads { get; set; }
24+
public long? TotalUpdates { get; set; }
2625
public long? TotalScans { get; set; }
2726
public long? TotalSeeks { get; set; }
2827
public long? TotalLookups { get; set; }

Server/Query.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,6 @@ SET NOCOUNT ON
77
SET ARITHABORT ON
88
SET NUMERIC_ROUNDABORT OFF
99
10-
IF OBJECT_ID('tempdb.dbo.#AllocationUnits') IS NOT NULL
11-
DROP TABLE #AllocationUnits
12-
13-
CREATE TABLE #AllocationUnits (
14-
ContainerID BIGINT PRIMARY KEY
15-
, ReservedPages BIGINT NOT NULL
16-
, UsedPages BIGINT NOT NULL
17-
)
18-
19-
INSERT INTO #AllocationUnits (ContainerID, ReservedPages, UsedPages)
20-
SELECT [container_id]
21-
, SUM([total_pages])
22-
, SUM([used_pages])
23-
FROM sys.allocation_units WITH(NOLOCK)
24-
GROUP BY [container_id]
25-
HAVING SUM([total_pages]) BETWEEN @MinIndexSize AND @MaxIndexSize
26-
2710
IF OBJECT_ID('tempdb.dbo.#ExcludeList') IS NOT NULL
2811
DROP TABLE #ExcludeList
2912
@@ -83,17 +66,29 @@ SELECT p.ObjectID
8366
, PartitionNumber = p.[partition_number]
8467
, DataCompression = MAX(p.[data_compression])
8568
, RowsCount = ISNULL(SUM(p.[rows]), 0)
86-
, PagesCount = SUM(a.ReservedPages)
87-
, UnusedPagesCount = SUM(CASE WHEN ABS(a.ReservedPages - a.UsedPages) > 32 THEN a.ReservedPages - a.UsedPages ELSE 0 END)
88-
FROM #AllocationUnits a
89-
JOIN #Partitions p ON a.ContainerID = p.[partition_id]
69+
, PagesCount = SUM(a.[total_pages])
70+
, UnusedPagesCount = SUM(CASE WHEN ABS(a.[total_pages] - a.[used_pages]) > 32 THEN a.[total_pages] - a.[used_pages] ELSE 0 END)
71+
FROM #Partitions p
72+
JOIN (
73+
SELECT [container_id]
74+
, [total_pages] = SUM([total_pages])
75+
, [used_pages] = SUM([used_pages])
76+
FROM sys.allocation_units WITH(NOLOCK)
77+
WHERE [total_pages] > 0
78+
GROUP BY [container_id]
79+
) a ON a.[container_id] = p.[partition_id]
9080
GROUP BY p.[object_id]
9181
, p.[index_id]
9282
, p.[partition_number]
9383
) p
9484
JOIN sys.indexes i WITH(NOLOCK) ON i.[object_id] = p.ObjectID AND i.[index_id] = p.IndexID {6}
9585
WHERE i.[type] IN ({0})
9686
AND i.[object_id] > 255
87+
AND (
88+
(i.[type] IN (0, 1, 2) AND p.PagesCount BETWEEN @MinIndexSize AND @MaxIndexSize)
89+
OR
90+
(i.[type] IN (5, 6) AND p.PagesCount <= @MaxIndexSize)
91+
)
9792
9893
DECLARE @files TABLE (ID INT PRIMARY KEY)
9994
INSERT INTO @files
@@ -255,7 +250,7 @@ SELECT i.ObjectID
255250
, i.RowsCount
256251
, i.IndexType
257252
, i.IsAllowPageLocks
258-
, u.TotalWrites
253+
, u.TotalUpdates
259254
, u.TotalSeeks
260255
, u.TotalScans
261256
, u.TotalLookups
@@ -407,7 +402,7 @@ AND [object_id] NOT IN (SELECT * FROM #ExcludeList)
407402
public const string IndexStats = @"
408403
SELECT ObjectID = [object_id]
409404
, IndexID = [index_id]
410-
, TotalWrites = NULLIF([user_updates], 0)
405+
, TotalUpdates = NULLIF([user_updates], 0)
411406
, TotalSeeks = NULLIF([user_seeks], 0)
412407
, TotalScans = NULLIF([user_scans], 0)
413408
, TotalLookups = NULLIF([user_lookups], 0)
@@ -428,7 +423,7 @@ FROM sys.dm_db_index_usage_stats WITH(NOLOCK)
428423
public const string IndexStatsAzureMaster = @"
429424
SELECT ObjectID = NULL
430425
, IndexID = NULL
431-
, TotalWrites = NULL
426+
, TotalUpdates = NULL
432427
, TotalSeeks = NULL
433428
, TotalScans = NULL
434429
, TotalLookups = NULL

Server/QueryEngine.cs

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -183,47 +183,43 @@ public static List<Index> GetIndexes(SqlConnection connection) {
183183

184184
DateTime? lastWrite = _.Field<DateTime?>(Resources.LastWrite);
185185
DateTime? lastRead = _.Field<DateTime?>(Resources.LastRead);
186-
long? seeks = _.Field<long?>(Resources.TotalSeeks);
187-
long? scans = _.Field<long?>(Resources.TotalScans);
188-
long? lookups = _.Field<long?>(Resources.TotalLookups);
189186

190187
Index index = new Index {
191-
DatabaseName = connection.Database,
192-
ObjectId = _.Field<int>(Resources.ObjectID),
193-
IndexId = _.Field<int>(Resources.IndexID),
194-
IndexName = _.Field<string>(Resources.IndexName),
195-
ObjectName = _.Field<string>(Resources.ObjectName),
196-
SchemaName = _.Field<string>(Resources.SchemaName),
197-
PagesCount = _.Field<long>(Resources.PagesCount),
198-
UnusedPagesCount = _.Field<long>(Resources.UnusedPagesCount),
199-
PartitionNumber = _.Field<int>(Resources.PartitionNumber),
200-
RowsCount = _.Field<long>(Resources.RowsCount),
201-
FileGroupName = _.Field<string>(Resources.FileGroupName),
202-
IndexType = indexType,
203-
IsPartitioned = _.Field<bool>(Resources.IsPartitioned),
204-
IsUnique = _.Field<bool>(Resources.IsUnique),
205-
IsPK = _.Field<bool>(Resources.IsPK),
206-
IsFiltered = _.Field<bool>(Resources.IsFiltered),
207-
FillFactor = _.Field<int>(Resources.FillFactor),
208-
IndexStats = _.Field<DateTime?>(Resources.IndexStats),
209-
TotalWrites = _.Field<long?>(Resources.TotalWrites),
210-
TotalReads = (seeks ?? 0) + (scans ?? 0) > 0 ? (seeks ?? 0) + (scans ?? 0) : (long?)null,
211-
TotalSeeks = seeks,
212-
TotalScans = scans,
213-
TotalLookups = lookups,
214-
LastWrite = lastWrite,
215-
LastRead = lastRead,
216-
LastUsage = Nullable.Compare(lastWrite, lastRead) > 0 ? lastWrite : lastRead,
217-
CreateDate = _.Field<DateTime>(Resources.CreateDate),
218-
ModifyDate = _.Field<DateTime>(Resources.ModifyDate),
219-
DataCompression = (DataCompression)_.Field<byte>(Resources.DataCompression),
220-
Fragmentation = _.Field<double?>(Resources.Fragmentation),
221-
PageSpaceUsed = _.Field<double?>(Resources.PageSpaceUsed),
222-
IsAllowReorganize = _.Field<bool>(Resources.IsAllowPageLocks) && indexType != IndexType.HEAP,
223-
IsAllowOnlineRebuild = isOnlineRebuild,
224-
IsAllowCompression = Settings.ServerInfo.IsCompressionAvailable && !_.Field<bool>(Resources.IsSparse),
225-
IndexColumns = _.Field<string>(Resources.IndexColumns),
226-
IncludedColumns = _.Field<string>(Resources.IncludedColumns)
188+
DatabaseName = connection.Database,
189+
ObjectId = _.Field<int>(Resources.ObjectID),
190+
IndexId = _.Field<int>(Resources.IndexID),
191+
IndexName = _.Field<string>(Resources.IndexName),
192+
ObjectName = _.Field<string>(Resources.ObjectName),
193+
SchemaName = _.Field<string>(Resources.SchemaName),
194+
PagesCount = _.Field<long>(Resources.PagesCount),
195+
UnusedPagesCount = _.Field<long>(Resources.UnusedPagesCount),
196+
PartitionNumber = _.Field<int>(Resources.PartitionNumber),
197+
RowsCount = _.Field<long>(Resources.RowsCount),
198+
FileGroupName = _.Field<string>(Resources.FileGroupName),
199+
IndexType = indexType,
200+
IsPartitioned = _.Field<bool>(Resources.IsPartitioned),
201+
IsUnique = _.Field<bool>(Resources.IsUnique),
202+
IsPK = _.Field<bool>(Resources.IsPK),
203+
IsFiltered = _.Field<bool>(Resources.IsFiltered),
204+
FillFactor = _.Field<int>(Resources.FillFactor),
205+
IndexStats = _.Field<DateTime?>(Resources.IndexStats),
206+
TotalUpdates = _.Field<long?>(Resources.TotalUpdates),
207+
TotalSeeks = _.Field<long?>(Resources.TotalSeeks),
208+
TotalScans = _.Field<long?>(Resources.TotalScans),
209+
TotalLookups = _.Field<long?>(Resources.TotalLookups),
210+
LastWrite = lastWrite,
211+
LastRead = lastRead,
212+
LastUsage = Nullable.Compare(lastWrite, lastRead) > 0 ? lastWrite : lastRead,
213+
CreateDate = _.Field<DateTime>(Resources.CreateDate),
214+
ModifyDate = _.Field<DateTime>(Resources.ModifyDate),
215+
DataCompression = (DataCompression)_.Field<byte>(Resources.DataCompression),
216+
Fragmentation = _.Field<double?>(Resources.Fragmentation),
217+
PageSpaceUsed = _.Field<double?>(Resources.PageSpaceUsed),
218+
IsAllowReorganize = _.Field<bool>(Resources.IsAllowPageLocks) && indexType != IndexType.HEAP,
219+
IsAllowOnlineRebuild = isOnlineRebuild,
220+
IsAllowCompression = Settings.ServerInfo.IsCompressionAvailable && !_.Field<bool>(Resources.IsSparse),
221+
IndexColumns = _.Field<string>(Resources.IndexColumns),
222+
IncludedColumns = _.Field<string>(Resources.IncludedColumns)
227223
};
228224

229225
indexes.Add(index);
@@ -251,8 +247,6 @@ public static List<Index> GetIndexes(SqlConnection connection) {
251247
.Replace("]", string.Empty)
252248
.Replace(" ", string.Empty).Truncate(240);
253249

254-
long? seeks = _.Field<long?>(Resources.TotalSeeks);
255-
long? scans = _.Field<long?>(Resources.TotalScans);
256250
DateTime? lastRead = _.Field<DateTime?>(Resources.LastRead);
257251

258252
Index index = new Index {
@@ -266,9 +260,8 @@ public static List<Index> GetIndexes(SqlConnection connection) {
266260
FileGroupName = "PRIMARY",
267261
IndexType = IndexType.MISSING_INDEX,
268262
IndexStats = _.Field<DateTime?>(Resources.IndexStats),
269-
TotalReads = (seeks ?? 0) + (scans ?? 0) > 0 ? (seeks ?? 0) + (scans ?? 0) : (long?)null,
270-
TotalSeeks = seeks,
271-
TotalScans = scans,
263+
TotalSeeks = _.Field<long?>(Resources.TotalSeeks),
264+
TotalScans = _.Field<long?>(Resources.TotalScans),
272265
LastRead = lastRead,
273266
LastUsage = lastRead,
274267
DataCompression = DataCompression.NONE,
@@ -404,8 +397,8 @@ public static void FindUnusedIndexes(List<Index> indexes) {
404397
foreach (Index ix in indexes.Where(
405398
_ => !_.IsPartitioned
406399
&& _.Warning == null
407-
&& _.TotalWrites > 50000
408-
&& (_.TotalReads ?? 0) < _.TotalWrites / 20
400+
&& _.TotalUpdates > 50000
401+
&& (_.TotalScans ?? 0) + (_.TotalSeeks ?? 0) < (_.TotalUpdates ?? 0) / 20
409402
&& (_.IndexType == IndexType.CLUSTERED || _.IndexType == IndexType.NONCLUSTERED || _.IndexType == IndexType.HEAP))) {
410403
ix.Warning = WarningType.UNUSED;
411404
}

0 commit comments

Comments
 (0)