17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

occur, and that one or more non-leaf level page must also be modified to have the reference to the proper<br />

leaf page.<br />

Sometimes — quite often actually — not creating that extra index is the thing to do. Sometimes, the best<br />

thing to do is choose your indexes based on the transactions that are critical to your system and use the<br />

table in question. Does the code for the transaction have a WHERE clause in it? What column(s) does it<br />

use? Is there a sorting required?<br />

Choosing That Clustered Index<br />

Remember that you can have only one, so you need to choose it wisely.<br />

By default, your primary key is created with a clustered index. This is often the best place to have it, but<br />

not always (indeed, it can seriously hurt you in some situations), and if you leave things this way, you<br />

won’t be able to use a clustered index anywhere else. The point here is don’t just accept the default.<br />

Think about it when you are defining your primary key — do you really want it to be a clustered index?<br />

If you decide that you indeed want to change things, that is, you don’t want to declare things as being<br />

clustered, just add the NONCLUSTERED keyword when you create your table. For example:<br />

CREATE TABLE MyTableKeyExample<br />

(<br />

Column1 int IDENTITY<br />

PRIMARY KEY NONCLUSTERED,<br />

Column2 int<br />

)<br />

Chapter 9: <strong>SQL</strong> <strong>Server</strong> Storage and Index Structures<br />

Once the index is created, the only way to change it is to drop and rebuild it, so you want to get it set<br />

correctly up front.<br />

Keep in mind that if you change which column(s) your clustered index is on, <strong>SQL</strong> <strong>Server</strong> will need to do<br />

a complete resorting of your entire table (remember, for a clustered index, the table sort order and the<br />

index order are the same). Now, consider a table you have that is 5,000 characters wide and has a million<br />

rows in it — that is an awful lot of data that has to be reordered. Several questions should come to mind<br />

from this:<br />

❑ How long will it take? It could be a long time, and there really isn’t a good way to estimate that<br />

time.<br />

❑ Do I have enough space? Figure that in order to do a resort on a clustered index, you will, on<br />

average, need an additional 1.2 times (the working space plus the new index) the amount of<br />

space your table is already taking up. This can turn out to be a very significant amount of space<br />

if you’re dealing with a large table — make sure you have the room to do it in. All this activity<br />

will, by the way, happen in the database itself, so this will also be affected by how you have<br />

your maximum size and growth options set for your database.<br />

❑ Should I use the SORT_IN_TEMPDB option? If tempdb is on a separate physical array from your<br />

main database and it has enough room, then the answer is probably yes.<br />

285

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

Saved successfully!

Ooh no, something went wrong!