Tag books online

An Introduction to SQL Server System Databases

SQL Server 2008 (and 2005) contain five special databases: master, model, tempdb, msdb, and mssqlsystemresource (aka Resource). These databases are used by SQL Server for its own maintenance and management.

master

The master database contains all of the system level information for SQL Server – all of the logins, linked servers, endpoints, and other system-wide configuration settings. The master database is also where SQL Server stores information about the other databases on this instance and the location of their files. If the master database is not present, SQL Server cannot start.

Always take regular backups of the master database.

Since the master database hold all of the information related to logins, endpoints, linked servers, and user databases, it’s important that you take a backup of the master database after configuring any of these server level changes. Otherwise, if your SQL Server suffers a catastrophic failure, those changes will be lost to the sands of time.

Books Online contains a great deal of helpful information about the master database that you can find in the appropriately named master Database article.

Speaking from personal experience, one of the most important points from that article is this: “Do not create user objects in master. Otherwise, master must be backed up more frequently.” I cannot tell you the number of times I have connected to a development server, opened up a file, and run the DDL contents of the file only to realize that I have created new tables on the master database because I did not have a USE AdventureWorks at the top of my DDL script.

model

The model database is used as a template whenever a new user database is created. You can change most database properties, create users, stored procedures, tables, views, etc – whatever you do will be applied to any new databases.

The nice thing is that you can create a guaranteed set of users, stored procedures, and options (including collation options) by making changes to the model database. Once those changes are in place, they are applied to every new database.

Outside of its role as a template, model doesn’t do anything else. You can find more information in the model Database article of Books Online.

msdb

msdb is used by the SQL Server Agent, database mail, Service Broker, and other services. If you aren’t actively working with things like jobs, alerts, log shipping, etc you can pretty safely ignore msdb… sort of.

One important item is that msdb holds backup history. Using the msdb tables (you can start by taking a look at msdb.dbo.backupset), it’s possible to determine when each database and filegroup was last backed up. This is very useful, especially when you’ve just started working at a new company or taken over the maintenance of new servers.

A word of warning: you need to make sure that you are pruning old backup history from msdb. Leaving old backup data can slow down the performance of backup and restore operations. Brent Ozar documented this during his Backup Tricks Week article Checking MSDB Cleanups. In this article, the sp_delete_backuphistory system stored procedure is mentioned. This stored procedure will delete information older than the @oldest_date parameter. It is important that you are incredibly careful when using this store procedure and don’t attempt to delete all of the backup history data in your msdb at once. Attempting to clear out a large number of database backup history records in one fell swoop can have an adverse effect on performance. I suggest removing the data one to two days at a time. It’s tedious, I know, but your users will thank you.

Resource database

The resource database is a hidden system database. This is where system objects are stored. It isn’t possible to see the resource database by normal means. However you can see the data file by navigating to C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Binn. The exact size and modification data of this file will be different from version to version, but the modified date should be the same date that you see when you run SELECT @@version.

It is best to think of the resource database as if it were another system DLL. The resource database is designed to make it easy for quick database upgrades. If new system objects are being put in place, it is only necessary to swap out the resource database MDF file.

Typically, the only way to view the contents of the resource database is using the OBJECT_DEFINITION system function.

SELECT OBJECT_DEFINITION(OBJECT_ID('sys.objects'))

-- on my test system this yields the following:
CREATE VIEW sys.objects AS
    SELECT name, object_id, principal_id, schema_id, parent_object_id,
        type, type_desc, create_date, modify_date,
        is_ms_shipped, is_published, is_schema_published
    FROM sys.objects$

tempdb

We come, at last, to tempdb. Tempdb is the workhorse of the system databases. It is the workspace that SQL Server uses to store the intermediate results of query processing and sorting. You know how you see those spools in your execution plans? When you see one of those, SQL Server is probably spooling the data to a temporary table in the tempdb. Outside of storing temporary results, tempdb is also used during snapshot isolation and for user created temporary tables (this includes table variables).

One thing that is interesting to note about tempdb is that it is re-created every time the SQL Server service is started. Any objects that you have created in tempdb will be gone once the server restarts. If you want specific tables or stored procedures to always be available in tempdb, you will need to add them to the model database or else use a stored procedure to create them in tempdb when the SQL Server is started.

A properly sized and configured tempdb is vital for effective SQL Server performance. By default tempdb starts at 8MB in size and will continue to grow by ten percent until the drive is full. If the tempdb is sized too small, system resources will be used growing the tempdb file. This overhead can be avoided by increasing the default size of tempdb.

SQL Server System Databases
System Databases
Working with tempdb in SQL Server 2005
Microsoft SQL Server 2008 Internals pp 126 – 128

My First Microsoft Connect Item

I finally feel like a real SQL Server professional! I’ve submitted my first Connect feedback to Microsoft for a new feature of SQL Server: BOL for mobile devices.

This is a dream I’ve had since I first got my grubby little paws on my iPhone. Every day since then I’ve dreamed of having a copy of SQL Server Books Online on my iPhone. It would be better than games and movies on my iPhone. Heck, this would be better than just about any app for the iPhone. I even mentioned it to every Microsoft employee I could find at the PASS Summit. Most of them looked at me like I was at least a little bit crazy.

Why am I up in arms about this?

Do you know how many SQL questions I field? I’m pretty sure that if you’re reading this blog you do know how many SQL questions I field because you probably field more questions than I do. I get a lot of these questions when I’m nowhere near a computer and I’m not about to try to use MSDN over the EDGE network on a tiny little screen, which makes it tough to answer them. Plus, I wouldn’t have to lug giant SQL books to the doctor’s office so I have reading material. Instead I could pull out my trusty mobile device and read all about SQL Server while I wait for far too long to be told I’m not sick.

If you like the idea of having access to BOL anywhere you are, please vote for this Microsoft Connect item. Frankly, you just vote for it because it’s the coolest thing since sliced bread.

Links for the Week of 2009.04.10

SQL Server

A Web Based Search for Books Online Buck Woody provides links to macros for MSDN search that will give the search bar in your browser the ability to search online inside Books Online. This has become my go to method for searching BOL. It’s much faster than opening the desktop BOL client and the results are typically exactly what I need.

The Third Pillar – Fundamentally Sound Louis Davidson discusses the third (of seven) pillars of database design.

PASS Update SQL Batman infiltrated the lair of PASS in the last round of elections and is now providing insider reports on what’s happening in our world of databases. Or something like that.

Development

Meet and Code Recap My friend, co-worker, and co-conspirator in development mischief has provided a recap on the Meet and Code event that we put together a few weeks ago. It’s well worth checking out to get his thoughts on the thing. Especially since I wrote my thoughts on a combined total of 9 hours of sleep for the weekend.

Stuff & Things

Narrative Planetarium Ideas for exploring narratives provided by the night time view of a city’s skyline.

A Day in the Life of the Boss (NSFW) Normally I (probably) wouldn’t link to something NSFW, but in this case this was too funny. Really it’s just bad language in a flow chart, but some people could be offended. You have been warned, this link contains offensive language (and no, it’s not the words ‘CREATE CURSOR’).

This site is protected with Urban Giraffe's plugin 'HTML Purified' and Edward Z. Yang's Powered by HTML Purifier. 219 items have been purified.