Tag Archives: SQL

Explicit Transactions

19 Jul

This article first appeared on SQL Server Central on 30 Dec 2015.

Frequently there are questions relating to transactions posted on various forums and although the questions show a basic misunderstanding of this aspect of SQL Server, sometimes the answers show some misunderstanding also.

I initially started an article about nested transactions, because that is one of the areas that appear to cause the most confusion (and I wanted to make sure I understood it too). However, in looking into this I have also investigated other areas related to explicit transactions where I believe some simple examples could help to clarify.
This article will look at the various properties and usage of explicit transactions, as well as some behaviours that are frequently misunderstood.
Continue reading

Checking for Instant File Initialization

6 Jun [url=http://www.cartoonbucket.com/cartoons/road-runner-teasing-wile-e-coyote/][img]http://www.cartoonbucket.com/wp-content/uploads/2015/07/Road-Runner-Teasing-Wile-E.Coyote-600x489.jpg[/img][/url]

Background
When SQL Server extends data or log files it fills the newly-acquired space with zeroes, to overwrite any data that may have been there beforehand. This action takes place when a database is created, restored, files are extended or added.
This can take a long time, depending upon the amount of space being initialised. This is one good reason for sizing your file requirements accurately and choosing when to make any extensions, rather than having the system perform them when the system is probably busy and holding up any number of processes.

What is Instant File Initialization?
Instant File Initialization (IFI) removes the zero-fill operation, so once the space is allocated it is immediately available. This can save a considerable amount of time.

How do I check for IFI?
One way of being notified of the status of IFI is via SQLCop, a free add-on that can be integrated within Redgate’s SQL Test framework.

SQLCop Error Message


However, this requires that xp_cmdshell is enabled and so might not be suitable for all environments.

Another method is via Brent Ozar’s ‘sp_blitz’ procedure. For versions from SQL Server 2014 SP2 this is fine, as it reads the SQL Server error log for information message relating to IFI.

EXEC sys.xp_readerrorlog 0, 1, N'Database Instant File Initialization: enabled';

Response from xp_readerrlog

From SQL Server 2008R2 SP1 a DMV has been available, which shows (amongst other things) if IFI is enabled and the Service Account, which is required should you need to enable IFI.

SELECT  d.servicename ,
		d.service_account ,
		d.instant_file_initialization_enabled
FROM    sys.dm_server_services AS d;

Response from DMV

Caveats
IFI does not work against the log file – this must zero-fill as part of normal operation. It pays to size your log file correctly.

IFI does not work if Transparent Data Encryption (TDE) is used.

IFI does not work on sparse file (for example – database snapshots).

Trace flag 1806 disables IFI.

Because IFI does not overwrite newly-acquired disk space, it is technically possible to see the data within the deleted files that occupy this new disk space. This isn’t a trivial task (with DBCC PAGE) but may need to be considered.

Additional Links
sp_blitz
SQLCop
IFI and a DBCC PAGE example
Microsoft Docs on IFI

MERGE, OUTPUT and $Action

19 May

Recently I’ve been making a lot of use of the OUTPUT option for insert and update actions. From this work I’ve already posted about the behaviour of IDENTITY and suchlike.
Now I have looked at the behaviour of OUTPUT within the MERGE command.
Continue reading

Primary Key With Non Clustered Index

28 Apr

As a default, SQL Server creates a Primary Key column with a Clustered Index. However, this doesn’t have to be the case.
Continue reading

The Joys of @@IDENTITY, Scope_Identity and IDENT_CURRENT

31 Mar

For several years I have happily relied on @@IDENTITY to return the latest identity of a row when inserting data. However, I have started using the OUTPUT clause and have discovered the entertainment to be had in getting the latest identity when writing to two or more tables with what can appear to be one command. Continue reading

LDAP – Using SQL to get Active Directory details – Basic Examples

14 Jul

Recently I have had to extract user’s details from Active Directory (AD) for certain security groups. Having looked through a slew of internet resources it is obvious that the work required to do this has changed little over many years – and it looks like is was deliberately designed to make it difficult. I’d like to think it wasn’t but, that is how it looks. Continue reading

Partitioned Views

17 Dec

Partitioned views have been around since before SQL Server 2005 (whereupon partitioned tables were introduced) but this does not mean that they aren’t without their uses now.
Continue reading