Poor mans partitioning


There is absolutely nothing new about what I’m writing here, this is a really old trick back from the SQL 2000 days but there are quite a few people who are not aware of the powers of partitioning your data and this is why I’m posting. Unfortunately, native partitioning is for large corporations and companies with thick wallets where Enterprise version is the only way there is, while the rest of us will have to tweak that standard version until our eyes pop out. Well…for those of you that are not aware: there is a way to partition even with standard version and I’m gonna show you how. It will take a bit more effort than the native way and it doesn’t do everything you would hope for but it’s still pretty darn powerful.

And this little script here illustrates the whole concept; you create a few tables with some check constraints, you create a union all view and VOILA! There is your partitioning! Before you run the script make sure to turn on “Show actual execution plan” in your Management studio.

CREATE TABLE part_table1 (
  ID INT NOT NULL PRIMARY KEY CLUSTERED,
  SomeValue VARCHAR(20)
  )
GO
CREATE TABLE part_table2 (
  ID INT NOT NULL PRIMARY KEY CLUSTERED,
  SomeValue VARCHAR(20)
  )
GO
CREATE TABLE part_table3 (
  ID INT NOT NULL PRIMARY KEY CLUSTERED,
  SomeValue VARCHAR(20)
  )
GO

ALTER TABLE part_table1 WITH CHECK ADD CONSTRAINT CK_part_table1 CHECK (ID >= 0 AND ID <= 10) 
ALTER TABLE part_table1 CHECK CONSTRAINT CK_part_table1 

ALTER TABLE part_table2 WITH CHECK ADD CONSTRAINT CK_part_table2 CHECK (ID &gt;= 11 AND ID <= 100) 
ALTER TABLE part_table2 CHECK CONSTRAINT CK_part_table2 

ALTER TABLE part_table3 WITH CHECK ADD CONSTRAINT CK_part_table3 CHECK (ID &gt;= 101)
ALTER TABLE part_table3 CHECK CONSTRAINT CK_part_table3
GO

CREATE VIEW part_table
AS
SELECT * FROM part_table1
UNION ALL
SELECT * FROM part_table2
UNION ALL
SELECT * FROM part_table3
GO

INSERT INTO part_table SELECT 9, 'sqlteam'
INSERT INTO part_table SELECT 24, 'sqlteam'
INSERT INTO part_table2 SELECT 25, 'sqlteam'

SELECT * FROM part_table WHERE ID = 25

Notice in the execution plan when you do the inserts and the select. The insert is a clustered insert but as you can see it scans all the tables to find the right one which is somewhat annoying. But when you look at the final select you see where the “magic” is happening: when you do a select based on the “partitioning column” the query optimizer actually knows which data is where so it only has to search one of the tables! Pretty cool stuff…at least somewhat cool hehe. Using this method you can utilize quite a few of the partitioning techniques that are built in the native partitioning. You can for example have each partition on a different disk and you can add and remove partitions from the view as you like, just remember to do changes in the view first before you delete or rename any tables permanently…

Advertisements

Create an audit table on the fly


The following is a script that generates an audit table for a provided table name.

It will:
– Add 5 audit columns to an existing table (DataVersion, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy)
– Create 2 triggers on the production table, one for updates and one for deletes (inserts do not need audit info)
– Create an audit table with similar design as the production table
– Create a primary key index on the audit table matching that of the production table but with the DataVersion also added as a part of it

Prerequisites:
– The stored procedure “sp_ScriptTableForAudit” must be present in the database (provided here)
– The script must be run in the database that the production table resides.
– The script does not handle table names with other schemas than dbo
– The table must have a primary key

Please add feedback/bug reports etc as comments and I will try to fix them right away

/*
Script created by Henning Frettem, aka Lumbago @ SQLTeam, 2010-03-23
BE CAREFUL, THIS SCRIPT WILL MAKE CHANGES TO YOUR TABLES!!
*/

SET NOCOUNT ON

