[Top] [Prev] [Next]

Chapter 9

Managing Physical Resources


Resource Management Features

If you don't tell SQL Server where to put databases, tables, and indexes, and allocate enough space to each of them, then SQL Server uses default locations and space allocations for them. These defaults work to bring a system up and get it running, however, in almost every case, you'll achieve optimum performance if you customize SQL Server for your site. After an application stabilizes and you determine its data-handling requirements, you can fine tuning your system's physical resources by using the techniques outlined in this chapter. This chapter describes the following topics:

Storage Management Considerations

You should consider recovery and performance as the most important factors as you make decisions about the physical allocation of space to SQL Server databases. When organizations depend on their information systems, downtime can be disastrous¯resulting in lost revenue and dissatisfied customers. That's why Sybase has built availability and recovery into SQL Server architecture. SQL Server is designed to support on-line applications that must be available 24 hours a day, seven days a week.

Performance is an obvious requirement. When there are increasing amounts of data to process in an increasingly competitive business climate, performance can be the determining factor in the success of a product or service. Sybase provides a number of ways to ensure SQL Server reliability and improve performance by offering guidelines for configuring physical data placement.

Recovery

Although SQL Server supports non-stop recovery in a number of ways, disk mirroring is probably the most important method for achieving seamless failover in the event of a hard disk crash. Mirroring the database devices that contain the data and indexes¯not just the device containing the transaction log¯is required for recovery without downtime. The section below, ``Disk Mirroring'' discusses mirroring in more detail.

If you do not mirror the database device, full recovery from a media failure requires that database logs be stored on a different device from the actual data and indexes. In the event of a hard disk crash under these circumstances, you can still recreate an up-to-date database by loading a backup from the latest dump and applying the log records that were safely stored on another device.

Performance

There are some very effective ways to optimize system performance via physical placement of tables, indexes, and logs. If a table spans several physical disks, SQL Server can use partitions to distribute the table across those disks dramatically improving insert performance. Other ways to manage disks to improve performance include:

Storage Management and the System Tables

The placement of databases, tables (including the transaction log table, syslogs), and indexes is coordinated by two system tables in the master database and two more in each user database as follows:

Figure 9-1 illustrates the relationship between the tables. Column names show the joins between the tables. The "1...N" notation indicates that a single row in the table marked "1" can match many rows in the table marked "N". For example, a single segment listed in syssegments can store many objects, so it will have many matching rows in sysindexes.

Figure 9-1: The system tables that manage storage

Commands for Managing Physical Resources

Table 9-2 lists the commands most commonly used to manage the allocation of physical resources.

Table 9-1: Commands for allocating physical resources

Command Task
disk init name = "dev_name" physname = "phys_name" etc. Makes a physical device available to a particular SQL Server. Assigns a database device name (dev_name) used to identify the device in other SQL Server commands.
disk mirror name = "dev_name" mirror = "phys_name" etc. Mirrors a database device on a specific physical device.
sp_diskdefault "dev_name" etc. Adds dev_name to the general pool of default database space.
create database ... on dev_name or alter database ... on dev_name Makes database devices available to a particular SQL Server database. The log on clause places the database's logs on a particular database device.
create database ... or alter database ... When used without the on dev_name clause, these commands allocate space on the default database devices.
sp_addsegment seg_name, dbname, devname and sp_extendsegment seg_name, dbname, devname Creates a segment, named collection of space, from the devices available to a particular database.
create table ... on seg_name or create index ... on seg_name Creates database objects, placing them on a specific segment of the database's assigned disk space.
create table ... or create index ... When used without on seg_name, tables and indexes occupy the general pool of space allocated to the database (the default segment).
alter table... When used with the partition clause, allows you to create additional page chains for a table with no clustered index. The unpartition clause allows you to concatenate all page chains for a partition table.

Database Devices

