• subscribe
January 24, 2007 12:00 AM

Essential Aspects of Database Design

Important considerations for indexing, maintenance, and statistics
SQL Server Pro
InstantDoc ID #94585

Maintenance
When data changes, we need to perform maintenance. Maintenance helps keep your tables contiguous and your pages denser; it also helps improve resource utilization and therefore improves overall performance. But how do you maintain your tables, and what does it really mean to defragment a table? This topic is fairly complex, but here are a few things to think about and review.

In SQL Server 2005, you can use the sys.dm_db_index_physical_stats dynamic management function to determine how fragmented your table is. Use this function to get a feel for how much and how fast fragmentation is occurring. Also, consider rebuilding your table if you have 25 percent or greater fragmentation, you have a dedicated maintenance window and ample disk and log space, and you can rebuild online.

Consider defragmenting your table if you can't rebuild online, you don't have more than 25 percent fragmentation, and you can't afford the disk or log space to rebuild.

Statistics
Because statistics are the optimizer's source for estimating how much data will be processed, they're crucial for better performance. Statistics are used with indexes, but standalone statistics (statistics that are created in addition to indexes) can also help to improve a variety of optimization strategies as well as improve the usefulness of a composite nonclustered index. This is also complex, but here are a few things to think about and do: First, make sure that the database option "auto create statistics" is on. (It's on by default.) Second, make sure that the database option "auto update statistics" is on. (It's on by default.) Third, consider using the sp_createstats stored procedure to create additional statistics on the secondary columns of your nonclustered indexes. Use the following command:

sp_createstats ‘indexonly',
  ‘fullscan'

Finally, realize that certain correlations between columns can't be perfectly described by statistics alone. Sometimes the only way to answer a query is with the right index.

Results Match Effort
If you design with performance in mind, the rewards to your business can be huge. With careful thought and attention to indexing, maintenance, and statistics, as well as database design, you'll be the hero who designed for performance—and achieved it.

Related Reading

SQL SERVER MAGAZINE RESOURCES
To learn more about database design:
"brush Up on Design Essentials with the SQL Diagrammer," InstantDoc ID 49263
"Database Design for Performance," InstantDoc ID 93633
"Performance-tuning the Data Model: Configuring the Database," InstantDoc ID 46377

To learn more about indexing:
"Indexing Dos and Don'ts," InstantDoc ID 27334

MICROSOFT RESOURCES
"Microsoft SQL Server 2000 Index Defragmentation best Practices," http://www.microsoft.com/technet/prodtechnol/sql/2000/ maintain/ss2kidbp.mspx
"Statistics Used by the Query Optimizer in Microsoft SQL Server 2005," http://www.microsoft.com/technet/prodtechnol/sql/2005/qrystats.mspx
"Statistics Used by the Query Optimizer in Microsoft SQL Server 2000," http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsql2k/html/statquery.asp



ARTICLE TOOLS

Comments
  • Marcos
    3 years ago
    Jun 19, 2009

    Wonderful article, I just download the MSDN webcast from Kinberly and I must say: I am impressive with your capacity to translate very complex features in terms everybody can understand and follow. Thanks a lot, and please, continue writing to SQL Magazine, I saw that your last article was 2 years ago.

    Take Care!

  • Barry
    5 years ago
    Feb 12, 2007

    I enjoyed this article a lot...however, I have 2 comments:
    1. It is my understanding that several narrower non-clustered indexes are actually a better approach in many cases than composite indexes since SQL Server 2000 and higher takes advantage of index intersection.

    2. I agree with updating stats but I think a better idea than leaving auto-update stats on is to turn it off and then update stats on a nightly or even weekly schedule instead. This way, you don't have to worry about a slow-down due to SQL Server deciding to update stats in the middle of a busy day.

    Thoughts?

  • Chris
    5 years ago
    Feb 09, 2007

    Even understandable to those of us without decades of experience (yet). Thank you.

  • Diana
    5 years ago
    Jan 26, 2007

    Great to see an article like K. Tripps' Essential Aspects of DB Design. I've added it to the material I share with our developers who end up designing databases without having much background. In the SQL Server world it's all too common that databases are being designed by folks whose main expertise lies elsewhere; SQL Server Magazine can do a lot to mitigate this problem.


    You mentioned fragmentation. Just today I was looking at some very high fragmentation numbers. The thing is, the data and index files are stored on a dedicated Equalogics iSCSI disk array. These aren't just iSCSI RAID 10 arrays. One reason for the high price is the device self balances I/O across the spindles as it runs. If it notices a trend that spindle 6 is overused and spindle 3 is underused, it will start migrating blocks from 6 to 3 to balance the load.


    So, does fragmentation have any meaning on this kind of a device? The analysis tools will think of that volume as one ordinary disk with cylinders and tracks and so forth - but it's a fakeout. SQL Server is writing to the devices interfaces, which takes full responsibility for maintaining the write order and the atomicity of the write of blocks that are part of a single transaction - even if the power goes out. The real blocks may be all over the place.

    Clearly the one thing it won't do is compact down multiple pages that can fit into one. With 2 TB of disk and 64 G memory, a cache hit rate of 99.8%, and disk queues under .2/sec despite 59 databases used by over 100 apps with nearly 3000 users I'm not sure compacting it has much point.

    Obviously, I'd enjoy a few less articles on how to get by with RAID 5 and some more real life stuff with higher end devices.

    ---------------
    all the best
    Roger Reid

You must log on before posting a comment.

Are you a new visitor? Register Here