Saturday, 14 December 2019

Find Status or percentage completed of any DBCC Shrink Databse files

1.First try to find the space used and empty space left in databases


SET NOCOUNT ON

DBCC UPDATEUSAGE(0)

-- DB size.
EXEC sp_spaceused

 --- Table row counts and sizes.
CREATE TABLE #t
(
    [name] NVARCHAR(128),
    [rows] CHAR(11),
    reserved VARCHAR(18),
    data VARCHAR(18),
    index_size VARCHAR(18),
    unused VARCHAR(18)
)

INSERT #t EXEC sp_msForEachTable 'EXEC sp_spaceused ''?'''

SELECT *
FROM   #t

-- # of rows.
SELECT SUM(CAST([rows] AS int)) AS [rows]
FROM   #t

DROP TABLE #t

2. Find the Status or percentage completed of any DBCC Shrink Databse files with below query

SELECT
    percent_complete,
    start_time,
    status,
    command,
    estimated_completion_time,
    cpu_time,
    total_elapsed_time
FROM
    sys.dm_exec_requests
WHERE
    command = 'DbccFilesCompact'

Backup databases on SQL server Express edition

please see below on how to create backups on SQL Express edition,

SQL Server express edition do not have SQL agent .

1.create a linked server from any non express edition ,make sure you have sys admin previlage on the sql login you use
2.create SP(as below) on Express edition master database.
3.Schedule a JOB to call SP from non express edition.

use master
go
Create  PROC SP_Backup
AS
BEGIN
DECLARE @path1  VARCHAR(100)
Declare @DeleteDate datetime
SET @Path1='R:\'
SET @DeleteDate= DATEADD(day, -2, GETDATE())---older than 2 days
EXEC master.sys.xp_delete_file 0,@path1,'BAK',@DeleteDate,0;


DECLARE @destination VARCHAR(300),
@now VARCHAR(50),
@now_date DATETIME,
@exec_str VARCHAR(200),
@db_name VARCHAR(30),
@path  VARCHAR(100)

SET @Path='R:\'--Location of the files going to be
SELECT @now_date = GETDATE()
SELECT @Now = RIGHT(CONVERT(VARCHAR(4),DATEPART(yy,@now_date)),4) + RIGHT( '00' + CONVERT(VARCHAR(2),DATEPART(mm,@now_date)),2) +
RIGHT( '00' + CONVERT(varchar(2),DATEPART(dd,@now_date)),2) +'_'+ RIGHT( '00' + CONVERT(varchar(2),DATEPART(hh,@now_date)),2) +
RIGHT( '00' + CONVERT(varchar(2),DATEPART(mi,@now_date)),2) + RIGHT( '00' + CONVERT(varchar(2),DATEPART(ss,@now_date)),2)

SET NOCOUNT ON
DECLARE db_cursor CURSOR FOR
SELECT name FROM master.sys.databases
where name not in ('tempdb')
ORDER BY name

OPEN db_cursor
FETCH db_cursor INTO @db_name

WHILE (@@fetch_status = 0)
BEGIN

