Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Re-indexing script:

================

create table #allindexes (objectid int, indexid int, fragmentation float,Page_Count


int)

insert into #allindexes

select

object_id, index_id, avg_fragmentation_in_percent, page_count

from sys.dm_db_index_physical_stats (DB_ID(), null, null , null, 'sampled')

where index_id > 0

select 'Total Fragmented indexes found: ',COUNT(*) from #allindexes

where fragmentation > 30

select

'alter index ' + quotename(i.name) +' on ' + quotename(SCHEMA_NAME(schema_id)) +


'.' + OBJECT_NAME(i.object_id) + ' rebuild with (online=off)', '-- ' +
quotename(i.type_desc), 'Pages:',f.Page_Count as
PageCount,'Fragmentation>30:',f.fragmentation

from sys.indexes i

right join #allindexes f

on i.object_id = f.objectid and i.index_id = f.indexid

join sys.objects o

on f.objectid = o.object_id

where f.fragmentation > 30

ORDER BY i.object_id,i.index_id
drop table #allindexes

For update stats: Once we execute below script will get all tables update stats
script and then need to execute one by one.

Update stats script:

===================

select 'UPDATE STATISTICS '+SCHEMA_NAME(schema_id)+'.'+'['+name+']'+ ' WITH


FULLSCAN' from sys.tables where type ='U'

SOP

1. Check Sync status --Dashoboard/Run below query on secondary

select a.primary_database as DatabaseName, a.last_copied_file as BackupFileName,


a.last_copied_date as BackupDateTime, b.last_restored_date as RestoreDateTime from
dbo.log_shipping_secondary as a, dbo.log_shipping_secondary_databases as b;

2. Database on secondary server should be in standby mode, If not change restoring


mode to standby

** To check current restoring mode run below query-- if value of restore mode is 1
then it is in standby mode, If 0 it is in restoring mode.

SELECT
secondary_database,
restore_mode,
disconnect_users,
last_restored_file
FROM msdb.dbo.log_shipping_secondary_databases

** To change Logshipping db to standby mode, run below query. (make


@restore_mode=1)

EXEC sp_change_log_shipping_secondary_database
@secondary_database = 'Collection',
@restore_mode = 1,
@disconnect_users = 1

3. Again Check Sync Status

4. Disable LS_Backup on primary server.


5. Check active session on db. If any kill those session-

**To kill active sessions-run below query

select 'kill',spid,cmd from sys.sysprocesses where spid>50 and DBID=6

6. Run the query to make live DB in standby mode. (Need to change DB name & Backup
Path) -- Path- SQLDBA\AWS\Scripts\Switch over script

7. Generate tail backup on primary ( Check trn file - It should be grater)

8.Check run copy and restore job on secondary db. Disable copy and restore job

9. Once restoration done. check business queries, If matching, make db online on


seconday with recovery.

------------------------------

7. Generate tail backup on primary ( Check trn file - It should be greater) >> if
it is not greater, then the query again

8.

You might also like