A database device is dedicated to the storage of the objects that make up databases. The term "device" does not necessarily refer to a distinct physical device. It can refer to any region of a disk or a file in the file system that is used to store databases and database objects. Figure 9-2 shows a database with its log on one device, and other objects on another. This separation is important for reasons described in ``Assigning Databases to Database Devices''.

Figure 9-2: Objects within a database on a database device

Initializing Database Devices

Each database device must be prepared and made known to SQL Server before it can be used for database storage. This process is called initialization. After a database device is initialized, it can be:

The disk init command initializes new database devices. The system procedure sp_helpdevice provides information about the devices in the sysdevices table.

Designating Default Devices

To create a pool of default database devices to be used by all SQL Server users for creating databases, you use the sp_diskdefault command initializing devices with disk init. The sp_diskdefault command marks these devices as default devices. Whenever users create or alter databases without specifying a database device, new disk space is allocated from the pool of default disk space.

Assigning Databases to Database Devices

The commands create database and alter database allocate storage space to databases. In most organizations, only the System Administrator issues these commands to prevent inadvertent or unauthorized use of storage space. create database creates the new database on the devices you specify, and alter database adds space to an existing database.

You can specify one or more database devices and the amount of space that is to be allocated to the database on each device. In addition, the log on extension places the database's transaction log on a separate device or expands the log. You can add more space on the same device that the database already uses or you can specify other devices.

Assigning the Transaction Log to a Database Device

The log on extension to create database places the transaction log (the syslogs table) on a separate database device. There are several reasons for placing the logs on a separate logical database device. For example:

There are also reasons for placing the log on a separate physical device from the data tables:

Disk Mirroring

SQL Server supports software-based fault tolerance by providing disk mirroring for either the transaction log or the database itself. Mirroring a device containing a transaction log protects against loss of any committed transaction. Mirroring a database device guarantees continuous operation¯that is, non-stop recovery¯in the event of media failure.

The disk mirror command causes a SQL Server database device to be "duplicated." All writes to the primary device are copied to a separate physical device. If one of the devices fails, the other contains an up-to-date copy of all transactions.

When a read from or write to a mirrored device is unsuccessful, it causes the bad device to become unmirrored, and SQL Server prints error messages.

Deciding What to Mirror

When deciding to mirror a device, you must weigh such factors as the costs of system downtime, possible reduction or improvements in performance, and the cost of storage media. Such determinations will help you decide what to mirror¯just the transaction logs, all devices on a server, or selected devices.

Disk Mirroring Using Minimal Disk Space

Figure 9-3 illustrates the "minimum guaranteed configuration" for database recovery in case of hardware failure. The master device and a mirror of the user database transaction log are stored in separate partitions on one physical disk. The other disk stores the user database and its transaction log in two separate partitions.

In this configuration, if the disk with the user database fails, you can restore the user database on a new disk from the backups and the mirrored transaction log. If the disk with the master device fails, you can restore the master device from a database dump of the master database and re-mirror the user database's transaction log.

Figure 9-3: Disk mirroring using minimal physical disk space

This configuration minimizes the amount of disk storage required. It provides for full recovery, even if the disk storing the user database and transaction log is damaged, because the mirror of the transaction log ensures full recovery. However, this configuration does not provide non-stop recovery because the master and user databases are not being mirrored¯they must be recovered from backups.

Disk Mirroring for Non-Stop Recovery

Figure 9-4 represents another mirror configuration. In this case, the master device, user databases, and the transaction log are all stored on different partitions of the same physical device and are all mirrored to different partitions on a second physical device.

The configuration in Figure 9-4 provides non-stop recovery from hardware failure. Working copies of the master and user databases and log on the primary disk are all being mirrored, and failure of either disk will not interrupt SQL Server users.

Figure 9-4: Disk Mirroring for Rapid Recovery

With this configuration, all data is written twice, once to the primary disk, and once to the mirror. Applications that involve many writes may be that slower with disk mirroring than without mirroring.