BEGIN TRY

	DECLARE @TableName varchar(100); SET @TableName = 'myTable'
	DECLARE @AuditTableName varchar(200)

	--> Checking prerequisites
	IF NOT EXISTS (
			SELECT 1
			FROM sys.index_columns sc
			  JOIN sys.indexes i
				ON sc.object_id = i.object_id
				  AND sc.index_id = i.index_id
			WHERE OBJECT_NAME(sc.object_id) = @TableName 
			  AND i.is_primary_key = 1)
		RAISERROR('There is no primary key on the table', 16, 1)

	IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME = 'sp_ScriptTableForAudit')
		RAISERROR('The stored procedure sp_ScriptTableForAudit is not present in the database', 16, 1)

	SET @AuditTableName = @TableName + '_audit'

	DECLARE @SQL nvarchar(max); SET @SQL = ''

	--> Add audit columns to the production table if they don't exist already
	IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @Tablename AND COLUMN_NAME = 'DataVersion')
	  SET @SQL = @SQL + 'ALTER TABLE ' + @TableName + ' ADD DataVersion int NOT NULL DEFAULT 1' + CHAR(13)

	IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @Tablename AND COLUMN_NAME = 'CreatedDate')
	  SET @SQL = @SQL + 'ALTER TABLE ' + @TableName + ' ADD CreatedDate datetime NOT NULL DEFAULT GETDATE()' + CHAR(13)

	IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @Tablename AND COLUMN_NAME = 'CreatedBy')
	  SET @SQL = @SQL + 'ALTER TABLE ' + @TableName + ' ADD CreatedBy nvarchar(200) NOT NULL DEFAULT SUSER_NAME()' + CHAR(13)

	IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @Tablename AND COLUMN_NAME = 'ModifiedDate')
	  SET @SQL = @SQL + 'ALTER TABLE ' + @TableName + ' ADD ModifiedDate datetime NOT NULL DEFAULT GETDATE()' + CHAR(13)

	IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @Tablename AND COLUMN_NAME = 'ModifiedBy')
	  SET @SQL = @SQL + 'ALTER TABLE ' + @TableName + ' ADD ModifiedBy nvarchar(200) NOT NULL DEFAULT SUSER_NAME()' + CHAR(13)

	IF LEN(@SQL) > 0
	  BEGIN
		PRINT @SQL
		EXEC sp_executesql @SQL
		SET @SQL = ''
	  END

	--> Create the audit table with the same definition as the production table, but add the IsDeleted column
	IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @AuditTableName)
	  BEGIN
		DECLARE @AuditTableDefinition table (
		  RowNumber int IDENTITY(1, 1),
		  DefinitionRow varchar(max)
		  )

		INSERT INTO @AuditTableDefinition
		EXEC sp_ScriptTableForAudit @Tablename = @TableName, @NewTablename = @AuditTableName

		--> Need IsDeleted column in audit table
		UPDATE @AuditTableDefinition
		  SET DefinitionRow = ', IsDeleted bit NOT NULL DEFAULT(0) )'
		WHERE RowNumber = (SELECT MAX(RowNumber) FROM @AuditTableDefinition)

		SELECT @SQL = @SQL + DefinitionRow FROM @AuditTableDefinition

		--> Run the create table script in the database
		PRINT @SQL
		EXEC sp_executesql @SQL
		SET @SQL = ''
	  END

	--> Create Update trigger
	IF NOT EXISTS (SELECT 1 FROM sys.triggers WHERE name = 'trg' + REPLACE(@TableName, '.', '') + 'Update')
	  BEGIN
		SET @SQL = @SQL + '
		  CREATE TRIGGER [trg' + REPLACE(@TableName, '.', '') + 'Update] ON ' + @TableName + '
		  FOR UPDATE
		  AS

		  SET NOCOUNT ON

		  UPDATE a SET
			a.DataVersion = b.DataVersion + 1,
			a.ModifiedDate = GETDATE(),
			a.ModifiedBy = SYSTEM_USER
		  FROM ' + @TableName + ' a
			INNER JOIN inserted b
			  ON '

		SELECT @SQL = @SQL + 'a.' + c.name + ' = b.' + c.name + ' AND '
		FROM sys.index_columns sc
		  JOIN sys.columns c
			ON sc.object_id = c.object_id
			  AND sc.column_id = c.column_id
		  JOIN sys.indexes i
			ON sc.object_id = i.object_id
			  AND sc.index_id = i.index_id
		WHERE OBJECT_NAME(sc.object_id) = @TableName
		  AND i.is_primary_key = 1
		ORDER BY index_column_id ASC

		SET @SQL = LEFT(@SQL, LEN(@SQL) - 4) + CHAR(13) + CHAR(13)

		SET @SQL = @SQL + '  INSERT INTO ' + @AuditTableName + '
			('

		SELECT @SQL = @SQL + '[' + COLUMN_NAME + '], '
		FROM INFORMATION_SCHEMA.COLUMNS
		WHERE TABLE_NAME = @AuditTableName

		SET @SQL = LEFT(@SQL, LEN(@SQL) - 1)
		SET @SQL = @SQL + ')
		  SELECT '

		SELECT @SQL = @SQL + '[' + COLUMN_NAME + '], '
		FROM INFORMATION_SCHEMA.COLUMNS
		WHERE TABLE_NAME = @AuditTableName

		SET @SQL = LEFT(@SQL, LEN(@SQL) - 1)
		SET @SQL = @SQL + ' = 0
		  FROM deleted

		  GO
		  '

		PRINT @SQL
		EXEC sp_executesql @SQL
		SET @SQL = ''
	  END

	--> Create delete trigger
	IF NOT EXISTS (SELECT 1 FROM sys.triggers WHERE name = 'trg' + REPLACE(@TableName, '.', '') + 'Delete')
	  BEGIN
		SET @SQL = @SQL + '
		  CREATE TRIGGER [trg' + REPLACE(@TableName, '.', '') + 'Delete] ON ' + @TableName + '
		  FOR DELETE
		  AS

		  SET NOCOUNT ON

		  INSERT INTO ' + @AuditTableName + '
			('

		SELECT @SQL = @SQL + '[' + COLUMN_NAME + '], '
		FROM INFORMATION_SCHEMA.COLUMNS
		WHERE TABLE_NAME = @AuditTableName

		SET @SQL = LEFT(@SQL, LEN(@SQL) - 1)
		SET @SQL = @SQL + ')
		  SELECT '

		SELECT @SQL = @SQL + '[' + COLUMN_NAME + '], '
		FROM INFORMATION_SCHEMA.COLUMNS
		WHERE TABLE_NAME = @AuditTableName

		SET @SQL = LEFT(@SQL, LEN(@SQL) - 1)
		SET @SQL = @SQL + ' = 1
		  FROM deleted

		  GO'

		PRINT @SQL
		EXEC sp_executesql @SQL
		SET @SQL = ''
	  END
END TRY
BEGIN CATCH
    PRINT Error_Message()
END CATCH