Presentation is loading. Please wait.

Presentation is loading. Please wait.

October 15-18, 2013 Charlotte, NC SQL Server Index Internals Tim Chapman Premier Field Engineer.

Similar presentations


Presentation on theme: "October 15-18, 2013 Charlotte, NC SQL Server Index Internals Tim Chapman Premier Field Engineer."— Presentation transcript:

1 October 15-18, 2013 Charlotte, NC SQL Server Index Internals Tim Chapman Premier Field Engineer

2 October 15-18, 2013 | Charlotte, NC Please silence cell phones

3 Explore Everything PASS Has to Offer Free SQL Server and BI Web Events Free 1-day Training Events Regional Event Local User Groups Around the World Free Online Technical Training This is Community 3 Business Analytics Training Session Recordings PASS Newsletter

4 Session Evaluations ways to access Go to passsummit/evals Download the GuideBook App and search: PASS Summit 2013 Follow the QR code link displayed on session signage throughout the conference venue and in the program guide Submit by 5pm Friday Oct. 18 to WIN prizes Your feedback is important and valuable. 4

5 About Me Tim Chapman Dedicated Premier Field Engineer at Microsoft Contributing author SQL Server 2012 Bible SQL Server MVP Deep Dives 2 @chapmandew

6 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!

7 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”

8 Index Structures Clustered Indexes Non-Clustered Indexes

9 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

10 Clustered Index Structure

11 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

12 Non-clustered Index Structure

13 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

14 Included Columns

15

16 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

17 Showing Statistics…

18 Database Statistics Options (1) AUTO_CREATE_STATISTICS AUTO_UPDATE_STATISTICS AUTO_UPDATE_STATISTICS_ASYNC

19 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.

20

21 Index Maintenance Reviewing fragmentation Affects of fragmentation Rebuild vs Reorganization

22 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

23 Logical Fragmentation Index/Data pages not physically and logically aligned Can hurt scan performance, but not seek operations

24 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

25 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.

26

27 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 ALTER INDEX…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

28 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

29 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

30 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.

31 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

32 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

33

34 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

35 SQL 2014 Index Changes Online operation lock priority Hash and Bw-tree Indexes in Hekaton Create an index in table definition

36 Questions?

37 Resources 37


Download ppt "October 15-18, 2013 Charlotte, NC SQL Server Index Internals Tim Chapman Premier Field Engineer."

Similar presentations


Ads by Google