390 likes | 702 Views
SQL Server Index Internals. Tim Chapman Premier Field Engineer. About Me. Tim Chapman Dedicated Premier Field Engineer at Microsoft Contributing author SQL Server 2012 Bible SQL Server MVP Deep Dives 2. @ chapmandew. Session Goals. Index structure familiarity
E N D
SQL Server Index Internals Tim Chapman Premier Field Engineer
About Me Tim ChapmanDedicated Premier Field Engineer at Microsoft Contributing author SQL Server 2012 Bible SQL Server MVP Deep Dives 2 @chapmandew
Session Goals Index structure familiarity I won’t break out a debugger But, I will show you some neat tools for viewing indexes Have some fun talking about indexes!
Heaps • Tables without a clustered index • Unordered masses of data • Data rows fit where they can – PFS pages used • Great for quickly importing large sets of data • Not great for most production environments • Use ALTER TABLE…REBUILD to “rebuild”
Index Structures Clustered Indexes Non-Clustered Indexes
Clustered Indexes Implemented as a B-Tree data structure • Logical order must always be maintained • The leaf level of the index contains all table columns • Why there is only one per table – the index IS the table • We always implement these as unique
Non-clustered Indexes • Also a B-Tree structure data structure • Is NOT part of the table it is defined on • It MUST point to the base table somehow • Only a subset of the table columns • A skinny table for fast searching and sorting
Included Columns • Added columns of data in the leaf level of an NC index • Used for covering queries • Not restricted to the NC index 900 byte size restriction • You can use (n)varchar(max), but not (n)text or image data types
Demo Investigate Index/Data Pages
Statistics A sampling of the data in a given table/index column The optimizer relies on these for decision making Out of date or skewed statistics can lead to sub-optimal execution plans
Database Statistics Options (1) • AUTO_CREATE_STATISTICS • AUTO_UPDATE_STATISTICS • AUTO_UPDATE_STATISTICS_ASYNC
Database Statistics Options – Best Practices Use the defaults unless you NEED to do otherwise Often large DW workloads are the exception Note: Trace Flag 2371 can help with RT issues.
Demo Out of date statistics
Index Maintenance Reviewing fragmentation Affects of fragmentation Rebuild vs Reorganization
Page Splits • A record must always be placed on a specific page • We must maintain the index logical order • If the record doesn’t fit, we must do some rearranging • This is resource intensive - causes logical fragmentation
Logical Fragmentation Index/Data pages not physically and logically aligned Can hurt scan performance, but not seek operations
Page Density How full a page is upon a (re)build/reorganization More full pages can cause page splits Less full pages can waste Buffer Pool space
When does fragmentation matter? Negligible for singleton lookup seek operations Matters most for scanning purposes Note: If your index is highly fragmented, there is a good chance your statistics are skewed or out of date.
Demo The effects of Index Fragmentation
Rebuilding an index (1) • Very few uses for DROP INDEX…CREATE INDEX • All NC indexes rebuilt twice if you do this with the clustered index • You must know the exact index structure for recreation • ALTERINDEX…REBUILD • NC indexes not automatically rebuilt if done on a clustered index • ALTER TABLE…REBUILD • Use for Heap tables – will always rebuild all NC indexes too
Rebuilding an Index (2) • Offline rebuilds locks the index during the operation • NOT the entire table – though that can certainly be the case • ALTER INDEX ALL • Rebuilds all indexes on the table in index_id order • DROP_EXISTING • Part of the CREATE INDEX syntax • Great for changing the index definition
Index Reorganization Exclusively locks, compacts and reorders 8 pages at a time Removes leaf level fragmentation Tries to establish the original fill factor Always: single threaded, Online
Rebuild vs. Reorganize Strategy (1) • When to do what? • Common wisdom is: • If <= 30% logical fragmentation THEN Reorganize • If > 30% logical fragmentation THEN Rebuild • …your mileage will vary. Choose what works for you.
Rebuild vs. Reorganize Strategy (2) Reorganize… • Is always an online operation • Can be stopped and you won’t lose the work accomplished • Generates a LOT more t-log records than Rebuild • Typically will not remove as much fragmentation as Rebuild • Never updates Statistics
Rebuild vs. Reorganize Strategy (3) • Rebuild… • Is an atomic operation – all happens or none happens • Will update statistics with FULLSCAN • One caveat to this in 2012 • Will involve some blocking – even Online operations • Can be minimally logged
Demo Looking at transaction log usage from an index rebuild
SQL Server 2012 Index Changes Rebuild indexes with (B)LOB columns online Online rebuilds of partitioned tables do not always result in a 100% sample rate
SQL 2014 Index Changes Online operation lock priority Hash and Bw-tree Indexes in Hekaton Create an index in table definition