Search This Blog & Web

Thursday, May 29, 2014

SQL Server Storage Enigne: Update Statement Life Cycle Summary

From the previous blog
SQL Server Storage Engine: Select Statement life cycle summary

Now we will start learning about What happens in database engine when an update statement will be issued. Same structure follows as with select statement for SNI, and Rational Engine and Access Method part, as we can see till Point 5 from the previous post and we can also see this in attached picture.



At the point of Access Method as in Select statement It goes to Buffer Manager here it goes to Transaction Manager.

Transaction Manager

Write-ahead logging (WAL): The Access Methods code requests that the changes it wants to make are logged, and the Log Manager writes the changes to the transaction log.

Writing to the transaction log is the only part of a data modification transaction that always needs a physical write to disk because SQL Server depends on being able to reread that change in the event of system failure.

What’s actually stored in the transaction log is details of the page changes that occurred as the result of a modification statement. This is all that SQL Server needs in order to undo any change.

The actual data modification can only be performed when confirmation is received that the operation has been physically written to the transaction log. This is why transaction log performance is so crucial.

Once confirmation is received by the Access Methods, it passes the modification request on to the Buffer Manager to complete.

Buffer Manager

The page that needs to be modified is already in cache, the page is modified in the cache, and confirmation is sent back to Access Methods and ultimately to the client.

The key point here is that the UPDATE statement has changed the data in the data cache, not in the actual database file on disk.

This is done for performance reasons, and the page is now what’s called a dirty page because it’s different in memory from what’s on disk.


These dirty pages will be written back to the database file periodically whenever the free buffer list is low or a checkpoint occurs.

Tuesday, May 27, 2014

SQL Server Storage Enigne: SELECT Statement Life Cycle Summary



Figure shows the whole life cycle of a SELECT query, described here:

1. The SQL Server Network Interface (SNI) on the client established a connection to the SNI on the SQL Server using a network protocol such as TCP/IP. It then created a connection to a TDS endpoint over the TCP/IP connection and sent the SELECT statement to SQL Server as a TDS (Tabular data stream) message.

2. The SNI on the SQL Server unpacked the TDS message, read the SELECT statement, and passed a “SQL Command” to the Command Parser.

3. The Command Parser checked the plan cache in the buffer pool for an existing, usable query plan that matched the statement received. When it didn’t find one, it created a query tree based on the SELECT statement and passed it to the Optimizer to generate a query plan.

4. The Optimizer generated a “zero cost” or “trivial” plan in the pre-optimization phase because the statement was so simple. The query plan created was then passed to the Query Executor for execution.
    Next steps involve in making execution plan will be

         Phase 0 — During this phase the optimizer looks at nested loop joins and won’t consider            parallel operators.The optimizer will stop here if the cost of the plan it has found is < 0.2. A plan generated at this phase is known as a "transaction processing", or TP, plan.
         Phase 1 — it uses subset of the possible optimization rules and looks for common patterns for which it already has a plan. The optimizer will stop here if the cost of the plan it has found is < 1.0. Plans generated in this phase are called "quick" plans.
         Phase 2 — This final phase is where the optimizer pulls out all the stops and is able to use all of its optimization rules. It also looks at parallelism and indexed views (if you’re running Enterprise Edition). Plans created in this phase have an optimization level of “Full.”

5. At execution time, the Query Executor determined that data needed to be read to complete the query plan so it passed the request to the Access Methods in the Storage Engine via an OLE DB interface.

6. The Access Methods needed to read a page from the database to complete the request from the Query Executor and asked the Buffer Manager to provision the data page.

7. The Buffer Manager checked the data cache to see if it already had the page in cache. It wasn’t in cache so it pulled the page from disk, put it in cache, and passed it back to the Access Methods.

8. Finally, the Access Methods passed the result set back to the Relational Engine to send to the client.

Tuesday, May 13, 2014

Capture Deadlock information and History using SQL Server

Every DBA has some tasks to prepare database health check report on different database related key points i.e. database backup size trend, database size trend, index utilization and fragmentation, dead lock information, statistics report, event and error log, cache hit ratio and so on. It is easy to get backup report, database sizes, indexes report, statistics report event and error log but when it comes to deadlock information this is most import question in every DBA’s mind. How can I get information about deadlocks that happened last night. I will explain other tasks detail step by step in different post but In this post I will try to explain about deadlock

What is Deadlock


Deadlocks happen when two or more processes trying to access same source and both are holding locks on them.  Both processes trying to do some action on same instance and both are waiting for each other to complete its action. It is hard to reproduce it in real time but I did it by force for testing purpose.
Think of going shopping for furniture and two people (Person A and Person B) go in at same time.  Unknown to them they both want the same goods (Good A and Good B).  One starts searching from right another person starts searching from left side of the store.  Person A finds Good A (Exclusive Lock) and starts looking for Good B (Shared Lock).  But while Person A was searching for Good A, Person B found Good B (Exclusive Lock) and starts Looking for Good A (Shared Lock).  When they find the other good they realize it is already reserved and cannot have it, neither Person A nor Person B is willing to let the other person have both the Goods. Therefore we are stuck in a deadlock.

Create a Deadlock


Let’s start this example by creating a new table as I created a table (abc) with three columns with some default data.


