Tips and Techniques

All great administrators know a few tricks to help manage databases more efficiently and to keep things running smoothly. Here are a few tips to help you with database administration.

Copying and Moving Databases

All databases except model, msdb, and master can be copied or moved using the Copy Database Wizard. You can also use this wizard to create a copy of a database, to copy or move databases between different instances of Microsoft SQL Server, and to upgrade databases from SQL Server 2000 to SQL Server 2005. The Copy Database Wizard uses one of two techniques for copy and move operations:

  • Detach and Attach. This method is the fastest way to copy a database, but it requires the source database to be offline so it can be detached and copied/moved. The database is then reattached when the copy/move operation is complete. To use this technique, you must be a member of the sysadmin fixed server role on both the source and destination servers. Also, you should place the database in single-user mode before starting the copy operation to ensure that there are no active sessions. If there are active sessions, the Copy Database Wizard will not execute the move or copy operation.

  • SQL Management ObjectThis method is slower, but it does not require the source database to be offline. To use this technique, you must be a database owner for the source database and you must have the CREATE DATABASE permission or be a member of the dbcreator fixed server role on the destination database. You do not have to place the database in single-user mode prior to starting the copy/move operation. Active connections are allowed during the operation because the database is never taken offline.

Note

Note

The copy/move operation preserves full-text catalogs if both the source and destination servers are SQL Server 2005 servers. However, if the source server is a SQL Server 2000 server, the full-text catalogs must be rebuilt and fully populated again after the copy/move is completed.

When you move databases between different servers or disk drives, the Copy Database Wizard copies the database to the destination server and verifies that it is online. When you move databases between two instances on the same server, a file system move operation is performed. If you elect to move a database, the Copy Database Wizard deletes the source database automatically after the move is completed. However, the Copy Database Wizard does not delete the source database when you perform a copy operation.

You can copy or move a database by completing the following steps:

  1. In SQL Server Management Studio, right-click a database in Object Explorer view, point to Tasks, and then select Copy Database.

  2. When the Copy Database Wizard starts, click Next.

  3. On the Select A Source Server page, specify the server that has the database you want to copy or move. Type the DNS or host name of the source server, such as CORPSVR09 (see Figure 7-7). Alternately, you can click the button to the right of the Source Server box to browse for available source servers.

    The Select A Source Server page of the Copy Database Wizard

    Figure 7-7. The Select A Source Server page of the Copy Database Wizard

  4. Windows Authentication is used by default, which means that your current login credentials are used to determine if you have appropriate permissions. If you want to use SQL Server Authentication, select SQL Server Authentication, and then enter your SQL Server login and password in the text boxes provided. Click Next.

  5. On the Select A Destination Server page, specify the server to which you are copying or moving the selected database, and then specify the authentication technique to use. Click Next.

    Note

    Note

    SQL Server Agent must be running on the destination server.

  6. Select the transfer method—either Use The Detach And Attach Method or Use The SQL Management Object method. If you choose to detach and attach the database, the source database is reattached automatically by default if failure occurs. To prevent this, clear the check box for the option If A Failure Occurs, Reattach The Database. Click Next.

  7. As shown in Figure 7-8, you can now select the database you want to copy or move. Click Next.

    The Select Databases page of the Copy Database Wizard

    Figure 7-8. The Select Databases page of the Copy Database Wizard

  8. Use the Configure Destination Database page shown in Figure 7-9 to define the destination configuration of each database you are copying or moving, one at a time. Pay particular attention to the Source Database and Destination Database boxes. The Source Database box shows the current name of the database on the source. Use the Destination Database box to set the name that will be used on the destination server.

    The Configure Destination page of the Copy Database Wizard

    Figure 7-9. The Configure Destination page of the Copy Database Wizard

  9. Any data and log files associated with the database are shown with their destination file name and folder. You can change the default locations by typing new values. If you are creating a copy of a database on the same source and destination instance, be sure to change the database name and file names.

  10. If the destination database already exists, the default option is to stop the transfer. You can drop the existing database and force the transfer by selecting the Drop Any Database... option.

  11. Click Next. If you are copying or moving multiple databases, you will see a Configure Destination Database page for each database.

  12. When you have configured all destination databases, the next page you will see is the Configure The Package page. Set the package name and logging options you prefer, and then click Next.

  13. You can run the wizard now or schedule the wizard to run at a later time. To run the wizard immediately and perform the copy/move operations, select Run Immediately. To schedule the wizard to run at a later time, select Schedule, and then click Change. You will then be able to schedule this task as a new job. See Chapter 15, for details on scheduling.

  14. Click Next. Review your choices, and then click Finish. The wizard will perform the necessary tasks to prepare and create the copy/move package. If a critical error occurs during these tasks, the operation will fail, and you should view the report to determine what error occurred and then resolve it.

