Indexing Fundamentals Steve Hood SimpleSQLServer.com.

Slides:



Advertisements
Similar presentations
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Advertisements

EXECUTION PLANS By Nimesh Shah, Amit Bhawnani. Outline  What is execution plan  How are execution plans created  How to get an execution plan  Graphical.
Dos and don’ts of Columnstore indexes The basis of xVelocity in-memory technology What’s it all about The compression methods (RLE / Dictionary encoding)
Managing and Maintaining Indexes and Statistics. Table Fragmentation Causes 1 Massive Updates or deletes 2 Frequent Page Splitting 3 Disk space contention.
Chapter 8 File organization and Indices.
Indexes Rose-Hulman Institute of Technology Curt Clifton.
Denny Cherry twitter.com/mrdenny.
Lecture 8 Index Organized Tables Clusters Index compression
CS 345: Topics in Data Warehousing Tuesday, October 19, 2004.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Index tuning Performance Tuning. Overview Index An index is a data structure that supports efficient access to data Set of Records index Condition on.
Ashwani Roy Understanding Graphical Execution Plans Level 200.
Applications hitting a wall today with SQL Server Locking/Latching Scale-up Throughput or latency SLA Applications which do not use SQL Server.
SQL Server Indexes Indexes. Overview Indexes are used to help speed search results in a database. A careful use of indexes can greatly improve search.
Table Indexing for the.NET Developer Denny Cherry twitter.com/mrdenny.
Copyright © 2005 Ed Lance Fundamentals of Relational Database Design By Ed Lance.
Denny Cherry twitter.com/mrdenny.
T-SQL: Simple Changes That Go a Long Way DAVE ingeniousSQL.com linkedin.com/in/ingenioussql.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
SQLintersection Putting the "Squeeze" on Large Tables Improve Performance and Save Space with Data Compression Justin Randall Tuesday,
SQL SERVER DAYS 2011 Table Indexing for the.NET Developer Denny Cherry twitter.com/mrdenny.
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.
Analysing Indexes SQLBits 6 th October 2007 © Colin Leversuch-Roberts Kelem Consulting Limited September 2007.
Chapter 5 Index and Clustering
Session 1 Module 1: Introduction to Data Integrity
Creating Indexes on Tables An index provides quick access to data in a table, based on the values in specified columns. A table can have more than one.
Query Execution. Where are we? File organizations: sorted, hashed, heaps. Indexes: hash index, B+-tree Indexes can be clustered or not. Data can be stored.
Table Structures and Indexing. The concept of indexing If you were asked to search for the name “Adam Wilbert” in a phonebook, you would go directly to.
SQL SERVER DAYS 2011 Indexing Internals Denny Cherry twitter.com/mrdenny.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
October 15-18, 2013 Charlotte, NC Accelerating Database Performance Using Compression Joseph D’Antoni, Solutions Architect Anexinet.
How to kill SQL Server Performance Håkan Winther.
APRIL 13 th Introduction About me Duško Mirković 7 years of experience.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
Why Should I Care About … The Plan Cache? Tuning When Stakeholders Won’t Say Where It Hurts.
SQL Basics Review Reviewing what we’ve learned so far…….
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Fixing Page Life Expectancy Steve Hood Blog: SimpleSQLServer.com.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Chris Index Feng Shui Chris
Indexes By Adrienne Watt.
SQL Implementation & Administration
Indices.
UFC #1433 In-Memory tables 2014 vs 2016
Introduction to SQL Server Management for the Non-DBA
Database Administration for the Non-DBA
The Ins and Outs of Indexes
The Key to the Database Engine
Table Indexing for the .NET Developer
Lecture 12 Lecture 12: Indexing.
Fixing Page Life Expectancy
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Indexing Fundamentals
Indexes: The Basics Kathi Kellenberger.
Steve Hood SimpleSQLServer.com
The Ins and Outs of Indexes
Indexing for Beginners
Four Rules For Columnstore Query Performance
The Ins and Outs of Indexes
Diving into Query Execution Plans
All about Indexes Gail Shaw.
The Ins and Outs of Indexes
Presentation transcript:

Indexing Fundamentals Steve Hood SimpleSQLServer.com

The Rules Interrupt me Learn it as though you’ll teach it Don’t leave without understanding

Outline Index Types, focusing on clustered and nonclustered How indexes are used and abused The costs of clustered and nonclustered indexes Cleaning up unwanted indexes Links

