-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_maint.sql
More file actions
64 lines (44 loc) · 1.42 KB
/
Copy pathindex_maint.sql
File metadata and controls
64 lines (44 loc) · 1.42 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
use <>
go
select left( b.name, 32 ) as 'schema', a.object_id, a.name as 'table', stats_date( a.object_id, c.stats_id ) as 'statsdate'
from sys.tables a
join sys.schemas b
on a.schema_id = b.schema_id
join sys.stats c
on a.object_id = c.object_id
where datediff( dd, ( stats_date( a.object_id, c.stats_id ) ), getdate() ) <= 90
order by a.name
go
select top 10 *
from sys.stats
select top 10 *
from sys.indexes
-- select a.object_id, left( b.name, 32 ) as 'schema', a.name as 'table', c.name as 'index', stats_date( a.object_id, c.index_id ) as 'statsdate'
set nocount on
declare @execstr varchar( 8000 )
declare execlist cursor read_only forward_only for
select 'alter index [' + c.name + '] on [' + b.name + '].[' + a.name + '] rebuild partition = all with ( sort_in_tempdb = on );'
from sys.tables a
join sys.schemas b
on a.schema_id = b.schema_id
join sys.indexes c
on (
a.object_id = c.object_id
)
where
-- datediff( dd, ( stats_date( a.object_id, c.index_id ) ), getdate() ) > 90 and
c.index_id > 0
order by a.name
open execlist
fetch next from execlist into @execstr
while ( @@fetch_status ) != -1
begin
print 'starting rebuild at ' + cast( getdate() as varchar( 64 ) )
print @execstr
--exec( @execstr )
print 'rebuild finished at ' + cast( getdate() as varchar( 64 ) )
fetch next from execlist into @execstr
end
deallocate execlist
set nocount off
go