Tuesday, November 4, 2014

Estimated Completion Time of SQL Queries (BACKUP/RESTORE)

When you restore or backup a huge database, you might be anxious to know the estimated completion time of that command.

There is a "STATS" parameter that you can use with the command, but it is not dynamic.

The following query can help you in such scenario.

select dr.session_id,      
       dr.command,
       percent_complete,
       dr.start_time,
       dateadd(mi,estimated_completion_time/60000,getdate()) EstimatedCompletionTime,
       ds.text
from  sys.dm_exec_requests dr cross apply sys.dm_exec_sql_text(dr.sql_handle) as ds

where estimated_completion_time > 0

Note that, only following SQL Commands have the percentage value populated. 

·         ALTER INDEX REORGANIZE
·         SHRINK
·         BACKUP DATABASE
·         DBCC COMMANDS
·         RECOVERY
·         RESTORE DATABASE,
·         ROLLBACK
·         TDE ENCRYPTION


Monday, October 27, 2014

Report of All Databases, File Size and Space available.


If you want to get a list of all the databases and their file sizes and available free space, you can use the following script. 


You can also put a wrapper and make it a stored procedure. 




Thursday, September 18, 2014

List of all backups from the last full backup

What if one of your database is corrupted and you need to restore all the backups one by one from your last full.

It will be a time consuming and confusing task to get all the backups in order to be restored so as to not break the log chain.

The following script will help you in getting the list of all backups since the last full backup for all the databases in your instance.

It assumes there was atleast one full backup in the last 30 days...if not, you have a bigger problem.


/*
  Author: Siva Ramasamy
 
  Date  : 09/18/2014

  Description: This procedure will return the list of backup files in order from the last fullbackup for the DBs for all the databases.

  Assumption:   This procedure assumes that there was atleast one full backup in the last 30 days. If not :-) you have a bigger problem.

*/
begin
declare @db_name varchar(100)
declare @backup_start_date datetime
declare @most_recent_diffbackup_date datetime
declare @sqlcmd varchar(1000)

--drop table #temp

select bps.database_name     DatabaseName,
    'Full'                BackupType,
    CAST(CAST(bps.backup_size / 1000000 AS INT) AS VARCHAR(14)) + ' ' + 'MB' AS Size,
    bps.backup_start_date BackupStartDate,
    bps.backup_finish_date BackupFinishDate,
    CAST (DATEDIFF(MM,BPS.backup_start_date,bps.backup_finish_date) AS VARCHAR(10)) + ' Mins' DurationOfBackup,
       bmf.physical_device_name BackupFileName,
       ROW_NUMBER() OVER(PARTITION BY bps.database_name ORDER BY bps.Backup_Start_Date Desc) BackupOrder
into   #temp
from msdb..backupset bps inner join
  msdb..backupmediafamily bmf on bps.media_set_id = bmf.media_set_id
where bps.type = 'D' --  use the Full Backups as the starting point
and   bps.Is_Copy_Only = '0'   -- no copy only backups since we need to load logs as well.
and bps.backup_start_date > DATEADD (DD,-30,getdate()) -- assuming there was atleast 1 good full backup in the last 30 days

delete from #temp where BackupType = 'Full' and  BackupOrder > 1
--select * from #temp

-- Add and Update the Flag for Verification
Alter table #temp add Processed char(1)
Update #temp set Processed = 'N'