Index Types Heap (Not really an index) Clustered Nonclustered XML Spatial Full-Text Columnstore Hash

Clustered Index Telephone book Multiple keys Last Name First Name Middle Initial Address Phone Number

Heap Reference book without a table of contents. Has a page number It just happens to be in that order Rare to have a legitimate use Very small tables, although it doesn’t enforce uniqueness Very large tables always referenced through other indexes Significantly faster on inserts? This is a common myth

Nonclustered Index Index at the back of a book Example of a single key with either RID or Clustered Index Key

SARGable SARGable = Search Argument capable Bad: LastName LIKE '%Hoo%' Good: LastName LIKE 'Hoo%' Bad: Year(DateAdded) Good: DateAdded >= as VarChar(4)) + '-1-1' as DateTime) AND DateAdded < + 1 as VarChar(4)) + '-1-1' as DateTime) Bad: DateDiff(DAY, DateAdded, GetDate()) < 7 Good: DateAdded > DateAdd(DAY, -7, GetDate())

Execution Plans

Do NOT look at the whole plan SET STATISTICS TIME ON SET STATISTICS IO ON Indexes and Heaps are referenced in just three ways Scan Seek Lookup

Scan Read every row in an index or heap Not always a bad thing Using most or all records Not always a good thing Non-SARGable arguments used Lack of a Seek Predicate in an Execution Plan Denny Cherry: Seeks Aren’t Always Better Than Scans Rob Farley: Scans Are Better Than Seeks

Seek Find rows in an index knowing at least part of the first key field Heaps don’t have key fields, so you can’t seek. Typically more efficient Executing 1000 seeks can cost more than 1 scan (see links on last slide) Does not mean it filtered it down much Does not mean it didn’t scan through the rest of the records If there is a Predicate, there is at least one piece not handled by the key field(s)

Lookup A nonclustered index was used, but didn’t have all the columns RID Lookup on Heaps Key Lookup on Clustered Indexes Can be justified Less used query Large columns (especially XML, VarChar(Max), etc) Large number of columns

Covering Index A single index that has every column requested by the statement A clustered index includes every column Always a covering index A nonclustered index can be by adding included columns Updates are more likely to need to update this index Index is larger Disk Backups Memory

Scan Demo -- All demos are done on base install of AdventureWorks Also, all work on all supported versions of SQL Server SET STATISTICS IO ON SET STATISTICS TIME ON --SARGable Scan and Seek SELECT LastName, FirstName FROM Person.Person WHERE LastName LIKE '%simps%' SELECT LastName, FirstName FROM Person.Person WHERE LastName LIKE 'simps%'

Seek Demo SET STATISTICS IO ON SET STATISTICS TIME ON --Seek examples --Didn't filter it down much SELECT LastName, FirstName FROM Person.Person WHERE LastName LIKE '[a-m]%' --Scanned through the rest of the records SELECT LastName, FirstName FROM Person.Person WHERE LastName LIKE '[a-m]%' AND FirstName = 'Steve'

Lookup Demo SET STATISTICS IO ON SET STATISTICS TIME ON --Lookup Examples IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[Person].[Person]') AND name = N'IX_Person_LastName_FirstName_MiddleName_INCL') DROP INDEX [IX_Person_LastName_FirstName_MiddleName_INCL] ON [Person].[Person] GO --Able to do the same seek, but had to do a Lookup to get the Promotion SELECT LastName, FirstName, Promotion FROM Person.Person WHERE LastName LIKE '[a-m]%' AND FirstName = 'Steve'

Covering Index Demo --Covering Index Example CREATE INDEX IX_Person_LastName_FirstName_MiddleName_INCL ON Person.Person ( LastName, FirstName, MiddleName ) INCLUDE ( Promotion ) --Same select statement we used last time SELECT LastName, FirstName, Promotion FROM Person.Person WHERE LastName LIKE '[a-m]%' AND FirstName = 'Steve'

Filtered Index Add a WHERE clause to an index declaration Must match the WHERE clause in a query Index on Orders WHERE status is “open” Will probably be less than 1% of the results Index on Items WHERE QtyInStock > 0 Filtering by a common value that greatly reduces the number of rows Must be filtered by static values Can’t do WHERE DateAdded > DateAdd(DAY, -7, GetDate())