Sample code Starts transaction with Begin Trans but there is no commit or rollback transaction. It means this transaction is still doing some work and is busy and it has (Exclusive) lock on table (abc).



In this query window we are trying to delete data from same table that has a lock on it. This process goes on waiting state because of lock on this table and it will go into waiting state using (Share Lock). 
While both transactions are open and waiting to complete action by each other process. I execute another insert for table (abc) that will cause deadlock and with the help of Deadlock Monitor Thread (default method to resolve deadlock issue) one process will terminate to complete other.


Search a Deadlock

We have many ways to search deadlock info, some of them are listed below

Windows Performance Monitor 

SQL Server Profiler is used to get Server Side TraceEvent Class: LocksEvent Name: Deadlock generates an XML Graph.  Very easy to read and figure out what is going on. Windows performance monitor provides all the deadlocks that have happened on your server since the last restart.  To get dead lock information using following query



SELECT cntr_value AS NumOfDeadLocks,*
  FROM sys.dm_os_performance_counters
 WHERE object_name = 'SQLServer:Locks'
   AND counter_name = 'Number of Deadlocks/sec'

   AND instance_name = '_Total'








Trace Flags1204 and 1222

Trace flag 1204 has existed since at least SQL Server 2000.  And Trace Flag 1222 was introduced in SQL Server 2005.Both output Deadlock Information to the SQL Server ERRORLOG. You can enable trace flag setting using following query.

DBCC TRACEON (1222,-1)

You can review trace status using following query

dbcc tracestatus (1222, 1204)

Once deadlock occurred it can be reviewed in error log. Using following query you can review deadlock in detail.

EXEC sp_readerrorlog

System Health

System Health is like Default trace that exists in the background.  System Health has been around since 2008, however was not visible in the GUI. Following query used to review deadlock information.

SELECT
     xed.value('@timestamp', 'datetime2(3)') as CreationDate,
     xed.query('.') AS XEvent
FROM
(
     SELECT CAST([target_data] AS XML) AS TargetData
     FROM sys.dm_xe_session_targets AS st
     INNER JOIN sys.dm_xe_sessions AS s
            ON s.address = st.event_session_address
     WHERE s.name = N'system_health'
                 AND st.target_name = N'ring_buffer'
) AS Data
CROSS APPLY TargetData.nodes('RingBufferTarget/event[@name="xml_deadlock_report"]') AS XEventData (xed)
ORDER BY CreationDate DESC


We can click on any of the XML links to get the complete XML information for deadlock and then save it as Filename.XDL file to get it in Deadlock Graph in SSMS.
System Health is great, however it doesn’t save deadlocks indefinitely.  So next let’s look at implementing Deadlock Monitoring using Extended Events, much like SQL Server Server Side Trace.

Extended Events

GUI Interface for Extended events is in SQL Server 2012 RTM. However, after you create the Extended Events Trace (Session) you can take the SQL code and run it against 2008 or 2008 R2 to create a similar trace.


Using extended event you can create a new Session. After setting up a Wizard you will get deadlock information in mentioned files.


Following is the query to create extended event session in 2008 and 2008 R2

CREATE EVENT SESSION [Deadlock_Monitor] ON SERVER
 ADD EVENT sqlserver.xml_deadlock_report
 ADD TARGET package0.event_file(SET filename=N'C:\Deadlock\Deadlock_Monitor.xel')
 WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=ON)
 GO


Reviewing XML detail for deadlock, you need to see XML document as in picture.


From the attached picture you can see which two statements are involved in creating deadlock. In the <Owner> tab process lock and type is mentioned. “X” Exclusive for and “S” Shared in waiting state.















Deadlock Monitor Thread

In SQL Server to prevent this stalemate (i.e. a deadlock) from filling up the system, we have a Deadlock Monitor thread that is running in the background to “help” resolve deadlocks.

If we look at sys.dm_os_waiting_tasks we find a system task that is always waiting: REQUEST_FOR_DEADLOCK_SEARCH.  This thread wakes up every five seconds to see if we have any deadlocks.  If it finds any deadlocks, it terminates one of the sessions to keep all resources available for other session. But it is a little tricky how SQL Server decide by itself.
So one wonders what happens when they are the same?  It’s simple, SQL Server will kill whoever came in second.It wakes up every 5 seconds to check for deadlocks. If it finds any, it follows above process to decide how to resolve it.  However, first time it wakes up, it wakes up the second time right away, to make sure it is not a nested deadlock.  If there is one it kills that and goes back to sleep.  Next time it wakes up it wakes up 4.90 seconds (estimate on wake, I think it’s actually 10ms).  It will keep going down to as small as 100ms; that is, it will wake up 10 times per second to deal with Deadlock Issues.

Well, SQL Server does have to worry a bit to resolve this scenario and decided which session is going to kill.  However it has to make sure to kill the session that is the easiest to rollback.  Because if SQL Server kills a transactions, any work it has done must be rolled back to bring the database to consistent state.  It decided this looking at the LOG USED value. Whichever session generate in last have less used value for Log and it will be killed to release resources for other process.

List of available Microsoft SQL server updates from start to 2014

Here is the list of available Microsoft SQL server updates from 2000 to 2014.

http://support.microsoft.com/lifecycle/search/default.aspx?sort=PN&alpha=SQL+Server&Filter=FilterNO&wa=wsignin1.0