-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFragmented indexes.sql
46 lines (41 loc) · 1.54 KB
/
Fragmented indexes.sql
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
ALTER PROCEDURE [SQLCop].[test Fragmented Indexes]
AS
BEGIN
-- Written by George Mastros
-- February 25, 2012
-- http://sqlcop.lessthandot.com
-- http://wiki.lessthandot.com/index.php/Finding_Fragmentation_Of_An_Index_And_Fixing_It
SET NOCOUNT ON
DECLARE @Output VarChar(max)
SET @Output = ''
Create Table #Result (ProblemItem VarChar(1000))
If Exists(Select cmptlevel from master.dbo.sysdatabases Where dbid = db_ID() And cmptlevel > 80)
If Exists(Select 1 From fn_my_permissions(NULL, 'DATABASE') WHERE permission_name = 'VIEW DATABASE STATE')
Begin
Insert Into #Result(ProblemItem)
Exec('
SELECT OBJECT_NAME(OBJECT_ID) + ''.'' + s.name As ProblemItem
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, N''LIMITED'') d
join sysindexes s
ON d.OBJECT_ID = s.id
and d.index_id = s.indid
Where avg_fragmentation_in_percent >= 30
And OBJECT_NAME(OBJECT_ID) + ''.'' + s.name > ''''
And page_count > 1000
Order By Object_Name(OBJECT_ID), s.name')
End
Else
Set @Output = 'You do not have VIEW DATABASE STATE permissions within this database'
Else
Set @Output = 'Unable to check index fragmentation when compatibility is set to 80 or below'
If @Output > ''
Begin
Set @Output = Char(13) + Char(10)
+ 'For more information: '
+ 'http://wiki.lessthandot.com/index.php/Finding_Fragmentation_Of_An_Index_And_Fixing_It'
+ Char(13) + Char(10)
+ Char(13) + Char(10)
+ @Output
EXEC tSQLt.Fail @Output
End
END;