Moving Databases

You can move any system or user-defined database files except for Resource database files using the ALTER DATABASE statement. To move files, you specify the current logical name of the file and the new file path, which includes the new file name. You can move only one file at a time in this manner.

To move data or log files to a new location, follow these steps:

  1. Get the logical name of the data and log files associated with the database by typing:

    USE master
    
    SELECT name, physical_name
    
    FROM sys.master_files
    
    WHERE database_id = DB_ID("Personnel");
  2. Take the database you want to work with offline by typing:

    ALTER DATABASE Personnel
    
    SET offline
    
    GO
  3. Move one file at a time to the new location by typing:

    ALTER DATABASE Personnel
    
    MODIFY FILE ( NAME = Personnel_Data, FILENAME =
    "C:DataPersonnel_Data.mdf")
    
    GO
  4. Repeat the previous step to move other data and log files.

  5. Put the database online by typing:

    ALTER DATABASE Personnel
    
    SET online
    
    GO

You can verify the change(s) by typing:

USE master

SELECT name, physical_name

FROM sys.master_files

WHERE database_id = DB_ID("Personnel");

You can move full-text catalogs by their logical name as well. However, when specifying the new catalog location, you only specify the new_path rather than new_path/file_name. To move a full-text catalog file to a new location, follow these steps:

  1. Take the database you want to work with offline by typing:

    ALTER DATABASE database_name
    
    SET offline
    
    GO
  2. Move one file at a time to the new location by typing:

    ALTER DATABASE database_name
    
    MODIFY FILE ( NAME = logical_name, FILENAME = "new_path".
    
    GO
  3. Repeat the previous step to move other full-text catalog files as necessary.

  4. Put the database online by typing:

    ALTER DATABASE database_name
    
    SET online
    
    GO

Moving and Resizing tempdb

The tempdb database contains temporary tables created by users or by SQL Server, or both. SQL Server 2005 does not store complete transactions for temporary tables in tempdb. With temporary tables, SQL Server 2005 stores only enough information to roll back a transaction and not enough to redo a transaction.

The tempdb database is created each time you start the SQL Server service, which ensures that the database starts clean. As with other databases, the default structure of tempdb is based on the model database. This means that each time you start SQL Server, a snapshot is taken of the current model database and applied to tempdb.

By default, the tempdb primary data file has a size of 8 MB and is set to automatically grow the database by 10 percent when necessary. On a busy server, this 8 MB can fill up quickly, and as a result the server may need to frequently expand tempdb. Unfortunately, while tempdb expands, SQL Server locks the database. This can slow down queries and make the server seem unresponsive. Following are some ways you can improve the performance of tempdb:

  • Permanently expand tempdb to accommodate space needs during busy periods. To do this, follow the steps described in the earlier section of this chapter titled "Expanding Databases and Logs Manually." Even if the model database is smaller, tempdb will retain this new size.

  • By default, tempdb is stored in the same location as other data. To resolve any performance issues, you can create a secondary data file for tempdb and put this file on its own drive. Or you can move tempdb and all its associated files to a new location.

You can move individual tempdb files or all tempdb files by completing the following steps:

  1. Get the logical name of the data and log files associated with tempdb by typing:

    Use Master
    
    SELECT name, physical_name
    
    FROM sys.master_files
    
    WHERE database_id = DB_ID("tempdb");
    
    GO
  2. Move each data and log file to a new location one at a time by typing:

    USE master
    
    GO
    
    ALTER DATABASE tempdb 
    
    MODIFY FILE (NAME = logical_name, FILENAME = "new_path/file_name")
    
    GO
    
    ALTER DATABASE  tempdb 
    
    MODIFY FILE ( NAME = logical_name, FILENAME = "new_path/file_name")
    
    GO
  3. Repeat the previous step to move other data and log files as necessary.

  4. Stop and restart SQL Server.

You can verify the change(s) by typing:

USE master

SELECT name, physical_name

FROM sys.master_files

WHERE database_id = DB_ID("tempdb");

Creating Secondary Data and Log Files

Secondary data and log files can improve the performance of busy databases and can help make large databases easier to manage. You may want to create secondary files to distribute the load over several drives. For example, you could place the primary file on drive D, secondary files on drive E, and transaction logs on drive F. See the section titled "SQL Server 2005 and Your Hardware in Chapter 1, for more tips on drives and RAID arrays.

Another reason you may want to create secondary files is to make it easier to restore a large database. For example, if you have a 10-GB database in a single file, you can restore the database only on a 10-GB drive, which you may not have at 3:00 A.M. on a Sunday if a drive fails. Instead, create several smaller files for the database, such as five 2-GB files, and then you can restore these files to several smaller drives if necessary.

You can create secondary data or log files by completing the following steps:

  1. Start SQL Server Management Studio. In Object Explorer view, connect to the appropriate server, and then work your way down to the Databases folder.

  2. Right-click the database you want to manage, and then select Properties from the shortcut menu to open the Database Properties dialog box.

  3. Select the Files page from the Select A Page list in the Database Properties dialog box.

  4. On the Files page, click Add to set a secondary data file. Then, in the Database Files area, type a new file name, such as Personnel_Data2 or Personnel_Log2.

  5. Set the file type:

    • To create the new file as a data file, select Data under File Type.

    • To create the new file as a log file, select Log under File Type.

  6. Set the initial size of the file, and then click the button to the right of the Autogrowth box. Then you can set Autogrowth options for the new data or log file.

  7. Click the button to the right of the Path box to find a new path, or you can enter a new path directly. The file name is set based on the logical name and file type.

  8. Click OK to make the changes.

Preventing Transaction Log Errors

The transaction log is essential to the smooth running of SQL Server. If the log fills up or otherwise fails, SQL Server cannot process most types of queries. To ensure that the transaction log runs smoothly, you may want to use these techniques:

  • To reduce the load on the transaction log, use SQL commands that are not logged. This invalidates the transaction logs, as explained in Chapter 15.

  • To ensure that the log is cleaned out periodically, set the database Recovery Model to Simple. This invalidates the transaction logs, as explained in Chapter 15.

  • To prevent the log from running out of space, do not set a maximum file size, but do increase the frequency of the log backup and watch the amount of free drive space closely.

  • To make sure you can recover transactions, increase the permanent size of the log and increase the frequency of the log backup.

Preventing a Filegroup Is Full Error

When you encounter a situation in which it is not possible to write to a data file, you will see a Filegroup Is Full error. This error usually occurs when the data file has reached its maximum size or you have run out of file space. To reduce the chances of this error reoccurring, you can use the following techniques:

  • Do not set a maximum file size.

  • Watch the amount of free drive space closely.

  • Schedule data files to be compacted periodically.

  • Remove unused tables, indexes, or objects.

Creating a New Database Template

The model database is used as the template for all new databases. If you modify the options and properties of the model database, any new databases created on the server will inherit these options and properties.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset