30.06.2013 Views

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

341<br />

Chapter 11: <strong>SQL</strong> Refactoring<br />

Well, not so fast. Choosing the correct data types requires a very intimate knowledge<br />

of all processes that will use a database, along with the ability to project what kind of<br />

data you might have to handle in the future. For example, in one project, we created a<br />

clustered index key as a smallint, which is 2 bytes in size and can store numbers up to<br />

32,767. It seemed perfectly adequate for our needs. Two years later, however, and we had<br />

crossed that upper boundary, and needed to convert the key to an int type (4 bytes). On<br />

the surface this seems like quite a small change, but it requires a lot of internal, low-level<br />

changes to how the data is stored<br />

A deeper discussion of storage internals is outside the scope of this chapter, so just<br />

keep in mind that, while minimizing the size of your data types will bring performance<br />

benefits, as the following example will demonstrate, you do need to take some account<br />

of future growth.<br />

Let's take a look at a simple example that demonstrates just how much you can improve<br />

performance by choosing the right data types. Listing 11-16 creates two tables, one that<br />

uses large data types (bigint and nvarchar), and one that uses smaller types (int and<br />

varchar), more appropriate to the data being stored. The identical 10,000 rows of test<br />

data are inserted in each case.<br />

CREATE TABLE #LargeDataTypes<br />

(<br />

ID BIGINT IDENTITY(1, 1) , – – do we really need bigint?<br />

TextColumn NVARCHAR(200) – – uses 2 bytes per char<br />

)<br />

GO<br />

- – insert some test data<br />

INSERT INTO #LargeDataTypes<br />

( TextColumn<br />

)<br />

SELECT TOP 10000<br />

– – create a sequence like<br />

– – aaaaa..1, ..., aaaaa..9999, aaaaa..10000<br />

REPLICATE('a', 190)<br />

+ CONVERT(NVARCHAR(5), ROW_NUMBER()<br />

OVER ( ORDER BY t1.number ))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!