concept transaction in category sql

appears as: trsactions, transaction, trsaction, Trsaction, TRANSACTION, trsactions, A trsaction
SQL Server MVP Deep Dives Vol. 2

This is an excerpt from Manning's book SQL Server MVP Deep Dives Vol. 2.

The clustered index of an ever-increasing value that never changes will never get fragmented by INSERTs or UPDATEs (except UPDATEs that increase row size), thus reducing the need for index maintenance. A clustered index requires an exclusive, table-level lock in order to be rebuilt. Consequently, during a clustered index rebuild, table data is unavailable to the application except for transactions that are using the read-uncommitted isolation level. Therefore, using IDENTITY keys as clustered primary keys can reduce potential downtime.

begin transaction;

select next value for dbo.MySequence;

rollback transaction;

select next value for dbo.MySequence;
go

8.  Now that the row has been inserted, a Commit log record is created, and all of the log records associated with the transaction are now flushed (written) to the transaction log file from the log cache. It’s only at this point the transaction is complete. This process is known as write-ahead logging. In other words, all data modifications must first be made to the transaction log before they’re written to the data file. Because of write-ahead logging, should the SQL Server fail before the dirty data page is written back to the data file, the transaction can be rolled forward during the recovery process when SQL Server is restarted.

9.  Now that the transaction is complete, transactional locks are released and the batch is now considered complete, and the client is notified that the batch completed successfully.

So how does this example explain how a transaction log can become a bottleneck? Instead of just one row being inserted, let’s assume that tens of thousands of rows per second are being modified in a database from hundreds of simultaneous transactions. This means that all the log records for every data modification have to be written to the log file. If the I/O subsystem isn’t up to the task, transactions will have to wait before their log records can be written to the transaction log file, keeping them open longer than necessary and starting a potential chain of events (extra wait states, blocking, and so forth) that result in a bottleneck that can further hurt the performance of the server.

SQL Server DMVs in Action: Better Queries with Dynamic Management Views

This is an excerpt from Manning's book SQL Server DMVs in Action: Better Queries with Dynamic Management Views.

DMV information includes metrics that relate to indexes, query execution, the operating system, Common Language Runtime (CLR), transactions, security, extended events, Resource Governor, Service Broker, replication, query notification, objects, input/output (I/O), full-text search, databases, database mirroring, change data capture (CDC), and much more. In addition, many corollary areas enhance and extend the DMV output. I’ll discuss these a little later, in the section titled “DMV companions.”

A transaction is a unit of work. A unit of work is a set of SQL queries that are treated as a unit; for example, either they all commit or none of them commit their changes to the database. You can consider the movement of money from one bank account to another as two pieces of work, but to ensure consistency, they’re treated as a single unit of work, or one transaction.

Transactions affect how units of work are carried out; this influences the locking strategy and subsequent blocking. Locks are held by transactions. To ensure that the data used or modified by two or more queries is consistent, locks are taken out on the underlying data.

Before we can get into some interesting and useful scripts, it makes sense to examine the features that will allow you to understand more fully the output from the scripts. We’ll do this in the following sections, starting with an overview of transactions.

Listing 8.3. Starting an open transaction
USE IWS_TEMP BEGIN TRANSACTION INSERT INTO [dbo].[tblCountry] ([Code], [Description]) VALUES('ENG', 'ENGLAND')

Because this transaction has been started but not committed or rolled back, it’s described as open. This means it contains locks against the table dbo.tblCountry that will impact other queries that run against this table. This will allow us to investigate and discuss the impact of locks, blocking, and transactions.

8.4. The ACID properties of transactions

Transactions are often described as having ACID properties. The initialism ACID represents the atomic, consistent, isolated, and durable properties of transactions. An overview of each of the ACID properties is given in table 8.3.

SQL Server MVP Deep Dives

This is an excerpt from Manning's book SQL Server MVP Deep Dives.

What is the solution? Consider how long the locks are held and how much is being locked. The default isolation level for SQL Server, READ COMMITTED, will generally only lock one row at a time when it fetches rows. Even inside a transaction, you can get different results for the same query depending on the transaction isolation level. (Only the highest transaction isolation level, serializable, will prevent other users from changing or adding rows that your query has viewed; snapshot isolation level will let your results stay constant by saving changes temporarily for your queries.) However, transaction isolation assumes that the server can discern your actions from the structure of your database. For example, consider the following:

Listing 2. Backing up user databases
#backup.ps1 #Performs a Full backup followed by a transaction log backup on all user databases param ( [string]$srvname='MyServer\MyInstance' ) # Load SMO assembly, and if we're running SQL 2008 DLLs load the SMOExtended and SQLWMIManagement libraries $v = [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') $p = $v.FullName.Split(',') $p1 = $p[1].Split('=') $p2 = $p1[1].Split('.') if ($p2[0] -ne '9') { [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended') | out-null [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SQLWMIManagement') | out-null } $s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $srvname $bkdir = $s.Settings.BackupDirectory $dbs = $s.Databases foreach ($db in $dbs) { if ($db.IsSystemObject -eq $False -and $db.IsMirroringEnabled -eq $False) { $dbname = $db.Name $dt = get-date -format yyyyMMddHHmmss $bk = new-object ('Microsoft.SqlServer.Management.Smo.Backup') $bk.Action = 'Database' $bk.BackupSetDescription = "Full backup of " + $dbname $bk.BackupSetName = $dbname + " Backup" $bk.Database = $dbname $bk.MediaDescription = "Disk" $bk.Devices.AddDevice($bkdir + "\" + $dbname + "_db_" + $dt + ".bak", 'File') $bk.SqlBackup($s) # Simple Recovery Model has a Value Property of 3 # SQL Server 2008 doesn't recognize the enumerated value so the code is slightly different # Set a variable to run the transaction log backup, and if Simple, turn it off $trnbck = 1 if ($p2[0] -eq '9') { if ($db.DatabaseOptions.RecoveryModel -eq 'Simple') { $trnbck = 0 } } else { if ($db.RecoveryModel.value__ -eq 3) { $trnbck = 0 } } if ($trnbck -eq 1) { $dt = get-date -format yyyyMMddHHmmss $trn = new-object ('Microsoft.SqlServer.Management.Smo.Backup') $trn.Action = 'Log' $trn.BackupSetDescription = "Trans Log backup of " + $dbname $trn.BackupSetName = $dbname + " Backup" $trn.Database = $dbname $trn.MediaDescription = "Disk" $trn.Devices.AddDevice($bkdir + "\" + $dbname + "_tlog_" + $dt + ".trn", 'File') $trn.SqlBackup($s) } } }