Labels

Showing posts with label SQLDBA. Show all posts
Showing posts with label SQLDBA. Show all posts

Tuesday, 16 February 2021

Get Indexfragmentation details for all databases in a SQL Server instance

 

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
GO

DECLARE @Tbl TABLE (
	ServerName VARCHAR(128)
	,DBName VARCHAR(128)
	,SchemaName VARCHAR(128)
	,TableName VARCHAR(100)
	,IndexName VARCHAR(100)
	,FragPercent FLOAT
	,IndexType TINYINT
	,IsPrimaryKey BIT
	);

INSERT INTO @Tbl
EXEC SP_MSforeachdb @command1 = 'use [?];
                select  @@Servername, 
                        DB_NAME(),
                        sc.name as SchemaName,
                        object_name (s.object_id) as TableName, 
                        I.name, 
                        s.avg_fragmentation_in_percent, 
                        I.type, 
                        I.is_primary_key
                from sys.dm_db_index_physical_stats (DB_ID (), NULL, NULL, NULL, ''LIMITED'') as S
                    join sys.indexes as I on s.object_id = I.object_id and s.index_id = I.index_id
                    join sys.objects as O on s.object_id = O.object_id 
                    join sys.schemas as sc on O.schema_id = sc.schema_id
                where o.type = ''U'' and avg_fragmentation_in_percent > 20 and (I.name is not null) 
                ORDER BY  avg_fragmentation_in_percent DESC'

SELECT *
FROM @Tbl
GO


 

Monday, 21 September 2020

Script to send e-mail once transaction logfile usage reaches to 60%

 

USE msdb

SET NOCOUNT ON;

IF OBJECT_ID('tempdb..#MonitorTransactionLogFileUsage') IS NOT NULL
	DROP TABLE #MonitorTransactionLogFileUsage

CREATE TABLE #MonitorTransactionLogFileUsage (
	ID INT IDENTITY(1, 1)
	,DatabaseName SYSNAME
	,LogSizeInMB DECIMAL(18, 5)
	,LogSpaceUsedInPercentage DECIMAL(18, 5)
	,[Status] INT
	)

INSERT INTO #MonitorTransactionLogFileUsage (
	DatabaseName
	,LogSizeInMB
	,LogSpaceUsedInPercentage
	,[Status]
	)
EXEC ('DBCC SQLPERF(LOGSPACE)')

IF OBJECT_ID('tempdb..#LogfileGrowth') IS NOT NULL
	DROP TABLE #LogfileGrowth

CREATE TABLE #LogfileGrowth (LogGrowth VARCHAR(4000))

INSERT INTO #LogfileGrowth
SELECT 'Database ' + '' + cast(DatabaseName AS VARCHAR(50)) + 
 ' LogSpaceUsedInPercentage  has grown to ' + 
cast(LogSpaceUsedInPercentage AS VARCHAR(50)) + '' + '% ' + ' ' + '' +  
'Database recover model is ' + '' + cast(recovery_model_desc 
COLLATE Latin1_General_CI_AS_KS_WS AS VARCHAR(50)) + ' and it is growing due to ' 
+ cast(log_reuse_wait_desc AS VARCHAR(150)) LogGrowth
FROM #MonitorTransactionLogFileUsage a
INNER JOIN sys.databases b ON a.DatabaseName = b.name
	AND a.DatabaseName = 'DB Name you would like to monitor '
WHERE LogSpaceUsedInPercentage > 60

DECLARE @body1 VARCHAR(4000)

SET @body1 = (
		SELECT TOP 1 LogGrowth
		FROM #LogfileGrowth
		)

DECLARE @server_name VARCHAR(100);

SELECT @server_name = convert(VARCHAR(100), SERVERPROPERTY('servername'));

DECLARE @sub VARCHAR(1000);

SELECT @sub = 'Logfile is getting filled on  ' + @server_name + ' ';

DECLARE @rowcount INT

SELECT @rowcount = (
		SELECT count(1)
		FROM #LogfileGrowth
		HAVING count(1) > 0
		)

IF @rowcount > 0
BEGIN
	--database-notifications@accuratebackground.pagerduty.com
	EXEC msdb.dbo.sp_send_dbmail @profile_name = 'your db profile name'
		,@recipients = ' mention list of recipients you need to send mail'
		,@subject = @sub
		,@body = @body1
		,@body_format = 'HTML';
