Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Understanding and creating indexes

From: SQL Server 2008 Essential Training

Video: Understanding and creating indexes

Flip to the back of any thick technical book and you're going to find an index. If you need to look up some content that's buried somewhere in a thousand pages of text, you're going to turn to the back of the book, scan through the index to find what you're looking for, say deviation analysis, realize it's on page 322 and then turn directly to that section to read that content. Indexes give you a quick way to look something up. That's what they do in books and that's what they do in databases. When we're working with SQL Server 2008, we can create two kinds of index.

Understanding and creating indexes

Flip to the back of any thick technical book and you're going to find an index. If you need to look up some content that's buried somewhere in a thousand pages of text, you're going to turn to the back of the book, scan through the index to find what you're looking for, say deviation analysis, realize it's on page 322 and then turn directly to that section to read that content. Indexes give you a quick way to look something up. That's what they do in books and that's what they do in databases. When we're working with SQL Server 2008, we can create two kinds of index.

The first is what's called a clustered index. A clustered index is applied to one of the columns in our table and it will order our table based on that index. In this case I'm looking at it as the CustomerID in ascending order. If for example I remove the clustered index from that column and applied it to the LastName column, it would require that the data itself be reorganized internally based on that clustered index. So this method of indexing will actually have an effect on the internal structure of your tables.

It'll order it internally based on the index. Now because of that, you can only ever have one clustered index per table. We can't actually order our data by both last name and customer ID at the same time. Now because of this, it is actually quite common that it's your primary key and in fact when you apply a primary key to a new table it will cluster that by default. And it's a pretty good idea to have your primary key be the clustered index. You'll be using that key to directly access those rows.

It's a very fast way to work with your content. So in this example that I'm looking at, I almost certainly wouldn't create the clustered index on the last name. I'd leave it on the CustomerID. And in that situation we might be in a place where we would use two kinds of indexes. We go back to the idea that our CustomerID was our clustered index, but we know that we also want to directly look up people by say last name or company name. It's all really dependent on what the needs of your business are. So we'll create a non-clustered index, and we'll do that on the LastName column.

What we'll do is scan through the LastName column and create its own index, a separate index that points to the different places in our table. So if we now have a non-clustered index based on LastName, and we're looking for the last name of Beck, I can scan quickly through that index. Find the entry that will actually point me to the row 16. And if you are writing SELECT statements that need to select again say in this case last name or company name, you'd want to think long and hard about do I need a non-clustered index? Because that will improve the performance of the selects of queries, of joins, with all my retrieval statements.

Non-clustered indexes than are a separate index based on a column. You can actually add multiple, so you can have several non-clustered indexes per table. Now you might be thinking well, why I don't just make everything a non-clustered index, why don't I index every single part of every single table of my database? Well here is the issue. Because you're storing that in separate indexes, if you insert a new row into that table it's going to cause an extra right per index. If you have 10 indexes and insert one row, it's going to have to update those indexes 10 times.

So, well it can improve the performance of your SELECT statements. It can certainly have a detrimental effect to your INSERTs, to your UPDATEs, to your DELETEs. We want to use non-clustered indexes when we know that we're going to be searching on that column, using that column to look something up, using it in a where clause or in ordered by, because it will help to those queries. So how do we make one? It's actually very simple. We drill down into our database and find the table that we're looking for.

Let's say for example we're working with the Customer table here. Now I'm working with a very small database that only has 847 rows. But let's say I was working with one with 100,000, 200,000, two million rows. I'm going to leave the CustomerID as the clustered index, because it's a great one to have, we use the CustomerID a lot. But I might want to ask how we can improve the performance of SELECT statements that go against the LastName.

Oh, here is how I do it. I'm going to right-click Customer and jump into Design. Well it doesn't become immediately apparent, the best way to look at your indexes is right click in the blank area here and select the Indexes/Keys section. And one that is actually telling me is that I have a couple of indexes already there. I've got the PK_Customer_customerID, PK representing not surprisingly primary key. So our primary key index is on CustomerID ascending and I can see down here it's created as clustered.

And that would have been the case when this table was first defined. There's also an extra index here on EmailAddress, so I've got the IX, which tends to represent index. It is non-clustered. Again it can't be. We can't change this to clustered, because we've already got a clustered index. But somebody has obviously indexed EmailAddress if they think they're going to use that, which is a pretty good idea. And an EmailAddress is a very good exact match. Well, I'm going to add one for the LastName. Click Add and we get IX_Customer something.

What we need to do first of is say what column that we want the index to be on. I'll click the ellipsis. It's not going to be CustomerID. It's going to be a LastName and Ascending is fine. Click OK. Doesn't have to be unique. That's fine. We would expect that in a larger or even a small company we would have multiple last names, and the type is an index. We can't just say to unique key. Nope, that's not what we're after. It's just index. So give it a name by standard. It's the table name and the field name, so I'll just call this IX_Customer_LastName.

