How applications generally connect to database?

 1. When application try to connect, uses Config file (Configuration file--Resides in application server). Config file (.txt) should contains SQL Server instance name + user name [service account name--DBA team should add the account into SQL Server under security] + Password [Strong]

Application use all these details from Config file and point connection to SQL server database

Real time points:

> Any business 2 data centers maintains called PRODUCTION and DR data center.

> Always distance between data centers should not be more than 50 KM.

> Few people always work inside the data centers.

sp who3 script to check processes in SQL server

 --who3 to check active sessions in SQL Server.


SELECT

    SPID                = er.session_id

    ,BlkBy              = er.blocking_session_id      

    ,ElapsedMS          = er.total_elapsed_time

    ,CPU                = er.cpu_time

    ,IOReads            = er.logical_reads + er.reads

    ,IOWrites           = er.writes     

    ,Executions         = ec.execution_count  

    ,CommandType        = er.command         

    ,ObjectName         = OBJECT_SCHEMA_NAME(qt.objectid,dbid) + '.' + OBJECT_NAME(qt.objectid, qt.dbid)  

    ,SQLStatement       =

        SUBSTRING

        (

            qt.text,

            er.statement_start_offset/2,

            (CASE WHEN er.statement_end_offset = -1

                THEN LEN(CONVERT(nvarchar(MAX), qt.text)) * 2

                ELSE er.statement_end_offset

                END - er.statement_start_offset)/2

        )        

    ,STATUS             = ses.STATUS

    ,[Login]            = ses.login_name

    ,Host               = ses.host_name

    ,DBName             = DB_Name(er.database_id)

    ,LastWaitType       = er.last_wait_type

    ,StartTime          = er.start_time

    ,Protocol           = con.net_transport

    ,transaction_isolation =

        CASE ses.transaction_isolation_level

            WHEN 0 THEN 'Unspecified'

            WHEN 1 THEN 'Read Uncommitted'

            WHEN 2 THEN 'Read Committed'

            WHEN 3 THEN 'Repeatable'

            WHEN 4 THEN 'Serializable'

            WHEN 5 THEN 'Snapshot'

        END

    ,ConnectionWrites   = con.num_writes

    ,ConnectionReads    = con.num_reads

    ,ClientAddress      = con.client_net_address

    ,Authentication     = con.auth_scheme

FROM sys.dm_exec_requests er

LEFT JOIN sys.dm_exec_sessions ses

ON ses.session_id = er.session_id

LEFT JOIN sys.dm_exec_connections con

ON con.session_id = ses.session_id

CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) AS qt

OUTER APPLY 

(

    SELECT execution_count = MAX(cp.usecounts)

    FROM sys.dm_exec_cached_plans cp

    WHERE cp.plan_handle = er.plan_handle

) ec

ORDER BY

    er.blocking_session_id DESC,

    er.logical_reads + er.reads DESC,

    er.session_id


KILL all the connections for a database in SQL Server

 ——- KILL all the connections for a database ——

Use Master

Go

Declare @dbname sysname

Set @dbname = 'Put-Your-DB-Name'

Declare @spid int

Select @spid = min(spid) from master.dbo.sysprocesses

where dbid = db_id(@dbname)

While @spid Is Not Null

Begin

Execute ('Kill' + @spid)

Select @spid = min(spid) from master.dbo.sysprocesses

where dbid = db_id(@dbname) and spid > @spid

End


Top 10 Wait States in SQL Server

 select top 10 *

from sys.dm_os_wait_stats

where wait_type not in --remove common waits to identify worst bottlenecks

'KSOURCE_WAKEUP', 'SLEEP_BPOOL_FLUSH', 'BROKER_TASK_STOP',

'XE_TIMER_EVENT', 'XE_DISPATCHER_WAIT', 'FT_IFTS_SCHEDULER_IDLE_WAIT',   

'SQLTRACE_BUFFER_FLUSH', 'CLR_AUTO_EVENT', 'BROKER_EVENTHANDLER',

'LAZYWRITER_SLEEP', 'BAD_PAGE_PROCESS', 'BROKER_TRANSMITTER', 

'CHECKPOINT_QUEUE', 'DBMIRROR_EVENTS_QUEUE', 'LAZYWRITER_SLEEP', 

'ONDEMAND_TASK_QUEUE', 'REQUEST_FOR_DEADLOCK_SEARCH', 'LOGMGR_QUEUE', 

'SLEEP_TASK', 'SQLTRACE_BUFFER_FLUSH', 'CLR_MANUAL_EVENT',

'BROKER_RECEIVE_WAITFOR', 'PREEMPTIVE_OS_GETPROCADDRESS', 

'PREEMPTIVE_OS_AUTHENTICATIONOPS', 'BROKER_TO_FLUSH'

order by wait_time_ms desc

Script to Identifying possible CPU pressure via signal wait time

 SELECT SUM(signal_wait_time_ms) AS TotalSignalWaitTime ,

( SUM(CAST(signal_wait_time_ms AS NUMERIC(20, 2)))

/ SUM(CAST(wait_time_ms AS NUMERIC(20, 2))) * 100 )

AS PercentageSignalWaitsOfTotalTime

FROM sys.dm_os_wait_stats