SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds


FAQ Topics

All FAQ's
General DBA
General Developer
DBA Performance Tuning
Developer Performance Tuning
Clustering
Error Messages

USEFUL SITES :

ASP.NET Tutorials
Windows and SQL Azure Tutorials
Cloud Hosting Magazine
SharePoint Tutorials
Windows Server Help

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

WebMatrix Tutorial - Getting Started
Working with IIS using PowerShell
Know your Data with Data Profiling
Overview of SQL Server 2008 R2 Express Edition

More     
 
Latest FAQ's

SQL Agent job getting suspended.
Queries which include DMFs return a syntax error ...
Could not find stored procedure 'dbo.sp_MSins_dboTest'
How to change server name when replication is enabled.

More     
   
Latest Software Reviews

dbForge Review
Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008

More     

Setting Up Data And Log Files For SQL Server



When a database is created in SQL Server it creates two files by default: one is the data file with an extension of .mdf that stores the actual data, and the other is the log file with an extension of .ldf that stores the transaction log information used to recover data in disaster recovery scenarios. The mdf is the primary data file; in case of very large databases it might be useful to have secondary data files with an extension of .ndf in which case  the data will be stored in both mdf as well as ndf files.

The placement of mdf, ndf and ldf files on a particular drive is a matter of decision based on the size requirements of each file, and more importantly on the impact it will have on the overall performance of the database. As the log file is write-intensive it should be placed on a separate drive and the data files also  placed on separate drives of their own. Even secondary data files should be placed on a separate drives to optimize performance. Why? This is because, it increases parallelism while accessing the data and log files for read/write operations.

The log file is write-intensive, and the data file (depending on your application) is read/write intensive. Suppose you have multiple users simultaneously updating and requesting data. For data retrieval, the log file is not needed, and so the requests for data can be satisfied from the data file placed on its own drive. This would involve the movement of only the spindle of that drive on which the data file is kept. For data updates, the log file is heavily used, and so any write operations would involve the movement of the spindle of the drive on which the log file is placed. Now, if both log and data files are placed on the same drive, the amount of spindle movement would be heavy so as to satisfy both read and write operations. But, if you place the data and log files on separate disk drives, then the spindle movement for write operations won’t interfere with that of the read operations, since each drive’s spindle would be moving independently to satisfy read as well as write requests, thus improving the performance of data retrieval and data updates.

 

Ideally, the separation of data and log files on drives should be done while creating the database, but this can be done later also after the database is created and is in use. SQL Server is very flexible in that. The way to go about doing this is to use the two statements that SQL Server provides: CREATE DATABASE   and   ALTER DATABASE. Let’s look at both examples where database_name is the name of the database being created and/or altered, and os_path is the operating system file path.

 

While creating a database, you use the Create Database statement in the following manner:

CREATE DATABASE database_name ON

(NAME=Database_name_data,

 FILENAME = ‘os_path\database_name_data.mdf’),

(NAME=Database_name_log,

 FILENAME = ‘os_path\database_name_data.ldf’)

If you want to add secondary data files, the statement would be:

CREATE DATABASE database_name ON

(NAME=Database_name_data,

 FILENAME = ‘os_path\database_name_data.mdf’),

(NAME=Database_name_data,

 FILENAME = ‘os_path\database_name_data.ndf’),

 (NAME=Database_name_log,

 FILENAME = ‘os_path\database_name_data.ldf’)

 

If you already have a database and want to change the drive location of either the data or log file, then use the Alter Database statement in the following manner:

 

First, bring the database to an offline status by giving the following command:

ALTER DATABASE database_name SET OFFLINE

 

Next, change the location of either the data file or the log file with the following command for each file:

ALTER DATABASE database_name MODIFY FILE ( NAME = database_name_data, FILENAME = 'new_path\database_name_data.mdf' )
ALTER DATABASE database_name MODIFY FILE ( NAME = database_name_data, FILENAME = 'new_path\database_name_data.ndf' )
ALTER DATABASE database_name MODIFY FILE ( NAME = database_name_data, FILENAME = 'new_path\database_name_data.ldf' )

 

Finally,  bring the database back online by issuing the following command(but before giving this command make sure you copy the data and log files to their new locations):

ALTER DATABASE database_name SET ONLINE

 

After this you should verify the changed locations of the files by querying the sys.master_files catalog view:

 

SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'database_name');

 

The above procedure is valid for both SQL Server 2008 and 2005.

                                                                                                       

Submitted by Amit Chaudhary

Ask A Question In the Forums











C# Help and Tutorials | PHP MySQL Tutorial | Sharepoint Tutorial | Azure Tutorial | Cloud Hosting Magazine | ASP.NET Tutorials | Derivatives | Windows Server Help | Windows Phone Pro | Silverlight Ace | Visual Studio Tutorials | Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Sonasoft | Andy Khanna | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 2010 Jude O'Kelly. All rights reserved