 |
 |
| SQL Server Tips: |
|
 |
 |

DATABASE ADMINISTRATION
Optimize T-SQL data types in SQL Server
Matthew Schroeder, Contributor 12.03.2007
Rating: -4.42- (out of 5)




|
Many SQL Server developers do not give data types a lot of thought when they design database schemas. Initially it doesn't seem to matter much and the performance looks pretty good, but, over time, the system slows down. Let's take a look at the various SQL Server data types, the effects they have on performance, and what you should consider when choosing a data type.
Optimizing data types means choosing them carefully, as they have a huge impact on I/O, CPU and RAM consumption. A larger data type takes up more room in the cache (RAM) resulting in a smaller hit ratio on the cache, which causes more fetches from disk (more I/O). This results in a heavier CPU hit, longer update times, increased lock times and increased contention on the table, which will show up as blocks. Indexes defined on larger data types take longer to scan/seek because more pages have to be read, which will impact RAM, CPU and I/O. Larger data types also increase maintenance times on the various indexes, yielding longer "maintenance windows."
Primary key considerations
Clustered indexes default to the primary key fields in SQL Server. That said, several factors are important with data types. For this discussion, we will assume that your primary key is the same as the clustered index, since that is the preferred approach.
Any data type you pick for the primary key (usually also the clustered index) is included within every non-clustered index as a reference back to the original row. For example, if you pick the data type datetime as the primary key rather than an int type, any non-clustered indexes will be an ad
To continue reading for free, register below or login
To read more you must become a member of SearchSQLServer.com
');
// -->

ditional 4 bytes larger per row because the non-clustered index has to hold the primary key value. In tables with more than two billion rows, this can quickly add more than 2.4 GB per non-clustered index. You'll see the impact on CPU, I/O and RAM quickly accumulate in this scenario.
Smaller data types make it easier -- i.e., faster and less resource intensive -- for SQL Server to ensure that any inserted or updated values are unique against the existing table values.
Individual data type considerations
We will just cover the commonly misused data types here.
The size of the data rows and indexes impacts a wide range of hardware resources. SQL Server performance will increase exponentially as you reduce the size of the rows and indexes. A few fast points to remember: Always pick the smallest data type, use bits and integers (the smaller the int type the better) with lookup tables where possible – trying to avoid the use of (n)varchars.
If you have to use large text/binary values, then consider defining them as (Max) types and setting the options to store the data "out of row." If the text data is fixed length, then always use (n)char(n) data types so 2 bytes is not wasted storing offsets. If your primary concern is performance, avoid using any data types other than ints for primary/foreign keys, as this will significantly reduce your data row and index sizes. SQL Server data types seem like a small issue at design time, but they will have a huge impact on your system as it grows organically over time.
[TABLE]
 |

|
|
 |
|
 |
 |
 |
 |
| TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of . |
|
| |
All Rights Reserved, , TechTarget |
|
|
|
|
|