And that's pretty much it. Close. This is still considered an unsaved change to this table. So I'm going to save this now, and we now have a non-clustered index added to our Customer table.

Show transcript

This video is part of

Image for SQL Server 2008 Essential Training
SQL Server 2008 Essential Training

74 video lessons · 36356 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 2m 21s
    1. Welcome
      1m 19s
    2. Using the exercise files
      1m 2s
  2. 17m 58s
    1. SQL Server core concepts
      9m 4s
    2. SQL Server editions
      3m 8s
    3. Applications included with SQL Server
      5m 46s
  3. 26m 1s
    1. Preparing for installation
      3m 44s
    2. Creating service accounts
      2m 33s
    3. Installing SQL Server
      11m 42s
    4. Post-installation checks
      3m 9s
    5. Installing sample databases
      4m 53s
  4. 13m 35s
    1. Introduction to SQL Server Management Studio
      8m 7s
    2. Introduction to SQL Server Books Online
      3m 6s
    3. SQL Server system databases
      2m 22s
  5. 1h 26m
    1. Planning your database
      9m 39s
    2. Creating a SQL Server database
      4m 7s
    3. Creating tables
      7m 51s
    4. Data types in SQL Server
      12m 25s
    5. Defining keys
      8m 9s
    6. Creating default values
      4m 39s
    7. Creating check constraints
      2m 25s
    8. Creating unique constraints
      4m 34s
    9. Introduction to relationships and foreign keys
      9m 51s
    10. Creating relationships in SQL Server Management Studio
      8m 14s
    11. Database normalization
      11m 47s
    12. Creating computed columns
      3m 10s
  6. 23m 11s
    1. Using the SQL Server Import and Export Wizard
      3m 58s
    2. Importing Excel files into SQL Server
      6m 11s
    3. Importing CSV files into SQL Server
      5m 27s
    4. Importing Access databases into SQL Server
      7m 35s
  7. 55m 29s
    1. Introduction to Transact-SQL
      3m 43s
    2. Using SELECT statements
      7m 16s
    3. Changing the default database
      2m 21s
    4. Creating conditions in SQL
      8m 10s
    5. Sorting your output
      3m 23s
    6. Using aggregate functions
      7m 12s
    7. Finding unique values
      2m 14s
    8. Joining multiple tables together
      8m 0s
    9. Using subqueries
      9m 33s
    10. Viewing execution plans
      3m 37s
  8. 19m 36s
    1. Writing INSERT statements
      5m 47s
    2. Writing UPDATE statements
      4m 38s
    3. Writing DELETE statements
      2m 54s
    4. Using the OUTPUT clause to return inserted keys and GUIDs
      6m 17s
  9. 32m 52s
    1. Introduction to SQL functions
      6m 26s
    2. Using SQL configuration functions
      2m 14s
    3. Using string functions
      7m 26s
    4. Using date functions
      6m 27s
    5. Creating user-defined functions
      10m 19s
  10. 28m 46s
    1. Introduction to stored procedures
      4m 23s
    2. Creating stored procedures
      11m 23s
    3. Introducing transactions
      4m 23s
    4. Creating transactions
      8m 37s
  11. 16m 39s
    1. Understanding and creating indexes
      6m 32s
    2. Monitoring and rebuilding indexes
      6m 0s
    3. Monitoring database size and integrity
      4m 7s
  12. 11m 41s
    1. Creating backups
      4m 21s
    2. Creating differential backups and using backup compression
      3m 40s
    3. Restoring databases
      3m 40s
  13. 17m 40s
    1. Introduction to SQL Server security and permissions
      5m 54s
    2. Adding a Windows user to the database
      5m 7s
    3. Creating SQL Server logins and switching authentication modes
      6m 39s
  14. 36m 41s
    1. Introduction to SQL Server Reporting Services
      2m 52s
    2. Connecting to the Report Manager
      4m 29s
    3. Using Report Builder
      12m 4s
    4. Formatting values in reports
      4m 17s
    5. Adding indicators to reports
      5m 11s
    6. Adding charts to reports
      3m 54s
    7. Working with report security
      3m 54s
  15. 24m 41s
    1. Introduction to SQL Server Integration Services (SSIS)
      1m 57s
    2. Using Business Intelligence Development Studio (BIDS)
      6m 59s
    3. Creating and executing a simple SSIS package
      7m 35s
    4. Importing packages into SQL Server Management Studio
      3m 21s
    5. Scheduling jobs with SQL Server Agent
      4m 49s
  16. 31s
    1. Goodbye
      31s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed SQL Server 2008 Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.