|
1 | 1 | # Script to Get Fragmentation Stats for All Dbs on Multiple Servers (in Parallel Jobs)
|
2 |
| -$dbServers = @('TUL1CIPBIAPP1','TUL1CIPBIAPP2','TUL1CIPBIAPP3','TUL1CIPXDB18','TUL1SUB2008'); |
| 2 | +$dbServers = @('TUL1CIPCNPDB1','TUL1CIPEDB2','TUL1CIPXDB13'); |
3 | 3 |
|
4 | 4 | $dbQuery = @"
|
5 | 5 | select d.name
|
6 | 6 | from sys.databases as d
|
7 | 7 | where not(d.source_database_id IS NOT NULL or d.state_desc = 'OFFLINE' or d.database_id <= 4)
|
| 8 | +and d.compatibility_level = (select m.compatibility_level from sys.databases as m where m.name = 'model') |
8 | 9 | "@;
|
9 | 10 |
|
10 | 11 | $IndexQuery = @"
|
11 | 12 | select @@serverName as ServerName,
|
12 | 13 | db_name(ips.database_id) as DataBaseName,
|
13 |
| - object_name(ips.object_id) as ObjectName, |
14 |
| - sch.name as SchemaName, |
| 14 | + sch.name + '.' + object_name(ips.object_id) as TableName, |
15 | 15 | ind.name as IndexName,
|
16 | 16 | ips.index_type_desc,
|
| 17 | + ips.alloc_unit_type_desc, |
| 18 | + ODefrag.UpdatedTime as OlaIndexDefrag, |
17 | 19 | avg_fragmentation_in_percent as avg_fragmentation,
|
18 | 20 | avg_page_space_used_in_percent,
|
19 | 21 | page_count,
|
20 |
| - ps.row_count, |
21 |
| - STATS_DATE(ind.object_id, ind.index_id) AS StatsUpdated |
| 22 | + ps.row_count |
| 23 | + --,sts.name as StatsName |
| 24 | + ,sp.last_updated as stats_last_updated |
| 25 | + ,sp.rows as stats_rows |
| 26 | + ,sp.modification_counter as stats_modification_counter |
| 27 | + ,STATS_DATE(ind.object_id, ind.index_id) AS StatsUpdated |
| 28 | + ,OSts.UpdatedTime AS OlaStatsUpdated |
| 29 | + ,[DeFrag_Filter = {PageCount >= 1000}] = case when ips.page_count >= 100 then 'Yes' else 'No' end |
| 30 | + ,[Stats_Filter = {ModifiedStatistics}] = case when sp.modification_counter > 0 then 'Yes' else 'No' end |
| 31 | + ,[Stats_Filter = {@StatisticsModificationLevel}] = case when SQRT(ps.row_count * 1000) >= sp.modification_counter then 'Yes' else 'No' end |
22 | 32 | from sys.dm_db_index_physical_stats(DB_ID(),NULL,NULL,NULL,'LIMITED') as ips
|
23 |
| - inner join sys.tables as tbl |
24 |
| - on ips.object_id = tbl.object_id |
25 |
| - inner join sys.schemas as sch |
26 |
| - on tbl.schema_id = sch.schema_id |
27 |
| - inner join sys.indexes as ind |
28 |
| - on ips.index_id = ind.index_id and |
29 |
| - ips.object_id = ind.object_id |
30 |
| - inner join sys.dm_db_partition_stats as ps |
31 |
| - on ps.object_id = ips.object_id and |
32 |
| - ps.index_id = ips.index_id |
33 |
| -where page_count >= 1000; |
| 33 | +inner join sys.indexes as ind on ips.index_id = ind.index_id and ips.object_id = ind.object_id |
| 34 | +inner join sys.tables as tbl on ips.object_id = tbl.object_id |
| 35 | +inner join sys.schemas as sch on tbl.schema_id = sch.schema_id |
| 36 | +inner join sys.dm_db_partition_stats as ps on ps.object_id = ips.object_id and ps.index_id = ips.index_id |
| 37 | +left join sys.stats as sts on sts.object_id = ind.object_id |
| 38 | +cross apply sys.dm_db_stats_properties(ind.object_id, sts.stats_id) as sp |
| 39 | +outer apply (SELECT MAX(cl.EndTime) as UpdatedTime FROM DBA..CommandLog as cl WHERE cl.DatabaseName = DB_NAME() and tbl.name = cl.ObjectName and sch.name = cl.SchemaName and ind.name = cl.IndexName AND cl.CommandType = 'UPDATE_STATISTICS') AS OSts |
| 40 | +outer apply (SELECT MAX(cl.EndTime) as UpdatedTime FROM DBA..CommandLog as cl WHERE cl.DatabaseName = DB_NAME() and tbl.name = cl.ObjectName and sch.name = cl.SchemaName and ind.name = cl.IndexName AND cl.CommandType = 'ALTER_INDEX') AS ODefrag |
| 41 | +where sts.name = ind.name |
| 42 | +order by avg_fragmentation DESC |
34 | 43 | "@;
|
35 | 44 |
|
36 | 45 | foreach($srv in $dbServers) {
|
@@ -68,6 +77,7 @@ while($Jobs_Yet2Process -ne $null); # keep looping if jobs are still in progress
|
68 | 77 | # Save to Excel
|
69 | 78 | $IndexAnalysisResult | Export-Excel -Path C:\Temp\IndexAnalysisResult.xlsx -WorksheetName 'IndexAnalysisResult';
|
70 | 79 |
|
| 80 | + |
71 | 81 | # Find Jobs with Failures
|
72 | 82 | $Jobs_Issue = Get-Job -Name IndexStats* |
|
73 | 83 | Where-Object {$_.State -notin ('Completed','NotStarted','Running','Suspending','Stopping')};
|
|
0 commit comments