Database Files & Objects – Part 1

I am currently reading Kalen Delany and others book on SQL Server 2008 System internals. If you have not read this book from cover to cover, you should. It has a wealth of knowledge about database engine details.

To demonstrate key ideas from the book, I am going to expand on the [MATH] database that contains PRIME numbers. The trial division algorithm inside this database uses a brute force method for calculating prime numbers. This article will focus on files and objects that can be viewed through the system views, system stored procedures, or dynamic management views.

The first snippet of code creates a database name [MATH] with location, size, and file growth options.

The second snippet of code creates a file partitioning scheme to divide the discovered primes from one to six million into three groups. For each group, a file group with one secondary data file is created. Next, a partition function [PF_HASH_BY_VALUE] is created to divide the values into groups. Last but not least, a partition scheme [PS_HASH_BY_VALUE] maps the function onto the file groups.

The third snippet of code creates a table [TBL_PRIMES] with a primary key applied to the partitioning scheme. Please note that check, default and primary key constraints are demonstrated in the example.

The fourth snippet of code creates a trigger [TRG_TBL_PRIMES] that makes sure the divisor is less than the found prime. While this condition will never be meet to generate a roll back and error message, it is an good example of a data manipulation language (DML) trigger.

The fifth snippet of code creates a stored procedure. The [SP_IS_PRIME] stored procedure takes a number as input and returns 1 if the number is prime. Otherwise, it returns an zero for all non-prime numbers.

The sixth snippet of code creates a stored procedure. The [SP_STORE_PRIMES] stored procedure finds and stores discovered prime numbers from @VAR_ALPHA to @VAR_OMEGA. These discovered primes are stored in the table [TBL_PRIMES] for later analysis.

The seventh snippet of code creates view [VW_ONE_HUNDRED_K_PRIMES] to return all primes in the 100 K range.

The eight snippet of code creates function [FN_GET_PRIMES_BY_RANGE] to return a table containing prime numbers from @VAR_START to @VAR_END.

The ninth snippet of code executes the stored procedure [SP_STORE_PRIMES] to discover prime numbers from zero to six million.

In short, we have a sufficiently complicated enough database to demonstrate files and objects that can be viewed through the system views, system stored procedures, or dynamic management views. Next time, I will be reviewing code to examine the database files.

Related posts

Leave a Comment