Real World DBA

Making Life Easier for SQL Server DBAs

Archive for January, 2008

SQL UPSERT

Posted by mxhxrdba on January 31, 2008

What is an UPSERT?

An UPSERT is a technique to perform inserts/updates in a database using the least amount of database reads. For maximum performance, you want to absolutely minimize the number of reads required of the database.

An UPSERT is really just an UPDATE combined with an INSERT. The classic example, is if you are writing a stored proc to look for a row and update it; if it does not exist, then insert. Most programmers will use an IF, EXISTS syntax to accomplish this. This technique will perform two I/O operations regardless of the existence of the row: one to see if the row exists, and a second to either update or insert. A basic UPSERT syntax is:
———————————–
UPDATE dbo.tObject
SET objectStatusCode = ‘Active’
WHERE objectNumber = 100

IF @@ROWCOUNT = 0

INSERT INTO dbo.tObject
(objectNumber,
objectName)
VALUES
(100,
‘Test Object’)
GO
–You can run the following before and after the statement to view I/O stats:
SET STATISTICS IO ON
–Statement here
SET STATISTICS IO OFF

———————————–

If an INSERT is required, then we will have 2 I/O operations, but if only an UPDATE is required, it would only be 1 I/O operation. The alternative with an IF, EXISTS syntax is to always have 2 I/O’s.

Posted in Performance | Tagged: , , , , , | 1 Comment »

SQL Server 2008 Launch Events

Posted by mxhxrdba on January 30, 2008

I encourage you to sign up for the SQL Server 2008 (along with VS 2008 and Windows Server 2008) launch event; there’s more than likely one in your area. From past experiences, it’s a great way to demo new software, mingle with some of your peers, speak (gripe & moan) directly to people from Microsoft about any issues, and get some free schwag including promo copies of SQL, VS, and Windows Server 2008.

One of the highlights of these events are the informal Q&A sessions, where you can speak one-on-one with a product expert about a specific problem that you may be experiencing in your shop; they are typically very knowledgeable and have machines on hand where they can show you how to potentially leverage Microsoft technologies to address your issues.

http://www.microsoft.com/heroeshappenhere/register/default.mspx

Posted in 2008, Events | Tagged: , , , , , , , , , , , | Leave a Comment »

Indexes – to Rebuild or Reorganize?

Posted by mxhxrdba on January 27, 2008

Index maintenance is one of the best “bang for the buck” performance tuning activities a DBA can perform. Due to factors such as page splits and updates to index and branch leaf pages, index become fragmented, resulting the in index data no longer being physically contiguous. Looking at our available options as far as index maintenance goes, there may be some confusion as whether we should be rebuilding our indexes, or performing an index reorganization. It really depends on their degree of fragmentation.

Microsoft recommends rebuilding if fragmentation is over 30%, reorganizing if it’s between 5% and 30% and leaving it alone if fragmentation is below 5%. In SQL Server 2005, you can use the sys.dm_db_index_physical_stats function to view the fragmentation of an index.

This will get detail for all tables in your DB, returning the three key fields first and ordering by most fragmented:

—————————————————————————————-

SELECT OBJECT_NAME(i.object_id) AS [Table Name],
i.name AS [Index Name],
dm.avg_fragmentation_in_percent, *
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, ‘DETAILED’) dm
INNER JOIN sys.indexes i ON i.object_id = dm.object_id
AND i.index_id = dm.index_id
order by avg_fragmentation_in_percent desc

—————————————————————————————-

To reorganize: ALTER INDEX REORGANIZE –reorgs and repacks the leaf pages of the index; this makes DBCC INDEX DEFRAG obsolete.

To rebuild: ALTER INDEX REBUILD WITH (ONLINE = ON) –drops the index and recreates it; this makes
DBCC DBREINDEX and DBCC INDEX REBUILD obsolete.

Rebuild syntax: ALTER INDEX PKtTable1
ON dbo.tTable1
REBUILD
GO

The (ONLINE = ON) operator allows the index to be rebuilt while the underlying data is accessed by other users (new to SQL 2005).

The following script will rebuild indexes for all tables in your database:

—————————————————————————————-

DECLARE @Database VARCHAR(255)
DECLARE @Table VARCHAR(255)
DECLARE @cmd NVARCHAR(500)
DECLARE @fillfactor INT

SET @fillfactor = 90

DECLARE DatabaseCursor CURSOR FOR
SELECT name FROM master.dbo.sysdatabases
WHERE name = ‘YourDatabaseName’
ORDER BY 1

OPEN DatabaseCursor

FETCH NEXT FROM DatabaseCursor INTO @Database
WHILE @@FETCH_STATUS = 0
BEGIN

SET @cmd = ‘DECLARE TableCursor CURSOR FOR SELECT table_catalog + ”.” + table_schema + ”.” + table_name as tableName
FROM ‘ + @Database + ‘.INFORMATION_SCHEMA.TABLES WHERE table_type = ”BASE TABLE”’

– create table cursor
EXEC (@cmd)
OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @Table
WHILE @@FETCH_STATUS = 0
BEGIN

– SQL 2000 command
–DBCC DBREINDEX(@Table,’ ‘,@fillfactor)

– SQL 2005 command
SET @cmd = ‘ALTER INDEX ALL ON ‘ + @Table + ‘ REBUILD WITH (FILLFACTOR = ‘ + CONVERT(VARCHAR(3),@fillfactor) + ‘)’
EXEC (@cmd)

FETCH NEXT FROM TableCursor INTO @Table
END

CLOSE TableCursor
DEALLOCATE TableCursor

FETCH NEXT FROM DatabaseCursor INTO @Database
END
CLOSE DatabaseCursor
DEALLOCATE DatabaseCursor

—————————————————————————————-

Now, you may notice that even after rebuilding your indexes, many still show a large percentage of fragmentation. Hint: check out the page_count column of sys.dm_db_index_physical_stats. If it is a small number, SQL Server may have just skipped over this index. This is fine, as fragmentation in small tables/indexes has a minimal hindrance on performance.

Posted in Administration, Indexing, Performance | Tagged: , , , , , , , , , , , , , , , | 2 Comments »

SQL Server 2008 RTM delayed

Posted by mxhxrdba on January 27, 2008

Looks like SQL Server 2008 RTM (release-to-manufacturing) won’t be released at during the 2nd quarter of this year, and will instead be pushed to 3rd quarter 2008. But to Microsoft’s credit, it still falls within their prediction of a release that is “2 to 3 years after SQL Server 2005″.

katmai

Posted in 2008 | Leave a Comment »

Autogrowth values keep changing – 3200%, 6400%

Posted by mxhxrdba on January 27, 2008

We ran across this (potentially serious) problem in a handful of databases in our preproduction environment: The autogrowth values for log and data files kept changing to 3200% or 6400%. Imagine if you have a 50GB datafile, and it attempts to expand by 3200%. SQL Server would time out while trying to allocate that space, and likely fill all available space on the hard disk, bringing down the system. The change in autogrowth values usually manifest themselves after restarting SQL Server.We have not seen the issue since upgrading to SP2.KB Artice: 919611

Posted in Administration | Leave a Comment »