How many rows in a table before using a clustered index?

  • Hi All,

    I've just inherited a database and whilst doing a health check discovered about 300 tables that don't have clustered indexes.

    Now anything with zero rows I will ignore and anything with 20,000 rows and above I will immediately investigate to see about slapping on a clustered index.

    What about the others?

    Is there a "rule of thumb" ? Eg: Any table with more than 1,000 rows could benefit from a clustered index?

    What's the tipping point where there's so few rows it's not worth it?

    Cheers!

  • you should have clustered index on every table .

    that is what they called as best practice.

    data is not organized properly in heap tables (tables without clustered index)

    http://msdn.microsoft.com/en-us/library/hh213609.aspx

    http://technet.microsoft.com/en-us/library/cc917672.aspx

    http://www.simple-talk.com/blogs/2007/12/17/should-you-always-use-a-clustered-index/

    -----------------------------------------------------------------------------
    संकेत कोकणे

  • Thanks for the textbook answer. Not!

    Yeah, that's what it says "in the manual", but the reality is that there is a point below which a Clustered Index is more of an overhead than a help.

    Anyone got a rough rule of thumb for what that point is?

    1,000 rows? 5,000 rows?

  • Stueyd (11/2/2012)


    What's the tipping point where there's so few rows it's not worth it?

    0.

    Every table should have a clustered index unless you have a good reason otherwise (low row count is not a good reason)

    but the reality is that there is a point below which a Clustered Index is more of an overhead than a help.

    No, there isn't.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • This was removed by the editor as SPAM

  • Here is a link to the MSDN for Clustered Index Design Specifications which may help you better understand what columns you should use for your clustered index.

    http://msdn.microsoft.com/en-us/library/ms190639%28v=sql.105%29.aspx

    Also, you may want to consider taking a look at the "Stairway to SQL Server Indexes" which explains both clustered and nonclustered indexes

    http://www.sqlservercentral.com/stairway/72399/

  • Stewart "Arturius" Campbell (11/2/2012)


    Clustered indexes are best suited for those columns most frequently used in range-based searches.

    I would tend to disagree with that, but that's just me.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Me too !!

    -----------------------------------------------------------------------------
    संकेत कोकणे

  • GilaMonster (11/2/2012)


    Stewart "Arturius" Campbell (11/2/2012)


    Clustered indexes are best suited for those columns most frequently used in range-based searches.

    I would tend to disagree with that, but that's just me.

    Not just you.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • I would always have a clustered index. Unless I had a really good reason not to have one. I'd read some of the articles mentioned, especially any by Gail on indexing to help decide what column(s) to use in the index.

    Other indexes may or may not help, depending on the size of the table.

  • Thanks for all your replies.

    Although it would normally be very unusual for me NOT to use a clustered index (yeah, yeah, i've been there, done that, got the t-shirt) I do find it hard to believe there's not a table size where it's actually beneficial not to bother (1 row even?).

    When I get a spare moment i'll jump on a test server and try it out with varying levels of data and see what the execution plan says!

  • GSquared (11/2/2012)


    GilaMonster (11/2/2012)


    Stewart "Arturius" Campbell (11/2/2012)


    Clustered indexes are best suited for those columns most frequently used in range-based searches.

    I would tend to disagree with that, but that's just me.

    Not just you.

    You have quite a community here 🙂

    To OP: Don't waste your time. Every permanent table in database better to have a clustered index. There are many reasons for that.

    Exceptions are very few: some times ETL processing staging table used mainly for inserts of large chunks and then truncated or even, better dropped, may not need clustered index...

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • When there's not a clustered index on a table and a query is performed against it, a full table-scan will be needed as SQL Server has no idea of the order of the rows on the physical data pages. Regardless if all the rows in the result are found on the 1st page, SQL Server is unaware of this as there isn't a specific ordering (clustered index) for how the rows will be physically stored inside the database. Thus, it will check every page before returning a result-set. Here's the wikipedia definition of a full-table scan:

    Full table scan (also known as sequential scan) is a scan made on the database where each row of the table under scan is read in a sequential (serial) order and the columns encountered are checked for the validity of a condition. Full table scans are usually the slowest method of scanning a table due to the heavy amount of I/O reads and writes required from the disk which consists of multiple seeks as well as costly disk to memory transfers. Sequential scan takes place usually when, the column or group of columns of a table (the table may be on disk or may be an intermediate table created by the join of two or more tables) needed for the scan do not contain an index which can be used for the purpose

    By having a clustered index on the table, as rows are inserted they're physically being stored (on the data pages) in order according to the clustered index. For example, when a query is performed on the table that's using the clustered index column within the "where" clause, SQL Server is able to look-up the physical location of each of the rows (similar to an index inside of a book) and know the location(s) of every record that meets the specified criteria. Because of this there's no need to perform a full table scan since SQL Server knows the location of all the records for the clustered index and is able to go directly to the data page(s) where the information is stored.

    I apologize the information above isn't entirely accurate as I'm not well versed in the internals of clustered/nonclustered indexes. As I mentioned in my previous post, the Stairway articles explain this very well and can help with most of the questions on indexes you may have. I hope it helps. 😀

  • I like to think there is always an exception to every rule. Take for instance a table that ALWAYS has inserts and NEVER has any selects. Putting a clustered index on that table would be a waste.

  • scogeb (11/2/2012)


    I like to think there is always an exception to every rule. Take for instance a table that ALWAYS has inserts and NEVER has any selects. Putting a clustered index on that table would be a waste.

    A table that never has any selects is a waste with or without a clustered index. If there are never any selects, what use would the table be since the data isn't used?

Viewing 15 posts - 1 through 15 (of 43 total)

You must be logged in to reply to this topic. Login to reply