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.

We use a FILLFACTOR when we need to adjust the page densities. As already discussed, lower page densities<br />

(and therefore lower FILLFACTORs) are ideal for OLTP systems where there are a lot of insertions;<br />

this helps prevent page splits. Higher page densities are desirable with OLAP systems (fewer pages to<br />

read, but no real risk of page splitting due to few to no inserts).<br />

If we wanted to rebuild the index that serves as the primary key for the Sales.SalesOrderDetail<br />

table we were looking at earlier with a fill factor of 65, we would issue an ALTER INDEX command as<br />

follows:<br />

ALTER INDEX PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID<br />

ON Sales.SalesOrderDetail<br />

REBUILD WITH (FILLFACTOR = 100)<br />

This would be a good time to digress and revisit the idea of your object names. Notice how long this<br />

name is. The more characters, the more chance for typos. Balance that against the need for the name to<br />

be meaningful. For this particular query, my own naming conventions would probably have dropped<br />

the inclusion of the table name (when referring to an index, you usually already know what table you’re<br />

talking about) and eliminated the underscores to wind up with something like PKSalesOrderIDSalesOrderDetailID.<br />

The name implies the nature of the index (it’s supporting a primary key) and the<br />

columns involved. Be careful, however, with using the column name idea. On the rare occasion you<br />

wind up with several columns in your index, it can make for an unruly index name.<br />

We can then re-run the sys.dm_db_index_physical_stats to see the effect:<br />

database_id object_id index_id index_depth avg_fragmentation_in_percent page_count<br />

----------- ----------- ----------- ----------- ---------------------------- -----------<br />

8 610101214 1 3 0.0810372771474878 1234<br />

8 610101214 2 3 1.70731707317073 410<br />

8 610101214 3 2 6.14035087719298 228<br />

(3 row(s) affected)<br />

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

The big one to notice here is the change in avg_fragmentation_in_percent. Assuming we’re looking<br />

for high page densities (higher read performance), then we want the number as low as possible. The<br />

number didn’t quite reach 0 percent because <strong>SQL</strong> <strong>Server</strong> has to deal with page and row sizing, but it gets<br />

as close as it can.<br />

Several things to note about ALTER TABLE REINDEX/REORG and FILLFACTOR:<br />

❑ If a FILLFACTOR isn’t provided, then the ALTER TABLE will use whatever setting was used to<br />

build the index previously. If one has never been specified, then the FILLFACTOR will make the<br />

page full (which is too full for most situations).<br />

❑ If a FILLFACTOR is provided, then that value becomes the default FILLFACTOR for that index.<br />

❑ While a REORGANIZE is done live and a REBUILD can be (if you have the licensing required to<br />

enable that feature), I strongly recommend against it. It locks resources and can cause a host of<br />

problems. At the very least, look at doing it at non-peak hours.<br />

295

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

Saved successfully!

Ooh no, something went wrong!