Skip to main content

Transactions

There are basically three set of statements:

  • DDL: Data Definition Language - Create and Alter statements fall in this category
  • DML: Data Manipulation Language - Insert, Update and Delete statements fall in this category
  • DCL: Data Control Library - Commit and Rollback statements fall in this category
DDL and DCL are permanent in nature and DML statements are actually treated as transactions. Transactions should follow atomicity i.e. All or none property i.e. if execution happens it will happen for all the statements in a transactions or else it would not happen for anyone.

In SQL server every command or statement(DML) is treated as one transaction.
So there are basically two types of transactions:
  • Implicit: Where we do not need to add any keyword from our side to begin and end a transaction. SQL server itself adds it at the beginning and completion of each statement.
  • Explicit:  Where we need to add "Begin Tran" at the beginning and 'commit or rollback" at the end of the statement or statements.
Example:

BEGIN TRANSACTION

Statement 1
Statement 2
       .
       .
       .
Statement n

COMMIT OR ROLLBACK

Commit is meant for completing the transaction while Rollback for reverting back the transaction

When we are talking about the transaction, you should know about the concept of primary key. It helps in uniquely identifying the rows in a table.
There are two attributes of a primary key:
1. Unique
2. Not null: No 2 nulls can exist in a table

As already discussed in previous topics, we are having two files for a database .mdf and .ldf. .mdf file can have both committed or uncommitted data at any point of time. Whatever committed data is there will be redone called as redo phase and whatever uncommitted data is there will be rolled back called as undo phase.

So, how it works?

Lets take an example, suppose you were executing a transaction which consists of 1000 statements and in the middle of the execution power goes or system shuts down for any reason. Now, when the system comes up recovery starts. For recovery to happen, it goes through 3 phases:
1. Analysis
2. Undo
3. Redo

So, when system comes up, first SQL server analyses what statements it needs to redo and what needs to be undo. After that, uncommitted transactions are rolled back i.e. undo and committed ones are committed i.e. redo. This is how recovery happens in SQL server.

Now, what happens internally?

Suppose you have a box i.e. system PC1 which is having instance I1 i.e. PC1\I1 and 2 GB RAM is installed. As soon as data comes to .mdf it is copied to RAM in the form of pages and are called as clean pages. As soon as we make changes in the page that came from .mdf to RAM, it is called as Dirty Page. Now, whatever transactions are there related to the data that came from RAM are written in .ldf first.

Now, the question arises, if we are having only 2 GB RAM and all the data is getting copied on RAM, why does not it gets full then?

So for this, we have two background processes:

1. Lazy writer: This background process works on server level. It waits for RAM to reach the threshold limit e.g. RAM threshold is set to 80%, it will wait till 80% RAM gets full. When the RAM reaches the threshold value, lazy writer copies the dirty pages from RAM to .mdf and removes those pages which have been copied to .mdf from RAM. It flushes buffer pool(execution plan, compiled data, data pages and memory objects). Lazy writer follows FIFO(First In First Out) or LRU(Least Recently Used).

2. Checkpoint: This background process works on DB level. It is supposed to run at regular intervals. It copies dirty pages from RAM to .mdf and does not removes any page. It flushes only data pages to disk.

WAL: WAL stands for Write Ahead Logging Mechanism. It means that before writing data to .mdf data file, it is written in .ldf.

LSN: LSN stands for Log Sequence Number. It is a unique no. that is attached to every transaction. Every transaction has its own LSN which is unique.

How to see transaction Log?

  • You should know the dbid i.e. database ID. For that, you can run          select * from sys.sysdatabases
  • When you know dbid, you can run                                                       dbcc log(ID,3) 
Here, second argument can be either 0, 1, 2 or 3 based on how much detailed result you want.

To know the transaction information, you can also run
select * from fn_dblog(null, null)

To know about a particular transaction: 
select * from fn_dblog(null, null) where [Transaction Name]= 'DELETE'

Comments

Popular posts from this blog

SQL Server Upgrade Issues

Pre-Requisites We usually think of SQL Server simply as a database engine, while it is a data platform which supports data warehousing, reporting, data integration and data analytics. An in-depth planning is required when upgrading from one version of SQL Server to a newer version along with the features that SQL Server offers. Upgrading these extensive number of features can be remarkably complicated and time-consuming than just upgrading the database engine alone.   Below mentioned are few of the “lessons learned” that help reduce risks of data losses etc.: 1.        Application compatibility: Before planning for upgrade activity, make sure that you discuss the compatibility of SQL Server and the associated application with your application vendor.  2.        Preparation for migration: In case your upgrade activity fails due to some reason and there is no solution possible other than migration,...

Data Compression - Row & Page ( Sketch notes)

Just some notes on compression... something helpful for a beginner.

Best Practices to be followed for SQL Server

1. Memory Capping SQL Server is an application that uses as much memory as is provided to it. Setting memory too high can cause other applications to compete with SQL on the OS and setting memory too low can cause serious problems like memory pressure and performance issues. It is important to provide at least 4 GB memory for OS use, if you are running only SQL Server as an application on the server.  2. Data File Locations SQL Server accesses data and log files with very different I/O patterns. Data file access is mostly random whilst transaction log file access is sequential. Spinning disk storage requires re-positioning of the disk head for random read and write access. Sequential data is therefore more efficient than random data access. Separating files that have different access patterns helps to minimize disk head movements, and thus optimizes storage performance.  3. Auto growth Auto-growth of the database files should be set in MBs as it will al...