Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SSRS 2005 MatrixesDo not use spaces or other invalid characters in your column names »
    comments

    By definition, primary keys must contain unique data. They are implemented in the database through the use of a unique index. If there is not already a clustered index on the table, then the primary key's index will be clustered. It's not always true, but most of the time, you want your primary keys to be clustered because it is usually the key criteria in your requests to the data. This includes join conditions and where clause criteria. Clustered indexes give you exceptional performance because it allows SQL Server to create optimal execution plans.

    How to detect this problem:

    1. Select AllTables.Name
    2. From   (
    3.        Select Name, id
    4.        From   sysobjects
    5.        Where  xtype = 'U'
    6.        ) As AllTables
    7.        Left Join (
    8.          Select parent_obj
    9.          From   sysobjects
    10.          Where  xtype = 'PK'
    11.          ) As PrimaryKeys
    12.          On AllTables.id = PrimaryKeys.parent_obj
    13. Where  PrimaryKeys.Parent_Obj Is NULL
    14. Order By AllTables.Name

    How to correct it: Identify tables without a primary key using the query above. Examine each table and identify what makes each row in the table unique. Modify the table to include a primary key.

    Level of severity: Moderate
    Level of difficulty: Easy

    About the Author

    George has been developing software professionally for 19 years, first for the department of defense, and then for various other companies. In 1998, George started his software company, Orbit Software, specializing in School Bus Transportation software. His specialty is refining SQL Server queries to deliver optimal performance.
    Social SitingsTwitterLTD RSS Feed
    Instapaper

    8 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis I agree with this....can't remember how many times I had to clean up tables and even worse with child tables (but no pk --> fk) running group by having count(*) > 1

    figuring out how to merge the data etc etc

    try deleting a row that has no PK with dups from the GUI
    11/11/09 @ 19:41
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Are there any exceptions to the rule? Say, when would you want to have a heap table?
    And also, when would we have a PK, but no clustered key? What will happen in that situation?
    02/17/11 @ 13:12
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Are you saying it's easy to add a new clustered primary key on a huge table?
    08/23/11 @ 11:10
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) Naomi,

    I'm saying it is easy to detect which tables to not have primary keys, and relatively easy to add them. All of the rules change when you are working with huge tables. It's still relatively easy, but it could be costly in terms of time, log file usage and disk usage.
    08/23/11 @ 11:27
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky When it's OK to have a heap table? Say, the table is used for auditing purposes - is it OK to leave it as a heap?
    03/14/12 @ 09:17
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) I have plenty of auditing tables in my database. None of them are heaps because at some point I want to do something with them. My auditing tables have a column for AuditDateTime (plus other columns). This works well for auditing tables because rows are never updated and inserted rows always go at the end of the table. This prevents fragmentation and also allows for fast looks on the auditing data.
    03/14/12 @ 09:34
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Thanks. Assuming that table doesn't need to be queried, is having the table as a heap allow for faster insertion or not if the table has a clustered index?
    03/14/12 @ 14:14
    Comment from: George Mastros (gmmastros) [Member]
    George Mastros (gmmastros) You could argue that if the table doesn't need to be queried, then you don't need that table.

    Sounds like you just came up with a good blog post. Test the table insertion with a heap table and then again with a non-heap table. Compare the performance. I suspect that a single clustered index on a table won't make a difference as long as you are using an identity for the primary key or a DateTime + Other column(s). The difference in insert time will be minimal (almost non-existent) in my opinion. The only way to know for sure would be to test it.
    03/14/12 @ 14:30

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)