Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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

3 Types of index A clustered index »These can be unique and or composite A nonclustered index »These can be unique and or composite They are created on a column of a table

4 A clustered index In this index the leaf pages are the data pages from the table. The table is physically sorted by the clustered indexed column. Like a telephone directory or encyclopaedia. You can have only one clustered index per table

5 A nonclustered index In this index the leaf page has a pointer (row number) which points to the data in the table. Like a reference book. The table is not physically sorted You can have more than one nonclustered index on a table.

6 Simplest syntax CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name ON { table | view } ( column) [ WITH [,...n] ] [ ON filegroup ] Leaves you with CREATE INDEX index_name ON table_name (col_name) Creates nonclustered index

7 The options - UNIQUE Creates a unique index (one in which no two rows are permitted to have the same index value) on a table or view. Benefit? Once SQL Server has found this entry it knows to stop searching. CREATE UNIQUE INDEX index_name ON table_name (col_name )

8 The options - COMPOSITE Uses more than one column for the index key Can be clustered or nonclustered CREATE INDEX index_name ON table ( column1,column2)

9 Index example Example - when designing database you discover that – A list of employees will be created showing their department number. The list will be produced in alphabetical order by last name, and then followed by first name. What would be the best index here? Write the create statement.

10 Composite Index If this is a common query SELECT LastName, FirstName FROM Employee WHERE DeptNo = 50 ORDER BY LastName Create this index CREATE CLUSTERED INDEX EmployeeName ON Employee (LastName, FirstName,DeptNo) Clustered because you want a sorted output Composite because it covers more than 1 column

11 INDEX OPTIONS :: = { PAD_INDEX | FILLFACTOR = fillfactor | }

12 FILLFACTOR Specifies a percentage to indicate how full SQL Server should make the leaf level of each index page during index creation. When an index page fills up, SQL Server must take time to split the index page to make room for new rows.

13 What Fillfactor to set If data is volatile, FILLFACTOR needs to be low If data is static, FILLFACTOR can be high The default is a high fillfactor CREATE INDEX index_name ON table_name (col_name) WITH FILLFACTOR = 50 Reduces page splitting Double the size of the database with a clustered index 

14 Which of the following fillfactors would be 90% and which would be 60%?

15 A. High fill factor Very soon the leaf page will have to be split as more data is entered into the table

16 B. Low fill factor The leaf level has plenty of space to take more data. Performance will not be reduced due to lots of page splitting The index will take up almost twice the disk space 

17 PAD_INDEX Used in conjunction with FILLFACTOR CREATE INDEX index_name ON table_name (col_name) WITH FILLFACTOR = 50, PAD_INDEX This gives the root page and intermediate pages in the index the same amount of free space when the index is created. Reduces page splitting

18 Which columns? What type of index? –Range queries (WHERE cus_name BETWEEN ‘A%’ and ‘D%’) Cus IDcus_name 3456Anderson 3467Archer 6789Batty 2345Brearley 7659Carter 7654Denton 1222Ellis CLUSTERED on cus-name Table data is in alphabetical order

19 Which columns? What type of index? –Wildcard queries (WHERE customer_name LIKE ‘ch%’) CusNoName 1253Charr 6788Charter 1222Chester 7890Chett 2222Chippen CLUSTERED Index on Name column

20 Which columns? What type of index? an exact match query that uses the WHERE clause to return a unique record eg select empname where NINO = 12 And another query that lists employees in alphabetical order PayroleNoEmpnameNINO 134LECCinderey12 UNIQUE, NONCLUSTERED index on NINO. SqlServer stops looking after it has found one record Clustered (not unique) index on Empname

21 Deciding which columns to index Columns which are used in table joins CLUSTERED (WILL SORT ON THE JOIN COLUMN) but if there is already a clustered index use a non-clustered index.

22 You’ve already created indexes Creating a primary key automatically creates a unique index

23 This is the end of the lecture. Any Questions? Use the forum or bring the questions to class


Download ppt "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."

Similar presentations


Ads by Google