Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

SQL203 — Indexes can help|hurt

Posted on March 21, 2012March 21, 2012 by slowder

Yeah, I used a pipe in my title.  That’s because indexes can help some things, but hurt others.  The brief of it is indexing helps reads, but comes at a cost: your inserts, updates and deletes are going to take longer.  That’s because as you change your data, you’re going to have to update the index too.  Let’s dive into a scenario I was working on a few weeks ago.

Our “Bad” Table

badTableSo we had this table in our database that supports a high profile product from a company we all know.  We’re currently rolling out the largest instance of this product in the world.  So we’re pushing a lot more data into the system than any other users of this product.  So we expected to find places where the use of the table was greater than the table designers anticipated.

During several of our test deploys we discovered deadlocking on one process.  So we began to dig in.  We found that this one table had a lock that was being held whenever a user or process was accessing the system during a deploy.  This lock was caused by the fact our IO, although on a high end SAN, still couldn’t keep up with our usage (or misuse as we would soon discover).

After a little digging we narrowed the problem down to this one table.  whenever we deploy we have to read this table, find the object ids that are being updated during the deploy, and write some changes to this table.

Straightforward, right?

Well, not so much.  It turns out that this table has over 100 columns, and 42 non clustered indexes.

I was shocked.  How could a product from this company have a table like this?  So I started looking at the index statistics.

SELECT o.name Object_Name,
   i.name Index_name,i.Type_Desc, s.*
FROM sys.objects AS o
JOIN sys.indexes AS i
   ON o.object_id = i.object_id
LEFT OUTER JOIN sys.dm_db_index_usage_stats AS s
   ON i.object_id = s.object_id  AND i.index_id = s.index_id
WHERE
   o.name ='badTable'

Yeah, we weren’t using most of these indexes.  We used the clustered for our bookmark lookups, and a couple of the non-clustered indexes for seeks, but most of them had no reads, seeks, or scans.  But they all had remarkable update costs.

So during one of our meetings with the vendor about our deploy problem we disclose our findings.  They listen to us patiently and take notes on all we’ve found.  They take it back to their engineers and do some research of their own.  After a week, they present us with a fix.  Add 14 more indexes.

Really!?

We’re holding off on implementing these indexes until we can have another discussion about this issue, but it does bring up something you really must know if you’re going to architect database solutions.  You really have to understand that indexes will only speed up your reads.  They can change table scans into index seeks.  If you add a covering index you can even eliminate lookups.  But they come at a cost.

Your writes are going to take longer because you’re also going to have to write to the indexes.  So in our case, rather than updating just the table, you’re updating up to 42 non clustered indexes.  It depends on what columns you’re changing in the update.  You have to update each index that covers the value you changed.  This cost can become so high, you end up with deadlocks.

When you start designing indexes, look at the workload.  Don’t just throw indexes at a problem.  And for all that is good and just in the world, please, take anything the Database Engine Tuning Advisor says and test it before you send it out as a good suggestion.  It only looks at the one query you send it, not at your entire workload.  The DMV’s will help you look at that entire workload.

—

If you’re struggling with indexing, or anything else in SQL Server, let me know.  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