SELECT  @destination = 'R:\' + @db_name + '_Full_' + @Now +'.bak'
SELECT  @exec_str = 'BACKUP DATABASE ' + @db_name + ' TO DISK = ''' + @destination + ''' WITH COMPRESSION'
--PRINT @exec_str
EXEC (@exec_str)

FETCH db_cursor INTO @db_name
END

CLOSE db_cursor
DEALLOCATE db_cursor
 END

Notify if a database backup missing

declare @ServerName varchar(250)

declare @DatabaseList varchar(4000)
declare @CountMissingBackups int
declare @MailSubject varchar(250)
declare @MailText varchar (4000)
 
;WITH CTE as
(
SELECT
   CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS [Server],
   msdb.dbo.backupset.database_name
FROM    msdb.dbo.backupset
WHERE     msdb.dbo.backupset.type = 'D'
GROUP BY msdb.dbo.backupset.database_name
HAVING      (MAX(msdb.dbo.backupset.backup_finish_date) < DATEADD(hh, - 24, GETDATE()))
 
UNION
 
--Databases without any backup history
SELECT    
   CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS [Server],
   master.dbo.sysdatabases.NAME AS database_name
FROM
   master.dbo.sysdatabases LEFT JOIN msdb.dbo.backupset
       ON master.dbo.sysdatabases.name  = msdb.dbo.backupset.database_name
WHERE msdb.dbo.backupset.database_name IS NULL AND master.dbo.sysdatabases.name <> 'tempdb'
--ORDER BY msdb.dbo.backupset.database_name
)
select @CountMissingBackups = count(*), @ServerName = [Server] ,@Databaselist = COALESCE(@DatabaseList + ', ', '') + CAST(database_name AS varchar(250))
from CTE
group by [Server], database_name
 
 
-- send the mail
 
if @CountMissingBackups > 0
begin
 
set @MailSubject = 'Backups on '+@ServerName
set @MailText = 'The following databases have not been backed up in the past 24 hours: '+@Databaselist
 
EXEC msdb.dbo.sp_send_dbmail 
@profile_name='Default',
@recipients='abc@XYZcom',
@subject=@MailSubject,
@body=@MailText
 
end
   

send the result of stored proceedure via email

declare @subject varchar(1000)

set @subject = 'Space Alerts on FPVI-Xweb1'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Default',
@recipients = 'abc@xyz.com',
@query = 'EXEC [FPVI-XWEB1].Master.dbo.XP_FixedDrives',
@subject = @subject
GO

Send email when Blocking Happens and Track Queries

 --This script will send an email to operator if blocking process counter rise above 0

USE [msdb]
GO
EXEC msdb.dbo.sp_update_alert @name=N'Blocking',
@message_id=0,
@severity=0,
@enabled=1,
@delay_between_responses=120,
@include_event_description_in=0,
@database_name=N'',
@notification_message=N'',
@event_description_keyword=N'',
@performance_condition=N'SQLServer:General Statistics|Processes blocked||>|0',
@wmi_namespace=N'',
@wmi_query=N'',
@job_id=N'401a9e4d-ae94-47ca-9ac4-deb4c244690a'
GO

-----------------------------JOB NAME "Blocking Job"
 --------------------when blocking happens to notify what script blocking , we can have the below script run to keep track of blocking transaction
--CREATE TABLE AuditBlocking
--    (
--      session_id INT ,
--      Databasename VARCHAR(100) ,
--      DatetimeEvent DATETIME NOT NULL
--                             DEFAULT ( GETDATE() ) ,
--      SQL_text VARCHAR(MAX) ,
--      blocking_session_id INT ,
--      wait_time INT ,
--      wait_type VARCHAR(100) ,
--      last_wait_type VARCHAR(100) ,
--      wait_resource VARCHAR(100) ,
--      transaction_isolation_level INT ,
--      lock_timeout INT
--    )
--GO


      WAITFOR DELAY '00:01:00';
      IF EXISTS ( SELECT    session_id ,
                            DB_NAME(database_id) AS 'Database_Name' ,
                            GETDATE() ,
                            sql_text.text AS 'Text' ,
                            blocking_session_id ,
                            wait_time --Milliseconds(1000 miliosec=1 sec) ,
                            wait_type ,
                            last_wait_type ,
                            wait_resource ,
                            transaction_isolation_level ,
                            ( er.wait_time / 1000 ) ,--added this
                            lock_timeout
                  FROM      sys.dm_exec_requests er
                            CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sql_text
                  WHERE     ( er.wait_time / 1000 ) > 60 --added this
)
        BEGIN

            INSERT  INTO AuditBlocking---i have created this table before
                    SELECT  session_id ,
                            DB_NAME(database_id) AS 'Database_Name' ,
                            GETDATE() ,
                            sql_text.text AS 'Text' ,
                            blocking_session_id ,
                            wait_time --Milliseconds(1000 miliosec=1 sec) ,
                            wait_type ,
                            last_wait_type ,
                            wait_resource ,
                            transaction_isolation_level ,
                            ( er.wait_time / 1000 ) ,--added this
                            lock_timeout
                    FROM    sys.dm_exec_requests er
                            CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sql_text
                    WHERE   ( er.wait_time / 1000 ) > 60;  --added this


            DECLARE @AlertMessage VARCHAR(MAX);
            SET @AlertMessage = 'There is Blocking happening on' + ' '
                + @@SERVERNAME + ' '
                + 'Server. Please see AuditDB.dbo.AuditBlocking Table for more Information';
            EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Default',
                @recipients = 'abc@xyz.com', @body = @AlertMessage,
                @importance = 'Low', @subject = 'Blocking Alert';

        END; 




-----------------------------TESTING
USE AuditDB
GO
CREATE TABLE ##Employees
    (
      EmpId INT IDENTITY ,
      EmpName VARCHAR(16) ,
      Phone VARCHAR(16)
    )
GO
INSERT  INTO ##Employees
        (EmpName,Phone )
VALUES  ('Martha','800-555-1212'),
        ('Jimmy','619-555-8080')
GO

CREATE TABLE ##Suppliers
    (
      SupplierId INT IDENTITY ,
      SupplierName VARCHAR(64) ,
      Fax VARCHAR(16)
    )
GO
INSERT  INTO ##Suppliers
        ( SupplierName, Fax )
VALUES  ('Acme', '877-555-6060' ),
        ('Rockwell', '800-257-1234' )
GO

USE AuditDB
GO
BEGIN TRANSACTION
SELECT  *
FROM    ##Employees WITH ( TABLOCKX, HOLDLOCK );
WAITFOR DELAY '00:02:00'
 ---Wait a minute!
ROLLBACK TRANSACTION
--Release the lock



 ----another query window
 SELECT *
 FROM   ##Employees


 --------------------------check results

 SELECT * FROM dbo.AuditBlocking
 ORDER BY DatetimeEvent asc

SELECT * FROM AuditBlocking
--WHERE   blocking_session_id>=50


/*testing
 SELECT * FROM AuditBlocking
 SP_who2 'Active'

--SELECT * FROM sys.messages
--WHERE message_id IN (823,824,825) AND language_id=1033

--SELECT * FROM sys.messages
--WHERE severity IN (016,017,018,019,020,021,022,023,024,025) AND language_id=1033
*/

Send email when DeadlockHappens and track Queries

CAUTION, as this is going to use the error log , please make sure you test in test environment before implementing in Production.


/*Capture deadlocks

CREATE Alert Deadlock ERROR 1205>SQL SERVER Performance Condition Alert>Locks>Number OF deadlocks/sec>_total>raises above 0
Response> CREATE a JOB WITH below script amd notify dba >options email


---To ensure the deadlock related errors are logged
EXEC master.sys.sp_altermessage 1205, 'WITH_LOG', TRUE;
GO
EXEC master.sys.sp_altermessage 3928, 'WITH_LOG', TRUE;
GO

--DBCC TRACEON (3605,1204,1222,-1)
Description:
3605 the results of the DBCC output to the error log.
1204 returns the resources and types of locks participating in the deadlock and the current command affected.
1222 Back to the resources and types of locks participating in the deadlock, and the use does not comply with any XSD schema in XML format affected the current command (further than 1204, SQL 2005 and available).
-1 Open the specified trace flag globally.
DBCC TRACEON (1204, -1)
DBCC TRACEON (1222, -1)----this is going to add 100 lines in log file


*/
--== This is for SQL 2005 and higher. ==--
--== We will create a temporary table to hold the error log detail. ==--
--== Before we create the temporary table, we make sure it does not already exist. ==--
 IF OBJECT_ID('tempdb.dbo.ErrorLog') IS Not Null
 BEGIN
 DROP TABLE tempdb.dbo.ErrorLog
 END
 --== We have checked for the existence of the temporary table and dropped it if it was there. ==--
 --== Now, we can create the table called tempdb.dbo.ErrorLog ==--
CREATE TABLE tempdb.dbo.ErrorLog (Id int IDENTITY (1, 1) NOT NULL,
logdate DATETIME, procInfo VARCHAR(10), ERRORLOG VARCHAR(MAX))
--== We create a 3 column table to hold the contents of the SQL Server Error log. ==--
--== Then we insert the actual data from the Error log into our newly created table. ==--
 INSERT INTO tempdb.dbo.ErrorLog
 EXEC master.dbo.sp_readerrorlog 0
--== With our table created and populated, we can now use the info inside of it. ==--
 BEGIN
--== Set a variable to get our instance name. ==--
--== We do this so the email we receive makes more sense. ==--
 declare @servername nvarchar(150)
 set @servername = @@servername
--== We set another variable to create a subject line for the email. ==--
 declare @mysubject nvarchar(200)
 set @mysubject = 'Deadlock event notification on server '+@servername+'.'
 --== Now we will prepare and send the email. Change the email address to suite your environment. ==--
 EXEC msdb.dbo.sp_send_dbmail
 @profile_name='Default',
 @recipients='abc@xyz.com,
 @subject = @mysubject,
 @body = 'Deadlock has occurred. View attachment to see the deadlock info',
 @query = 'select logdate, procInfo, ERRORLOG from tempdb.dbo.ErrorLog where Id >= (select TOP 1 Id from tempdb.dbo.ErrorLog WHERE ERRORLOG Like ''%Deadlock encountered%'' order by Id DESC)',
 @query_result_width = 600,
 @attach_query_result_as_file = 1
 END
 --== Clean up our process by dropping our temporary table. ==--
 DROP TABLE tempdb.dbo.ErrorLog

 --------------testing
-----------------------------------TESTING Below
 --DROP TABLE ##Employees
 --DROP TABLE ##Suppliers

USE AuditDB
 GO
 CREATE TABLE ##Employees ( EmpId INT IDENTITY, EmpName VARCHAR(16), Phone VARCHAR(16) )
 GO
 INSERT INTO ##Employees (EmpName, Phone)
 VALUES ('Martha', '800-555-1212'), ('Jimmy', '619-555-8080')
 GO

 CREATE TABLE ##Suppliers( SupplierId INT IDENTITY, SupplierName VARCHAR(64), Fax VARCHAR(16) )
 GO
 INSERT INTO ##Suppliers (SupplierName, Fax) VALUES ('Acme', '877-555-6060'), ('Rockwell', '800-257-1234')
 GO

 --TAB1
 BEGIN TRAN;
 UPDATE ##Suppliers
 SET Fax = N'676-1019'
 WHERE supplierid = 1


 ----IN the query tab 2
 --BEGIN TRAN;
 --UPDATE ##Employees
 --SET EmpName = 'Brian'
 --WHERE empid = 1

 --Now again in the query tab 1 EXECUTE
 UPDATE ##Employees
 SET phone = N'555-9999'
 WHERE empid = 1
 COMMIT


 ----And in the query tab 2  EXECUTE
 --UPDATE ##Suppliers
 --SET Fax = N'676-1019'
 --WHERE supplierid = 1
 --COMMIT 

Tuning areas where we need to Look

Tuning areas where we need to Look


PAGEIOLATCH—DISK
CXPACKET—Big Queries
SOS_Scheduled_yield—Lots of functions may be missing Indexes
LATCH_EX—Parallelism or missing Indexes