Indexed Views Require certain settings to be enabled by all connections editing data Changes to all tables involved can update the view Gets to be expensive Requires SCHEMABINDING on the view They have their place Few updates compared to reads Or updates done in a specific window where you can drop the index and recreate it later Joining the tables takes a lot of resources Can be used for aggregations Kendra Little wrote What You Can and Can’t Do With Indexed ViewsWhat You Can and Can’t Do With Indexed Views

Index Costs Clustered – insignificant differences to heap Nonclustered Data Modification Overhead Disk Space Backups Time and Size Memory

Memory Costs SQL Server reads and writes data in memory Writes ensure the page is in memory then updates it there Reads ensure the page is in memory then uses it from there SQL keeps data in memory as long as possible Page Life Expectancy shows how long it’s expected to hang around This reduces load on disk Less trips to disk mean better performance, even flash disks are slower What is competing for your memory? Cleaning Up the Buffer Pool to Increase PLE

Index Cleanup – Unused Indexes Sys.dm_db_index_usage_stats Make sure you trend over time Quarterly or annual reports DMV is reset on restart and can be reset on index maintenance Indexes – Unused and Duplicates

Index Cleanup – Duplicate Indexes Same first one or two key fields are typically considered duplicate More costly than unused indexes Can typically be combined More efficient even if new index is larger than any prior index Indexes – Unused and Duplicates

Duplicate Indexes Demo --Works best when run in prod, but it takes about 30 seconds of CPU to run in a descent environment SELECT Cached_MB = Cast(x.cached_MB as DEC(20,1)), ObjName = x.name, x.index_id, x.index_name, Pct_Of_Total_Cache = cast((x.cached_mb * 100) / cast(SUM(x.cached_mb) over () as DEC(20,4)) as DEC(4,2)), Pct_Of_Object_In_Cache = cast((x.cached_mb * 100) / cast(NULLIF(size.Used_MB,0) as DEC(30,4)) as DEC(10,2)), Object_Size_MB = size.Used_MB, Object_InRow_Size_MB = size.Used_InRow_MB, Object_LOB_Size_MB = size.Used_LOB_MB, free_space_mb, dirty_pages_mb FROM ( SELECT count(1)/128.0 AS cached_MB, name = OBJECT_SCHEMA_NAME(p.object_id) + '.' + object_name(p.object_id), p.object_id, p.index_id, index_name = i.name, free_space_mb = CAST(SUM(bd.free_space_in_bytes)/1024/ as DEC(20,1)), dirty_pages_mb = CAST(SUM(cast(is_modified as Int))/128.0 as DEC(20,1)) FROM sys.dm_os_buffer_descriptors AS bd with (NOLOCK) INNER JOIN sys.allocation_units AS au with (NOLOCK) ON bd.allocation_unit_id = au.allocation_unit_id INNER JOIN sys.partitions AS p with (NOLOCK) ON (au.container_id = p.hobt_id AND au.type IN (1,3)) OR (au.container_id = p.partition_id AND au.type = 2) INNER JOIN sys.indexes i ON p.object_id = i.object_id AND p.index_id = i.index_id WHERE database_id = db_id() GROUP BY p.object_id, p.index_id, i.name --HAVING Count(1) > 128 ) x INNER JOIN ( SELECT PS.object_id, PS.index_id, Used_MB = Cast(SUM(PS.used_page_count) / as DEC(20,2)), Used_InRow_MB = Cast(SUM(PS.in_row_used_page_count) / as DEC(20,2)), Used_LOB_MB = Cast(SUM(PS.lob_used_page_count) / as DEC(20,2)) FROM sys.dm_db_partition_stats PS GROUP BY PS.Object_ID, PS.Index_ID ) size ON x.object_id = size.object_id AND x.index_id = size.index_id ORDER BY 5 DESC, 1 DESC;

Data Compression – At a Glance Enterprise-Only feature Row and Page Compression Page is Row Compression + more B+Tree has Row Compression Uses more CPU because it uncompresses as it’s being used Uses less memory because it’s compressed in memory, too Uses less CPU and Disk IO for physical reads Less to read from disk Less data in memory, less memory pressure, lower data turnover Different data compresses at different rates (sp_estimate_data_compression_savings)

Posts I wrote for more info Fixing Page Life Expectancy (PLE) Cleaning Up the Buffer Pool to Increase PLE Indexing Fundamentals Optional Parameters Causing Index Scans Indexes – Unused and Duplicates