END

Script to monitor TransactionLogFileUsage and trigger backups automatically once logfile usage reaches to 40%

  

IF OBJECT_ID('tempdb..#MonitorTransactionLogFileUsage') IS NOT NULL

	DROP TABLE #MonitorTransactionLogFileUsage

CREATE TABLE #MonitorTransactionLogFileUsage (
	ID INT IDENTITY(1, 1)
	,DatabaseName SYSNAME
	,LogSizeInMB DECIMAL(18, 5)
	,LogSpaceUsedInPercentage DECIMAL(18, 5)
	,[Status] INT
	)

INSERT INTO #MonitorTransactionLogFileUsage (
	DatabaseName
	,LogSizeInMB
	,LogSpaceUsedInPercentage
	,[Status]
	)
EXEC ('DBCC SQLPERF(LOGSPACE)')

SELECT *
FROM #MonitorTransactionLogFileUsage

/* declare variables */
DECLARE @variable SYSNAME
	,@jobname SYSNAME

DECLARE cursor_name CURSOR FAST_FORWARD READ_ONLY
FOR
SELECT DatabaseName
FROM #MonitorTransactionLogFileUsage
WHERE LogSpaceUsedInPercentage >= 40

OPEN cursor_name

FETCH NEXT
FROM cursor_name
INTO @variable