Disk Mirroring for High Performance and Non-Stop Recovery

Figure 9-5 illustrates another configuration with a high level of redundancy. In this configuration, all three database devices are mirrored, but the configuration uses four disks instead of two. This configuration speeds performance during write transactions because the database transaction log is stored on a different device from the user databases, and the system can access both with less disk head travel.

Figure 9-5: Disk mirroring: keeping transaction logs on a separate disk

These three illustrations show different solutions to weighing the cost and performance trade-offs:

Cluster Fault Tolerant Companion Server

Sybase also supports a Companion Server version that provides fault tolerance in VAX OpenVMS clustered environments. The Companion Server version is configured on a node other than the one running the primary SQL Server, and remains active but not running¯that is, it can be used to run other applications.

If a media failure occurs on the CPU running the primary SQL Server, the CFT version automatically connects to all applications, runs the recovery mechanisms, and then begins normal operations.

Creating and Using Segments

Segments are named subsets of the database devices available to a particular SQL Server database. Conceptually, a segment is a label that points to one or more database devices. Segment names can be used in create table and create index commands to place the table and index objects on specific database devices. The use of segments can increase SQL Server performance, and can give the System Administrator or Database Owner increased control over placement and space usage of specific database objects. For example:

Segments are created within a particular database from the database devices already allocated to that database. Each SQL Server database can contain up to 31 segments. The database devices must first be initialized with disk init, and then be made available to the database with a create database or alter database statement before you can assign segment names.

When you first create a database, SQL Server creates three segments in the database:

Table 9-2 lists the SQL Server system procedures and commands for using segments.

Table 9-2: Commands for using segments

Command Action
sp_addsegment Defines a segment in a database.
create table create index Creates database objects on segments.
sp_dropsegment Removes a segment from a table.
sp_extendsegment Adds additional devices to an existing segment.
sp_helpsegment Displays segment allocation for a database or a particular segment.
sp_helpdb Displays the segments on each database device.

Getting Information About Storage

To find the name(s) of the database device(s) on which a particular database resides, use the system procedure sp_helpdb with the database name. For example:

sp_helpdb styledb

name  db_size     owner  dbid   created       status

----- ----------- ------ ------ ------------ --------------------

style 12.0 MB     sa          5 Nov 20, 1993 select into/bulkcopy

 

device_fragments  size    usage          free kbytes

----------------  ------  -------------- -----------

userdb_dev        8.0 Mb  data only             2320

userdb_dev        2.0 Mb  data only              352

log_dev           2.0 Mb  log only               992

To get a summary of the amount of storage space used by a database, execute the system procedure sp_spaceused:

sp_spaceused 

database_name  database_size 

-------------- --------------- 

styledb        12.0 MB 

 

reserved     data         index_size   unused 

------------ ------------ ------------ ---------

6100 KB      1992 KB      3210 KB      898 KB

Using Thresholds in Space Management

Thresholds monitor how much free space remains on a particular segment.

Some tasks that thresholds and threshold-activated procedures can perform are:

SQL Server tracks space on each segment of each database as space is allocated and freed. A Database Owner can create free-space thresholds on any segment to ensure that database space shortages are detected before problems arise. When you create a threshold, you specify the name of a stored procedure to execute when the space available in the database falls below the specified size.

Figure 9-6 shows a database segment with space remaining before the threshold. When user activity causes the space available in the database to decrease to the point where the threshold is reached, the threshold manager executes the stored procedure that is linked to that threshold.

Figure 9-6: Segment with a threshold in place

The Last-Chance Threshold on the Log Segment

When you create a database so that its log is stored on a separate segment, SQL Server automatically creates a "last-chance threshold" on the log segment. The last-chance threshold protects the transaction log from the possibility that there will be no space on which to store the backup log records. The last-chance threshold guarantees that there will always be room to perform the log dump.

