Module 6 Implementing Table Structures in SQL Server ®2008 R2.

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

Module 3: Creating and Tuning Indexes. Planning Indexes Creating Indexes Optimizing Indexes.
Denny Cherry Manager of Information Systems MVP, MCSA, MCDBA, MCTS, MCITP.
Module 8 Importing and Exporting Data. Module Overview Transferring Data To/From SQL Server Importing & Exporting Table Data Inserting Data in Bulk.
Database Administration, Integrity and Performance.
Project Management Database and SQL Server Katmai New Features Qingsong Yao
Working with SQL Server Database Objects
A HEAP OF CLUSTERS A look into heaps vs. clustered tables Ami Levin CTO, DBSophic X.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
Indexes Rose-Hulman Institute of Technology Curt Clifton.
Module 7: Creating and Maintaining Indexes. Overview Creating Indexes Creating Index Options Maintaining Indexes Introduction to Statistics Querying the.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand, create, and drop views Recognize the benefits of using views Grant and revoke user’s database.
Module 9 Designing an XML Strategy. Module 9: Designing an XML Strategy Designing XML Storage Designing a Data Conversion Strategy Designing an XML Query.
Denny Cherry twitter.com/mrdenny.
Module 17 Storing XML Data in SQL Server® 2008 R2.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
Using XML in SQL Server 2005 NameTitleCompany. XML Overview Business Opportunity The majority of all data transmitted electronically between organizations.
Module 8 Improving Performance through Nonclustered Indexes.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Module 9 Designing and Implementing Stored Procedures.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
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.
Module 16: Performing Ongoing Database Maintenance
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
M1G Introduction to Database Development 2. Creating a Database.
Module 3: Creating Data Types and Tables. Overview Working with Data Types Working with Tables Generating Column Values Generating Scripts.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
Denny Cherry twitter.com/mrdenny.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
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.
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.
Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu.
Session 1 Module 1: Introduction to Data Integrity
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
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.
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.
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Indexes Part 2 What type of Indexes are there? Make sure you have the pages 2 & 3 of the Lab for Indexes in front of you before playing this presentation.
October 15-18, 2013 Charlotte, NC SQL Server Index Internals Tim Chapman Premier Field Engineer.
SQL Basics Review Reviewing what we’ve learned so far…….
Module 6: Creating and Maintaining Indexes. Overview Creating Indexes Understanding Index Creation Options Maintaining Indexes Introducing Statistics.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Chris Index Feng Shui Chris
Indexes By Adrienne Watt.
The Basics of Data Manipulation
SQL Implementation & Administration
Data Definition and Data Types
Module 4: Creating and Tuning Indexes
Designing Database Solutions for SQL Server
Module 5: Implementing Data Integrity by Using Constraints
Table Indexing for the .NET Developer
Session #, Speaker Name Indexing Chapter 8 11/19/2018.
The Basics of Data Manipulation
Microsoft SQL Server 2014 for Oracle DBAs Module 7
Database systems Lecture 6 – Indexes
A Guide to SQL, Eighth Edition
SQL Server Indexing for the Client Developer
Presentation transcript:

Module 6 Implementing Table Structures in SQL Server ®2008 R2

Module Overview SQL Server Table Structures Working with Clustered Indexes Designing Effective Clustered Indexes

Lesson 1: SQL Server Table Structures What is a Heap? Operations on Heaps Forwarding Pointers What is a Clustered Index? Operations on Clustered Indexes Unique vs. Non-Unique Clustered Indexes Demonstration 1A: Rebuilding Heaps

What is a Heap? Heap idindex_id=0first_iam_page IAM Page Data Pages A table with: No specified order for pages within the table No specified order for data within each page Data that is inserted or modified can be placed anywhere within the table

Operations on Heaps INSERT  Each new row can be placed in the first available page with sufficient space UPDATE  The row can either remain on the same page if it still fits, otherwise, it can be removed from the current page and placed on the first available page with sufficient space DELETE  Frees up space on the current page  Data is not overwritten, space is just flagged as available for reuse SELECT  Entire table needs to be read for most queries, if no indexes are available

Forwarding Pointers Data modifications in heaps can leave forwarding pointers  Row IDs in other indexes do not need to be updated  Can lead to performance issues over time  Only a single forwarding pointer is used Performance would be improved by removing forwarding pointers and updating other indexes  No easy option for this prior to SQL Server 2008  SQL Server 2008 and later introduced ability to rebuild a table (including a heap)  ALTER TABLE WITH REBUILD Forwarding pointers are references left at the original location of a row, when the row has been moved.

What is a Clustered Index? Leaf Nodes idindex_id=1root_page Data Pages Intermediate Level Intermediate Level Index Pages Root Index Page Table pages stored in logical order Rows stored in logical order within table pages Single clustered index per table

Operations on Clustered Indexes INSERT  Each new row must be placed into the correct logical position  May involve splitting pages of the table UPDATE  The row can either remain in the same place if it still fits and if the clustering key value is still the same  If the row no longer fits on the page, the page needs to be split  If the clustering key has changed, the row needs to be removed and placed in the correct logical position within the table DELETE  Frees up space by flagging the data as unused SELECT  Queries related to the clustering key can seek  Queries related to the clustering key can scan and avoid sorts

Unique vs. Non-Unique Clustered Indexes Clustered indexes can be Unique Non-Unique SQL Server must be able to identify individual rows Adds a uniqueifier (4 bytes long) when needed for non- unique indexes Always specify indexes as unique if they are

Demonstration 1A: Rebuilding Heaps In this demonstration you will see how to:  Create a table as a heap  Check the fragmentation and forwarding pointers for a heap  Rebuild a heap

