Chapter 4 Indexes
Index Architecture By default data is inserted on a first-come, first-serve basis Indexes bring order to this chaos Once you create an index you will have data and index pages Indexes can be build against tables or views SQL Server Query Optimizer determines how the indexes are used © Wiley Inc All Rights Reserved.
Sysindexes Table Contains a record for each index in the database The index type for each index is stored in the indid column The FirstIAM column stores the location of the first Index Allocation Map page This page tells SQL Server where each related page is in the database © Wiley Inc All Rights Reserved.
Heaps A heap is a table with no clustered index Heaps have a value of 0 in the indid column of sysindexes Extents are not physically next to each other Pages in an extent are See us/library/ms aspx for pages and extentshttp://msdn.microsoft.com/en- us/library/ms aspx SQL Server must read the IAM (Index Allocation Map) page to find each page of the table This process is called a table scan © Wiley Inc All Rights Reserved.
B-tree Format Indexes are stored in a b-tree format There is a root level at the top Then there are several levels of intermediate pages The leaf levels hold the actual index data The data is different depending on the type of index © Wiley Inc All Rights Reserved.
Four + types of indexes Clustered Nonclustered Spatial Primary XML Other characters Unique Included columns Filtered
Clustered Indexes These physically rearrange the data in your table Organized much like a dictionary Have a value of 1 in the indid column of the sysindexes table The leaf level of the index contains the actual data us/library/ms aspx © Wiley Inc All Rights Reserved.
Access Data in a Clustered Index When queried, SQL Server reads the root column of sysindexes This contains the location of the root page of the index Each page in the index has a pointer to the next page Eliminates the need to read an IAM page SQL Server reads each page until it reaches the data in the leaf level and returns it to the user © Wiley Inc All Rights Reserved.
Modifying Data in a Clustered Index Each page has blank space to leave room for new data Because data must be physically arranged This space is called the fill factor A fill factor of 70 means 30 percent free space A fill factor of 100 or 0 means no room for record on the page us/library/ms aspx New data is inserted in the free space at the end of the correct page © Wiley Inc All Rights Reserved.
Modifying Data in a Clustered Index, Cont. Fill factor is not automatically maintained, that is manual When a page fills completely, half of the data is moved to a new page This is called a page split Setting the fill factor correctly can help avoid excessive page splits © Wiley Inc All Rights Reserved.
? How many clustered indexes can a table have? A.One or none B.Two C.More than one D.None of the above
Nonclustered Indexes These do not physically rearrange the data in your table Organized much like the appendix at the back of a book Have a value between 2 and 251 in the indid column of sysindexes This depends on how many nonclustered indexes you have on the table The leaf level of the index contains a pointer to the actual data us/library/ms aspx © Wiley Inc All Rights Reserved.
Accessing Data in a Nonclustered Index Like a clustered index, SQL Server reads the root column of sysindexes Each page in the index has a pointer to the next page SQL Server reads each page until it reaches the leaf level Then it uses the pointer in the leaf node to find the data If you are looking for a range of values, SQL Server must refer to the index page for each record in the range © Wiley Inc All Rights Reserved.
Accessing Data in a Nonclustered Index Nonclustered indexes can be created on tables with a clustered index If this is the case, the pointer in the leaf level does not point to the data It points to the key value of the clustered index © Wiley Inc All Rights Reserved.
Modifying Data in a Nonclustered Index In a nonclustered index on a heap, data is inserted wherever there is room On a clustered index, the data is physically arranged In both cases, the nonclustered index is updated to point to the new data This is especially useful when a page split occurs © Wiley Inc All Rights Reserved.
Costs of indexing Initial creation CPU time Store The index itself The related info Maintain Update when executing an action query Page split Recalculate the statistics
Included Columns and Covering Index An index that SQL Server can use to satisfy a query without having to access the table is called a covering index Included columns are not part of the key values of the index They are included in the data returned to the user This can speed up data access for complex queries The columns in the INCLUDE may be too big to be part of the index CREATE NONCLUSTERED INDEX IX_PPaR ON Production.ProductReview (ProductID, ReviewerName) INCLUDE (Comments); © Wiley Inc All Rights Reserved.
Included Column Considerations Nonkey columns can only be included in nonclustered indexes. Columns can’t be defined in both the key column and INCLUDE list. Column names can’t be repeated in the INCLUDE list. At least one key column must be defined. You must have between 1 and 16 key columns defined. You can only have up to a maximum of 1023 included columns. Nonkey columns can’t be dropped from a table unless the index is dropped first. The only changes allowed to nonkey columns are: Changing nullability (from NULL to NOT NULL and vice versa) Increasing the length of varbinary, varchar, or nvarchar columns. © Wiley Inc All Rights Reserved.
Relational Options PAD_INDEX Default OFF When ON, The percentage of free space that is specified by FILLFACTOR is applied to the intermediate-level pages of the index. FILLFACTOR 0 or 100 the same, otherwise the percent used, does not auto re-calculate SORT_IN_TEMPDB Where to store some sorting result IGNORE_DUP_KEY STATISTICS_NORECOMPUTE DROP_EXISTING ONLINE (ON and OFF) Query and creating of index at the same time? ALLOW_ROW_LOCKS ALLOW_PAGE_LOCKS MAXDOP Number of processors © Wiley Inc All Rights Reserved.
Creating and Maintaining Indexes Using the GUI tools Double click a database Right click the indexes folder Select “New index” to create a new one Select the index from the list of indexes to modify one