If a transaction log does fill to the last-chance threshold, user processes must be suspended or aborted. A System Administrator configures the threshold and controls the possibility of filling to the last-chance threshold. If you wish to avoid this situation, the System Administrator or Database Owner can create an additional threshold on the log segment, with a greater free-space value. This threshold calls a stored procedure that performs the transaction log backup. At the end of a transaction log backup, inactive transactions in the log are removed to reclaim space for new log records.

Figure 9-7 shows a last-chance threshold and a user-defined threshold on a transaction log segment. When the log fills to the user-defined threshold, SQL Server executes the stored procedure associated with that threshold and dumps the transaction log. The lower part of Figure 9-7 shows the space reclaimed after the dump completes.

Figure 9-7: Thresholds on the transaction log

Using Thresholds

Table lists the system procedures that manage thresholds on SQL Server.

Table 9-3: System procedures for managing thresholds

Procedure Name Task
sp_addthreshold Creates a threshold in a specified database, on a specified segment. Also provides the threshold size and the name of the procedure to execute when the threshold is crossed.
sp_modifythreshold Changes the procedure name, number of pages, or segment name for an existing threshold.
sp_dropthreshold Drops a threshold from the database.

When the threshold manager executes a procedure it passes these four arguments to the procedure:

These parameters and Transact-SQL's control of flow language allow you to create a single threshold procedure to handle many threshold events.

Enhancing Performance Using Segments

In a large multidatabase and/or multidrive SQL Server environment, the allocation of space to databases and the placement of database objects on physical devices can enhance system performance. For greatest efficiency, a System Administrator can allocate space so that no heavily used database needs to share a physical disk with another database.

Generally, placing a table on one physical device, its non-clustered index on a second physical device, and the transaction log on a third physical device can enhance performance. Placement of database objects on specific physical devices is managed through the use of segments.

Placing objects on segments may improve performance because default placement of objects can split tables and indexes across devices. Performance can be improved for very large databases and other high-volume multiuser applications when large tables are split across segments that are located on separate disk controllers

Table Partitioning

Distributing a heavily used table over several devices dramatically improves insert performance by reducing the chances for concurrent inserts to block. SQL Server 11 now lets you distribute (or partition) tables across disks. Partitioning improves system performance by increasing the rate at which data passes to cache by facilitating parallel loading. Instead of passing only one stream of data to one disk, the system can simultaneously pass several streams to several disks for the same table.

By default, SQL Server stores a heap table's data in one doubly linked chain of database pages. For example, an unpartitioned table that has no clustered index stores all data in a single "heap" of pages. When a transaction inserts a row into the table, it holds an exclusive page lock on the last page of the page chain while inserting the row. As multiple transactions attempt to insert rows into the same table at the same time, performance can suffer. Because only one transaction at a time can obtain an exclusive lock on the last page, other concurrent insert transactions block, as shown in Figure 9-8.

Figure 9-8: Page contention during inserts

SQL Server 11 can partition heap tables. (Partition is another term for a page chain.) Partitioning a table creates multiple page chains (partitions) for the table and, therefore, multiple last pages for insert operations.

When a transaction inserts data into a partitioned table, SQL Server randomly assigns the transaction to one of the table's partitions. Concurrent inserts are less likely to block, since multiple last pages are available for inserts, as shown in Figure 9-9.

Figure 9-9: Addressing page contention with partitions

If a table's segment spans several physical disks, SQL Server distributes the table's partitions across those disks when you create the partitions. This can improve I/O performance when SQL Server writes the table's cached data to disk because the I/O is distributed over several devices.

SQL Server manages partitioned tables transparently to users and applications. Partitioned tables appear to be exactly the same as unpartitioned tables, except when accessed via the dbcc checktable and dbcc checkdb commands or when viewed with the new sp_helpartition procedure. the sp_help and sp_helpindex procedures report on whther the table or index is partitioned.



[Top] [Prev] [Next] [Bottom]