Lesson 2: Working with Clustered Indexes Creating Clustered Indexes Dropping a Clustered Index Altering a Clustered Index Incorporating Free Space in Indexes Demonstration 2A: Clustered Indexes

Creating Clustered Indexes CREATE TABLE dbo.Article ( ArticleID int IDENTITY(1,1) PRIMARY KEY, ArticleName nvarchar(50) NOT NULL, PublicationDate date NOT NULL ); CREATE TABLE dbo.LogData ( LogID int IDENTITY(1,1), LogData xml NOT NULL ); ALTER TABLE dbo.LogData ADD CONSTRAINT PK_LogData PRIMARY KEY (LogId); CREATE CLUSTERED INDEX CL_LogTime ON dbo.LogTime(LogTimeID); CREATE TABLE dbo.Article ( ArticleID int IDENTITY(1,1) PRIMARY KEY, ArticleName nvarchar(50) NOT NULL, PublicationDate date NOT NULL ); CREATE TABLE dbo.LogData ( LogID int IDENTITY(1,1), LogData xml NOT NULL ); ALTER TABLE dbo.LogData ADD CONSTRAINT PK_LogData PRIMARY KEY (LogId); CREATE CLUSTERED INDEX CL_LogTime ON dbo.LogTime(LogTimeID); Can be created by specifying PRIMARY KEY on table Can be created directly

Dropping a Clustered Index DROP INDEX CL_LogTime ON dbo.LogTime; ALTER TABLE dbo.LogData DROP CONSTRAINT PK_LogData; DROP INDEX CL_LogTime ON dbo.LogTime; ALTER TABLE dbo.LogData DROP CONSTRAINT PK_LogData; Drop an external index via DROP INDEX Drop a PRIMARY KEY constraint via ALTER TABLE  May not be possible where foreign key references exist

Altering a Clustered Index ALTER INDEX CL_LogTime ON dbo.LogTime REBUILD; ALTER INDEX ALL ON dbo.LogTime REBUILD; ALTER INDEX ALL ON dbo.LogTime REORGANIZE; ALTER INDEX CL_LogTime ON dbo.LogTime REBUILD; ALTER INDEX ALL ON dbo.LogTime REBUILD; ALTER INDEX ALL ON dbo.LogTime REORGANIZE; Some modifications permitted via ALTER INDEX  Can REBUID or REORGANIZE  Can DISABLE Cannot modify the key columns of the index  CREATE INDEX WITH DROP EXISTING can do this

Incorporating Free Space in Indexes ALTER TABLE Person.Person ADD CONSTRAINT PK_Person_BusinessEntityID PRIMARY KEY CLUSTERED ( BusinessEntityID ASC ) WITH (PAD_INDEX = OFF, FILLFACTOR = 70); GO ALTER TABLE Person.Person ADD CONSTRAINT PK_Person_BusinessEntityID PRIMARY KEY CLUSTERED ( BusinessEntityID ASC ) WITH (PAD_INDEX = OFF, FILLFACTOR = 70); GO Free space can be left in indexes, including clustered indexes  FILLFACTOR  PADINDEX Free space can improve performance of certain operations Default value can be changed using sp_configure

Demonstration 2A: Clustered Indexes In this demonstration you will see how to:  Create a table with a clustered index  Detect fragmentation in a clustered index  Correct fragmentation in a clustered index

Lesson 3: Designing Effective Clustered Indexes Characteristics of Good Clustering Keys Appropriate Data Types for Clustering Keys Creating Indexed Views Indexed View Considerations Demonstration 3A: Indexed Views

Characteristics of Good Clustering Keys Good clustering keys have specific properties  Short  Static  Increasing (not necessarily monotonically)  Unique Limits on clustering keys  16 columns  900 bytes

Appropriate Data Types for Clustering Keys Data TypeComments intGood candidate particularly if IDENTITY bigintGood candidate particularly if IDENTITY uniqueidentifierSome concerns with size, big concern with lack of increasing values varcharConcerns with size, concerns with sorting performance, concerns with static nature dateConcern with uniqueness but excellent for range queries smalldatetimeConcern with uniqueness

Creating Indexed Views Clustered indexes can be based on views as well as on tables  Other database engines call these "materialized views" Query optimizer may use indexed views to speed up query operations  Even if the view is not mentioned in the query Indexed View Benefit  Can greatly speed up queries Indexed View Costs  Can significantly impact performance of data modification operations  Need to consider the balance of SELECT vs. data modification operations

Indexed View Considerations For an indexed view to be used in a query, a series of session SET options needs to match those when the indexed view was created Enterprise Edition and Datacenter Edition of SQL Server will automatically utilize indexed views where it makes sense to do so  Other editions need to use the NOEXPAND query hint View definition must be deterministic View must be created WITH SCHEMABINDING Full list of considerations is contained in Books Online

Demonstration 3A: Indexed Views In this demonstration you will see how to:  Obtain details of indexes created on views  See if an indexed view has been used in an estimated execution plan

Lab 6: Implementing Table Structures in SQL Server Exercise 1: Creating Tables as Heaps Exercise 2: Creating Tables with Clustered Indexes Challenge Exercise 3: Comparing the Performance of Clustered Indexes vs. Heaps (Only if time permits) Logon information Estimated time: 45 minutes

Lab Scenario One of the most important decisions when designing a table is to choose an appropriate table structure. In this lab, you will choose an appropriate structure for some new tables required for the relationship management system.

Lab Review When is it important that a clustered index has an increasing key? Which table structure is automatically assigned when a table is assigned a primary key during the table creation, without specifying a structure?

Module Review and Takeaways Review Questions Best Practices