WHILE @@FETCH_STATUS = 0
BEGIN
	SET @jobname = N'LSBackup_' + @variable

	IF EXISTS (
			SELECT name
			FROM msdb.dbo.sysjobs
			WHERE name = @jobname
			)
	BEGIN
		IF NOT EXISTS (
				SELECT 1
				FROM msdb.dbo.sysjobs_view job
				INNER JOIN msdb.dbo.sysjobactivity activity  
ON job.job_id = activity.job_id
WHERE activity.run_requested_date IS NOT NULL
		AND activity.stop_execution_date IS NULL
		AND job.name = @jobname
				)
		BEGIN
			EXEC msdb.dbo.sp_start_job @job_name = @jobname
		END
		ELSE
		BEGIN
			PRINT 'Job ''' + @jobname + ''' is already started ';
		END
	END

	FETCH NEXT
	FROM cursor_name
	INTO @variable
END

CLOSE cursor_name

DEALLOCATE cursor_name

DROP TABLE #MonitorTransactionLogFileUsage

Thursday, 17 September 2020

Identify queries causing logfile growth or consuming CPU,Memory

 During troubleshooting performance related issues such as what is caused for log file growth , currently running , causing CPU spikes etc the below query is very handful.

SELECT TOP 50 qs.execution_count
	,exec_count_per_sec = 
qs.execution_count / 
NULLIF(DATEDIFF(second, qs.creation_time, qs.last_execution_time), 0)
	,qs.creation_time
	,qs.last_execution_time
	,qs.total_worker_time AS Total_CPU
	,total_CPU_inSeconds = --Converted from microseconds
	qs.total_worker_time / 1000000
	,total_elapsed_time_inSeconds = --Converted from microseconds
	qs.total_elapsed_time / 1000000
	,qs.total_logical_reads
	,qs.total_logical_writes
	,qs.total_elapsed_time
	,st.TEXT
	,qp.query_plan
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) AS qp
--WHERE st.text like 'test%'
ORDER BY qs.total_logical_writes DESC

 



Friday, 13 September 2019

How many max connections does SQL Server allows.




To find this information we can use @@MAX_CONNECTIONS global variable.

@@MAX_CONNECTIONS in SQL Server returns maximum number of simultaneous user connections allowed. Maximum user connections allowed by SQL Server by default is 32,767; this number also depends on application and server hardware limits. This cam also be configured at server-level to avoid too many connections.
@@CONNECTIONS returns number of connection attempts (successful/failed) made to SQL Server since SQL Server is started. Since this include all attempts it can be greater than @@MAX_CONNECTIONS.

SELECT [ConnectionAttempts] = @@CONNECTIONS
        ,[MaximumAllowed] = @@MAX_CONNECTIONS

Friday, 23 September 2016

Common Significant Wait types with BOL explanations

                                                          WaitTypes 

Network Related Waits

       ASYNC_NETWORK_IO :Occurs on network writes when the task is blocked behind the network

 Locking Waits

1. LCK_M_IX: Occurs when a task is waiting to acquire an Intent Exclusive (IX) lock.

2. LCK_M_IU: Occurs when a task is waiting to acquire an Intent Update (IU) lock.

3. LCK_M_S: Occurs when a task is waiting to acquire a Shared lock.

I/O Related Waits

1.ASYNC_IO_COMPLETION: Occurs when a task is waiting for I/Os to finish.

2. IO_COMPLETION: Occurs while waiting for I/O operations to complete.              This wait type generally represents non-data page I/Os. Data page I/O completion waits appear  as

 PAGEIOLATCH waits

3. PAGEIOLATCH_SH:Occurs when a task is waiting on a latch for a buffer that is in an I/O request. The latch request is in Shared mode. Long waits may indicate problems with the disk subsystem.

4. PAGEIOLATCH_EX:Occurs when a task is waiting on a latch for a buffer that is in an I/O request.  The latch request is in Exclusive mode. Long waits may indicate problems with the disk subsystem.

5. WRITELOG : Occurs while waiting for a log flush to complete. Common operations that cause log flushes are checkpoints and transaction commits.

6. PAGELATCH_EX: Occurs when a task is waiting on a latch for a buffer that is not in an I/O request. The latch request is in Exclusive mode.

7. BACKUPIO:Occurs when a backup task is waiting for data, or is waiting for a buffer in which to store data

CPU Related Waits

1. SOS_SCHEDULER_YIELD:  Occurs when a task voluntarily yields the scheduler for other tasks to execute. During this wait the task is waiting for its quantum to be renewed.
2. THREAD POOL : Occurs when a task is waiting for a worker to run on.
  This can indicate that the maximum worker setting is too low, or that batch executions are taking  unusually long, thus reducing the number of workers available to satisfy other batches.
3. CX_PACKET: Occurs when trying to synchronize the query processor exchange iterator .You may consider lowering the degree of parallelism if contention on this wait type becomes a problem.

SQL Server : Slow running query resons

                                                    Query Performing Slowly

1) Identify the SPID of the query from Sys.sysprocesses

2) Find any blockings exists or not and if it exists find the LEAD Blocker.

3) Identify how many connections are there, Benchmark if exceeds is an issue

4) CPU Utilization, Memory Utilization, System Memory Information and how much SQL Server is consuming. If AWE is enabled or not.

5) Any jobs are running both at OS level and SQL Server level.

6) Affinity Mask for Processor and IO should be checked.

7) Query observation, Coding standards have been followed or not. Commit issued at regular intervals in the code.

8) Table statistics are outdated. Need to update stats.

9) Indexes are created on correct columns are not. Verify the query and check the columns listed under WHERE condition and ensure that there are indexes created.

10) Even if indexes are created, they are fragmented or not.

11) Run server side trace to track what other activities are currently being rolled out to find the cause of the issue. If approved Profiler also can be used.

12) Network Latency between Client and the Server can cause performance issue.

13) MAXDOP feature can be utilized to improve the parallel execution of the query.

14) If clustered instance and Active-Active (N+1/N+M) Multiple Instance. If all nodes fail the last surviving node has all instances running on it causing performance issue.

15 )  Check disk space availability for that database and also for system databases.

16 ) Verify if any Application or Database specific jobs are running or not.

17 ) Network bandwidth to be verified and also Storage SAN bandwidth when accessing data. Contact Network/Storage team.

18) Identify top 10 long running queries and see if they are causing the performance lag.

19) Ask requester for the Query. Verify the Estimated Execution Plan of the Query.

20) Check if any Table Scans are present in the Plan. Table Scans are very expensive from resource perspective.

Also verify the Load on the system, Example average load is 2500 but we could see 10,000 connections.

SQL Server DBA : Troubleshooting 100% Memory / Memory Leak(s)

                        Troubleshooting 100% Memory / Memory Leak(s):

ü  Verify Task Manager for basic understanding of Memory utilization by which Process.

ü  If memory is consumed by other processes contact respective team to get it fixed.

ü  If memory is consumed high by SQL Server follow below steps

 Verify SQL Server Error logs for any memory related errors

MTL Based Errors:

 i) SQL Server 2000

WARNING: Failed to reserve contiguous memory of Size

 ii) SQL Server 2005

Failed Virtual Allocate Bytes: FAIL_VIRTUAL_RESERVE

 iii)SQL Server 2005

Failed to initialize the Common Language Runtime (CLR)

BPool Based Errors:

 i) BPool::Map: no remappable address found.

ii) BufferPool out of memory condition

iii)LazyWriter: warning, no free buffers found.

BPool (or) MemToLeave errors:

i) Error: 17803 “Insufficient memory available..”

ii) Error: 701, Severity: 17, State: 123.

There is insufficient system memory to run this query.

b) If MTL is the reason for the Memory issue we have to determine  whether it is SQL Server or some non-SQL component that is using the most MemToLeave memory

Query:

select sum(multi_pages_kb)  from sys.dm_os_memory_clerks

In MTL if SQL Server Owned memory is very less ,then determine if there are COM objects, SQL Mail, or 3rd party extended stored procedures being used, and move them out of process if possible(or contact App Team).

c) If MTL is not the reason then we need to focus on BPool portion and who is occupying more in BPool.

To find out who is consuming more in BPool fire below query:

select  *  from sys.dm_os_memory_clerks order by  Single_pages_kb  desc

To calculate the BPool approximate usage size use below command:

select  sum(single_pages_kb) from sys.dm_os_memory_clerks

Extra Counters to Monitor:-

Monitor PLE(Perfmon Counter):-
PLE is the expected time a read page from file is maintained in Buffer Pool.

Monitor BCHR(Perfmon Counter):-

Buffer Cache Hit Ratio is the utilization ratio of Buffer Pool towards I/O operation. Microsoft recommends >95% for OLTP and >90% for OLAP. 

Thursday, 23 June 2016

Understanding the SQL Server error Logs

Error Logs name itself defines as it will logs the error events raised by SQL Server database engine or SQL Server Agent.
 Error Logs are the main source to troubleshooting the SQL Server problems.
 SQL Server supports 2 types of error logs

                * SQL Server Logs
                * SQL Agent Logs

What is recorded exactly in error logs?

1.       SQL Server start up events including database recovery.
2.       Backup and restore details.
3.       Any failed SQL Server jobs
4.       User defined error message which has WITH LOG clause.
5.       Maintenance related DBCC statements, such as DBCC CHECKDB and DBCC CHECKALLOC.
6.       Turning trace flags on or off.
7.       SQL Servers usage of a particular session for a long period of time.
8.       Starting and stopping Profiler traces

 By default SQL Server supports

                1 - Current Log
                6 - Archieve Logs

 Error logs are present in LOG folder of respective instance.
 We can read error logs using

                sp_readerrorlog
                xp_readerrorlog

 By default when the server was restarted the error logs are recycled automatically. We can recycle error logs using

                sp_cycle_errorlog


We can configure up to 99 error logs per instance.
Things about Error Logs everyone should know

How to View SQL Server Error Log file location

EXEC xp_readerrorlog 0,1,"Logging SQL Server messages in file";

You can change the default location of SQL Server error logs by using Startup parameter -e
“-e C:\Logs\NewLogs”

How to see the error logs location without SSMS
Open SQL Server Configuration Manager
Go to Start > All Programs > Microsoft SQL Server 2005 (or 2008) (or 2008 R2) > Configuration Tools > SQL Server Configuration Manager

How to view the SQL Server error log using management studio –
Connect to SQL Server > In Object Explorer > Expand a server > Expand Management > and then Expand SQL Server Logs. Right-click a log and click View SQL Server Log.
By Default SQL Server maintains 6 Error log files only. Default settings can be changed to any number between 6-99.


To change the default settings
Connect to SQL Server > In Object Explorer > Expand a server > Expand Management > and Right-click > Configure > Check the Limit box and change.

There are problems with the size of error logs. One error log file can grow up to any limit if SQL server is not restarted since a long time. On the other hand, a file can be very small if SQL server is restarted frequently.

As a best practice – SQL Server error logs can be recycled by creating a SQL Job which runs at a regular interval. That’s how you can prevent the problems of loading or reading the error log.


SQL Server error logs can give you information about machine type. Use the script below.
 EXEC xp_readerrorlog 0 ,1 ,"Manufacturer";








 SQL Server error logs can give you the port number which is used by SQL Server

 EXEC xp_readerrorlog 0 ,1 ,"Server is listening on";









SQL Server error logs can give you information about Startup Parameter that are used by SQL Server

EXEC xp_readerrorlog 0,1 ,"Registry startup parameters";








SQL Server error logs can give you information about Dedicated admin connection is used by SQL Server

 EXEC xp_readerrorlog 0 ,1 ,"Dedicated admin connection support";







SQL Server error logs can give you information about OS Process ID used by SQL Server

EXEC xp_readerrorlog 0,1 ,"This instance of SQL Server last reported using a process ID";

SQL Server error logs can give you information about SQL Server authentication Mode
EXEC xp_readerrorlog 0,1 ,"Authentication mode";




Monday, 16 May 2016

SQL DBA: Script to find missing log backup

Situation : While performing log shipping failover I got the error like LSN mismatch .

To fix this issue I need to find the missing log backup details.

For this we need to use system tables from master (sysdatabases)  and msdb databases  (backupset and backupmediafamily )

SELECT sd.NAME
 ,bs.TYPE
 ,bs.database_name
 ,bs.backup_start_date AS last_backup
FROM master..sysdatabases sd
LEFT JOIN msdb..backupset bs ON rtrim(bs.database_name) = rtrim(sd.NAME)
LEFT JOIN msdb..backupmediafamily bmf ON bs.media_set_id = bmf.media_set_id
WHERE sd.NAME = 'DBA_Info' -- pass the db name here
 AND bs.backup_start_date > getdate() - 10
ORDER BY sd.NAME
 ,last_backup

Script to find Backup or Restore estimation completion details.

As a DBA , It is routine activity to perform the backup / restore activities .
Some times either client or application team will be forcing us to share the estimation details about when will be backup or restore will complete .

We have to use sys.dm_exec_sql_text & sys.dm_exec_requests  database management views (DMV's) to find the backup or restore completion details.

SELECT r.session_id
 ,r.command
 ,CONVERT(NUMERIC(6, 2), r.percent_complete) AS [Percent Complete]
 ,CONVERT(VARCHAR(20), DATEADD(ms, r.estimated_completion_time, GetDate()), 20) AS [ETA Completion Time]
 ,CONVERT(NUMERIC(10, 2), r.total_elapsed_time / 1000.0 / 60.0) AS [Elapsed Min]
 ,CONVERT(NUMERIC(10, 2), r.estimated_completion_time / 1000.0 / 60.0) AS [ETA Min]
 ,CONVERT(NUMERIC(10, 2), r.estimated_completion_time / 1000.0 / 60.0 / 60.0) AS [ETA Hours]
 ,CONVERT(VARCHAR(1000), (
   SELECT SUBSTRING(TEXT, r.statement_start_offset / 2, CASE 
      WHEN r.statement_end_offset = - 1
       THEN 1000
      ELSE (r.statement_end_offset - r.statement_start_offset) / 2
      END)
   FROM sys.dm_exec_sql_text(sql_handle)
   ))
FROM sys.dm_exec_requests r
WHERE command IN (
  'RESTORE DATABASE'
  ,'BACKUP DATABASE'
  )

Monday, 2 May 2016

Full Vs Bulk Logged Vs Simple Recovery models

FULL RECOVERY
BULK LOGGED
SIMPLE RECOVERY
WAL concept 100% applicable. Every transaction write into transaction log
WAL Concept is applicable only except bulk transactions.
WAL concept 100% applicable.
Every transaction write into transaction
Log
Transactions are fully logged
Transactions are minimally logged
Transactions are fully logged
Point in time recovery is possible
Point in time recovery is not possible
Point in time recovery is not possible
No data loss or very minimal data loss
Data loss only when we perform bulk operation
Data loss chances are very high
Performance impact is slight
Performance impact is slight in normal transaction but where as in bulk transaction no major performance impact
No performance impact
Disk consumption is high
Disk consumption is high when normal transactions but in bulk Disk consumption is low
Disk consumption is Less
Use for production OLTP environments
Use for production when there is any bulk transactions
Always use for development servers