While (select count(1) from #temp where backuptype = 'Full' and  Processed = 'N') > 0
begin


  select top (1) @db_name = databasename, @backup_start_date = BackupStartDate from #temp where processed = 'N'

  --print @db_name
 --print @backup_start_date
 insert into #temp (DatabaseName, BackupType, Size, BackupStartDate, BackupFinishDate, DurationOfBackup, BackupFileName,BackupOrder,Processed)
 select  bps.database_name     DatabaseName,
       'Diff'                BackupType,
      CAST(CAST(bps.backup_size / 1000000 AS INT) AS VARCHAR(14)) + ' ' + 'MB' AS Size,
      bps.backup_start_date BackupStartDate,
      bps.backup_finish_date BackupFinishDate,
      CAST (DATEDIFF(MM,BPS.backup_start_date,bps.backup_finish_date) AS VARCHAR(10)) + ' Mins' DurationOfBackup,
      bmf.physical_device_name BackupFileName,
   ROW_NUMBER() OVER(PARTITION BY bps.database_name ORDER BY bps.Backup_Start_Date Desc) BackupOrder,
   'P'
    from    msdb..backupset bps inner join
         msdb..backupmediafamily bmf on bps.media_set_id = bmf.media_set_id
    where   bps.type = 'I' --  Pull all the differential backups after the full backup
    and     bps.Is_Copy_Only = '0'   -- no copy only backups since we need to load logs as well.
    and     bps.backup_start_date > @backup_start_date
 and     bps.database_name  = @db_name

  delete from #temp where BackupType = 'Diff' and  BackupOrder > 1 and DatabaseName = @db_name


    --get all the log backups after most recent full or differential backup
    insert into #temp (DatabaseName, BackupType, Size, BackupStartDate, BackupFinishDate, DurationOfBackup, BackupFileName, BackupOrder,Processed)
 select  bps.database_name     DatabaseName,
       'Log'                BackupType,
   CAST(CAST(bps.backup_size / 1000000 AS INT) AS VARCHAR(14)) + ' ' + 'MB' AS Size,
   bps.backup_start_date BackupStartDate,
   bps.backup_finish_date BackupFinishDate,
      CAST (DATEDIFF(MM,BPS.backup_start_date,bps.backup_finish_date) AS VARCHAR(10)) + ' Mins' DurationOfBackup,
      bmf.physical_device_name BackupFileName,
   ROW_NUMBER() OVER(PARTITION BY bps.database_name ORDER BY bps.Backup_Start_Date) BackupOrder,
   'P'
 from    msdb..backupset bps inner join
   msdb..backupmediafamily bmf on bps.media_set_id = bmf.media_set_id
 where   bps.type = 'L' --  Pull all the Log Backups after the most recent Differential Backup
 and     bps.Is_Copy_Only = '0'   -- no copy only backups since we need to load logs as well.
 and     bps.backup_start_date > (select max(BackupStartDate) from #temp where DatabaseName = @db_name)
 and     bps.database_name  = @db_name
 order by backup_start_date

  Update #temp set Processed = 'P' where databasename = @db_name

end

select * from #temp order by databasename, backupstartdate

end



Monday, September 1, 2014

Trancactional Replication - Setup Issues and Solution.

Recently I encountered an issue with setting up Transactional Replication. There are many blog posts about setting up Transactional replication and providing step by step explanations. I followed the exact steps mentioned in them but have been unsuccessful in setting up transactional replication successfully.

After spending some time on it, I figured out that it is due to windows login being used to connect to the publisher and subscriber. The Windows login has sysadmin privilege on the instance and I was able to connect to the instance using that login successfully. But, for some reason, It did not like it to be tied up with replication. So, I changed it to SQL account and it started working..!!

I tried to re-produce this issue by completely deleting the replication and tried again windows credentials..this time it was successful.

But, if you have been having issues with transactional replication/windows login, try changing it to SQL Login.



Friday, April 11, 2014

SQL Server Deadlocks



You are an accidental/junior DBA. One of your developer says he's getting a lot of deadlock messages in the application log suddenly.  He is asking for the DBA's (Your) help.  What are you going to do.  This blog post will help you to handle this issue.

What is deadlock
Deadlock is a situation when two transactions have mutually locked out each other. 

For example, Lets take two transactions
 "TRAN-1",  it has two insert statements,  First insert on Table-A and Second Insert on Table-B
 "TRAN-2" ,  This also has two insert statements, First insert in on Table-B,  Second Insert is on Table-A.

When both of these Transactions execute at the same time,
Tran-1 will finish the insert on Table-A and wait for the lock to be released (acquired by Tran-2) on Table-B. The lock on Table-A will still be active because it is inside a transaction.
Tran-2 would have completed the insert on Table-B but would not have released the lock since the transaction is not commited yet, but will be waiting for the lock to be released for Table-A.

At this point,  The deadlock situation has encountered. 

SQL Server is smart enough to diagnose the deadlock situation and identify one of the processes as victim and kill it so that the other process can continue to execute.

How to analyze deadlock
Deadlock related information will be logged to the errorlog only if the related trace flags are turned on. Otherwise, you won't find any useful info from the errorlog.

The traceflags are 1204 and 1222(After SQL Server 2005).

How to identify if the trace flags are already turned on

DBCC TRACESTATUS command will return the trace flags that are currently turned on.

How to turn on the trace flag.

The following commands will turn the trace flags on.

DBCC TRACEON (1222, -1)
DBCC TRACEON (1204, -1)

What to look for in the error logs

Here is a sample error message from the log

2014-04-11 15:24:22.22 spid4s      Requested by:
2014-04-11 15:24:22.22 spid4s        ResType:LockOwner Stype:'OR'Xdes:0x00000004E9FD8BC0 Mode: U SPID:63 BatchID:0 ECID:0 TaskProxy:(0x00000004E9BBC608) Value:0xeb284540 Cost:(0/144)
2014-04-11 15:24:22.22 spid4s     
2014-04-11 15:24:22.22 spid4s      Victim Resource Owner:
2014-04-11 15:24:22.22 spid4s       ResType:LockOwner Stype:'OR'Xdes:0x00000004F08D6D28 Mode: U SPID:58 BatchID:0 ECID:0 TaskProxy:(0x00000004E97B8608) Value:0xf5b126c0 Cost:(0/144)
2014-04-11 15:24:22.22 spid18s     deadlock-list
2014-04-11 15:24:22.22 spid18s      deadlock victim=process4f9025498
2014-04-11 15:24:22.22 spid18s       process-list
2014-04-11 15:24:22.22 spid18s        process id=process4f9025498 taskpriority=0 logused=144 waitresource=RID: 8:1:301:0 waittime=5851 ownerId=32694 transactionname=user_transaction lasttranstarted=2014-04-11T15:24:16.370 XDES=0x4f08d6d28 lockMode=U schedulerid=3 kpid=3476 status=suspended spid=58 sbid=0 ecid=0 priority=0 trancount=2 lastbatchstarted=2014-04-11T15:24:16.370 lastbatchcompleted=2014-04-11T15:24:07.050 lastattention=1900-01-01T00:00:00.050 clientapp=Microsoft SQL Server Management Studio - Query hostname=SIVA-PC hostpid=4024 loginname=Siva-PC\Siva isolationlevel=read committed (2) xactid=32694 currentdb=8 lockTimeout=4294967295 clientoption1=671090784 clientoption2=390200
2014-04-11 15:24:22.22 spid18s         executionStack
2014-04-11 15:24:22.22 spid18s          frame procname=adhoc line=4 stmtstart=16 sqlhandle=0x020000006306cb0282580a95a5146f4b2ce8d05ad05f852e0000000000000000000000000000000000000000
2014-04-11 15:24:22.22 spid18s     UPDATE [dbo].[DeadLockTest] set [col1] = @1    
2014-04-11 15:24:22.22 spid18s          frame procname=adhoc line=4 stmtstart=106 sqlhandle=0x020000000f8d9d36c56b96bdcb04670ca5b75bc17de9868c0000000000000000000000000000000000000000
2014-04-11 15:24:22.22 spid18s     UPDATE dbo.DeadLockTest SET col1 = 1    
2014-04-11 15:24:22.22 spid18s         inputbuf
2014-04-11 15:24:22.22 spid18s     BEGIN TRAN
2014-04-11 15:24:22.22 spid18s     UPDATE dbo.DeadLockTest2 SET col1 = 1
2014-04-11 15:24:22.22 spid18s     UPDATE dbo.DeadLockTest SET col1 = 1   
2014-04-11 15:24:22.22 spid18s        process id=process4f04fb0c8 taskpriority=0 logused=144 waitresource=RID: 8:1:303:0 waittime=650 ownerId=32286 transactionname=user_transaction lasttranstarted=2014-04-11T15:22:53.877 XDES=0x4e9fd8bc0 lockMode=U schedulerid=2 kpid=4148 status=suspended spid=63 sbid=0 ecid=0 priority=0 trancount=5 lastbatchstarted=2014-04-11T15:24:21.570 lastbatchcompleted=2014-04-11T15:24:12.090 lastattention=2014-04-11T15:23:49.210 clientapp=Microsoft SQL Server Management Studio - Query hostname=SIVA-PC hostpid=4024 loginname=Siva-PC\Siva isolationlevel=read committed (2) xactid=32286 currentdb=8 lockTimeout=4294967295 clientoption1=671090784 clientoption2=390200
2014-04-11 15:24:22.22 spid18s         executionStack
2014-04-11 15:24:22.22 spid18s          frame procname=adhoc line=1 stmtstart=16 sqlhandle=0x02000000ca356b202147fbe58aa2b109b537cf37cb3083430000000000000000000000000000000000000000
2014-04-11 15:24:22.22 spid18s     UPDATE [dbo].[DeadLockTest2] set [col1] = @1    
2014-04-11 15:24:22.22 spid18s          frame procname=adhoc line=1 sqlhandle=0x0200000087d432229b1acc5bc82908fa19f6f23bbc0e4e820000000000000000000000000000000000000000
2014-04-11 15:24:22.22 spid18s     UPDATE dbo.DeadLockTest2 SET col1 = 1    
2014-04-11 15:24:22.22 spid18s         inputbuf
2014-04-11 15:24:22.22 spid18s     UPDATE dbo.DeadLockTest2 SET col1 = 1   
2014-04-11 15:24:22.22 spid18s       resource-list
2014-04-11 15:24:22.22 spid18s        ridlock fileid=1 pageid=301 dbid=8 objectname=ReplB.dbo.DeadLockTest id=lock4edbe2480 mode=X associatedObjectId=72057594039828480
2014-04-11 15:24:22.22 spid18s         owner-list
2014-04-11 15:24:22.22 spid18s          owner id=process4f04fb0c8 mode=X
2014-04-11 15:24:22.22 spid18s         waiter-list
2014-04-11 15:24:22.22 spid18s          waiter id=process4f9025498 mode=U requestType=wait
2014-04-11 15:24:22.22 spid18s        ridlock fileid=1 pageid=303 dbid=8 objectname=ReplB.dbo.DeadLockTest2 id=lock4f5abe780 mode=X associatedObjectId=72057594039894016
2014-04-11 15:24:22.22 spid18s         owner-list
2014-04-11 15:24:22.22 spid18s          owner id=process4f9025498 mode=X
2014-04-11 15:24:22.22 spid18s         waiter-list
2014-04-11 15:24:22.22 spid18s          waiter id=process4f04fb0c8 mode=U requestType=wait


The above given information will be helpful in identifying the transactions involved, the logins involved, time of the deadlock etc.

You can start analysing the issue after gathering these informations.

How to turn off trace flags

Once you have obtained the necessary deadlock information from the log file, you can turn off the trace flags.

Command to turn off the traceflags

DBCC TRACEOFF (1204, -1)
DBCC TRACEOFF (1222, -1)

There is no need to restart the instance for turning these trace flags on/off.

Hope this helps.



Thursday, April 3, 2014

Changing Schema/Role Ownership

Though you can change ownership using SSMS, it is always good to know the command to perform the operation.


ALTER AUTHORIZATION ON SCHEMA/ROLE::[name] TO [new owner]

This will also be helpful when you need to change ownership on many objects at a time.