Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

Incomplete Tables

Posted on December 12, 2005 by slowder

One of the things I’ve noticed while performance tuning is many of the tables in my current environment are incomplete.  They all have columns defined, and they all have data, but they’re still missing something.

Many are missing a primary key.  You don’t always have to have a primary key, but when you’re joining against this table day in and day out, you would do well to have a primary key on the column or columns you’re joining on.  If not that, then at least declare a unique constraint.  That way you won’t get duplicates when you join to that table. 

But then we find something else is missing.  Those same tables are also missing a unique constraint.  Well, other than the dupe problems this isn’t a huge problem unless you’ve written indexes on these tables and there isn’t a clustered index on the table.

…Don’t tell me.  That’s missing too?

Ok, time to dig into this problem and start working some magic.  Let’s see how many tables are missing primary keys.

   1: SELECT soT.name

   2: FROM sys.objects soT

   3: LEFT JOIN sys.objects soPK

   4:     ON soT.object_id = soPK.parent_object_id

   5:     AND soPK.[type] = 'PK'  -- Primary Key

   6: WHERE 

   7:     soT.is_ms_shipped = 0  -- not from Microsoft

   8:     AND soT.[type] = 'U'  -- User Table

   9:     AND soPK.object_id IS NULL

  10: ORDER BY

  11:     soT.name

This query looks in the sys.objects table.  Check out the books on line article for more details.  But basically this system view lets you see information about all the objects created within a database.  That means you have to run this in a specific database.  I suggest AdventureWorks if you’re working on a test instance of SQL 2005.

I want you to notice I’ve limited my results to only object created by users (is_ms_shipped = 0), and I only wanted to see the tables that are missing a primary key.  Let’s not get into indexing views at this point.

When you run this query you could take the list of tables, and then manually inspect them in your copy of SQL Server Management Studio (SSMS), but rather than doing that, let’s use another query, to look to see how many of these tables are also missing a unique constraint.

   1: SELECT soT.name

   2: FROM sys.objects soT

   3: LEFT JOIN sys.objects soPK

   4:     ON soT.object_id = soPK.parent_object_id

   5:     AND soPK.[type] = 'PK'  -- primary key

   6: LEFT JOIN sys.objects soUQ

   7:     ON soT.object_id = soUQ.parent_object_id

   8:     AND soUQ.[type] = 'UQ' -- unique constraint

   9: WHERE 

  10:     soT.is_ms_shipped = 0 -- not from microsoft

  11:     AND soT.[type] = 'U' -- user tables only

  12:     AND soPK.object_id IS NULL

  13:     AND soUQ.object_id IS NULL

  14: ORDER BY

  15:     soT.name

In my results, I found that all the tables returned by the first query were also returned by the second query.  That means there is no way defined to uniquely identify a single row.  That’s a problem.  What I would do at this point is talk to the developers using these tables, If there were a DBA, I’d also invite him or her into a conversation.

I’d want to see if there is anything about the records in these tables that would uniquely identify each row to the table.  If there isn’t I’d suggest an IDENTITY column for the tables.  I would then mark that as my Primary Key.  Before adding it to the table, I would definitely talk through the impact of this change with the others.  Everyone needs to be on board with this change.  Even though it will make some things better, if the application isn’t ready for this change…bad things could happen.

But before I could add this column as a Primary Key, I’d want to make sure these tables didn’t already have a clustered index.  If they did have a clustered index, I’d ask if we could make that the Primary Key.  The following query would show me those tables that don’t have a Primary Key, a Unique Constraint, or  a Clustered Index.  The following query uses sys.indexes.  If you want more information about this query, please see the books on line.

   1: SELECT soT.name

   2: FROM sys.objects soT

   3: LEFT JOIN sys.objects soPK

   4:     ON soT.object_id = soPK.parent_object_id

   5:     AND soPK.[type] = 'PK' --primary key

   6: LEFT JOIN sys.objects soUQ

   7:     ON soT.object_id = soUQ.parent_object_id

   8:     AND soUQ.[type] = 'UQ' --unique constraint

   9: LEFT JOIN sys.indexes si

  10:     ON soT.object_ID = si.object_ID

  11:     AND si.[type] = 1 --clustered index

  12: WHERE 

  13:     soT.is_ms_shipped = 0  -- not from Microsoft

  14:     AND soT.[type] = 'U' -- a user table

  15:     AND soPK.object_id IS NULL

  16:     AND soUQ.object_id IS NULL

  17:     AND si.object_id IS NULL

  18: ORDER BY

  19:     soT.name

Those in this final list would need to be treated first.  Especially if they are high transaction volume tables.  By completing these tables you could see tremendous gains in performance.  I will put out one warning here…If these tables are written to frequently but never queried from… you could ignore them… Adding indexes could slow down the write with no real benefit.

Make sure you are reading from these tables before you go adding indexes, especially clustered indexes.

Hopefully this helps you out when you start looking at the state of your databases.  If you have any questions, please let me know!

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