Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

SQL103 – Designing Indexes

Posted on October 12, 2011October 14, 2011 by slowder

So you’ve designed your tables, then you set up your primary and foreign keys.  You even defined views that your programmers will use to present the data to end users.  What’s next?  Well, if you want to take a proactive approach to database design, you’re going to want to start thinking about indexing.

Initial Indexes

When you start putting your views together you have an idea on how they’re going to be used.  Let’s take our contact card view.  We know that to produce the view we’re going to have to do a join between the Customer table and the phone number table (several times). We want to make sure that join performs as quickly as possible.  So we’re going to want to index those key columns to ensure that performance.

Does everyone know the two primary types of indexes?

 

Clustered and Non-Clustered Indexes

Clustered indexes are the physical order of the data in the table.  Since you’re physically ordering the data, you can only have one clustered index.

On the other hand, non-clustered indexes are written separately from the table, and are more like what you think of when you’re talking about an index in a book.  It’s a list of references, along with a page number where you can find that term.  Since non-clustered indexes are written separate from the table, you can have as many of these as you’d like (within reason!)

Since the non-clustered points back to the table, you’re going to want to have a clustered index on the table before defining any non-clustered indexes.  You don’t have to have the clustered index first, but for performance, you want to try out any of your designs with the clustered index rather than trying it without.  Trust me on this one.

 

Let’s get started designing some indexes

The good news is (unless you created your primary key in a non-default way) you’ve already built an index on the primary key of  both tables.  By default SQL Server sets your primary key to be the clustered index on the table.  So you’ve already designed indexes!

If you had any tables where you don’t have a primary key defined, it can be tough judging which column or columns you want to set as your clustered index.  In these cases I usually will wait to see queries being run against the table before attempting to define the clustered index.  But there are some good rules of thumb for designing clustered indexes:

  1. You want to optimize your joins.  If you’re joining to a table on a specific column a lot, and you don’t have a clustered index, that column may be a good candidate for a clustered index.
  2. You want to optimize your where clauses.  If you include a column in a where clause quite often, and you don’t have a clustered index, and you don’t join against a certain column very often, you may have a good candidate for a clustered index.

 

Now, for non-clustered indexes

Let’s say we get word we’re going to have to add Address to vContactCard.

We know that we’re joining the Customer table to the Address table.  We also know that we’ll join on CustomerID.  Since that column is a foreign key in the Address table, and the address table already has a clustered index on AddressID, we have a candidate for a non-clustered index!

So in our design, we’re going to make index on CustomerID in the Address table.

If we wanted to speed up lookups in the Customer table, and we’re doing all our lookups by LastName, then we could add a non-clustered index on LastName in our Customer table.

 

Just don’t go crazy with indexing

Before you co adding non-clustered indexes on every column in your tables, know this: adding indexes can speed up reads, but they have a cost.  They slow down your writes (inserts and updates).  As a designer, you have to keep in mind this balance.  That’s one of the reason I will often design my database solutions only defining the clustered indexes.  I wait for testing and application development before adding non-clustered indexes.

I want the actual use of the tables to show me where the indexes are needed.  It’s served me well so far.

Well, if you have any questions on indexes in database design, send them in…I’m here to help!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • A New File Interrogator
  • Using Generative AI in Data Engineering
  • Getting started with Microsoft Fabric
  • Docker-based Spark
  • Network Infrastructure Updates

Recent Comments

  1. slowder on Data Engineering for Databricks
  2. Alex Ott on Data Engineering for Databricks

Archives

  • July 2023
  • June 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • October 2018
  • August 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • August 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • November 2004
  • September 2004
  • August 2004
  • July 2004
  • April 2004
  • March 2004
  • June 2002

Categories

  • Career Development
  • Data Engineering
  • Data Science
  • Infrastructure
  • Microsoft SQL
  • Modern Data Estate
  • Personal
  • Random Technology
  • uncategorized
© 2025 shannonlowder.com | Powered by Minimalist Blog WordPress Theme