Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
Stueyd
Stueyd
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 255
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!



Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3046 Visits: 5478
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
NuNn DaddY
NuNn DaddY
Old Hand
Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)Old Hand (343 reputation)

Group: General Forum Members
Points: 343 Visits: 481
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. :-D
scogeb
scogeb
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 420
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.
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3264 Visits: 11771
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?
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3264 Visits: 11771
If you don’t have a clustered index on a table, you can have serious problems with empty pages that never get de-allocated. I once found a heap table that had 125 small rows, but was using 16 GB of storage. Performance was horrible when the query had to scan the table.

Another reason to have a clustered index is to reduce the space used by the table. If you don’t have a clustered index, each index will usually consume more space.
Stueyd
Stueyd
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 255
Michael Valentine Jones (11/2/2012)
If you don’t have a clustered index on a table, you can have serious problems with empty pages that never get de-allocated. I once found a heap table that had 125 small rows, but was using 16 GB of storage. Performance was horrible when the query had to scan the table.

Another reason to have a clustered index is to reduce the space used by the table. If you don’t have a clustered index, each index will usually consume more space.



Yeah, but you can get around that by doing your deletes with tablock ;-)

Don't get me wrong, i'm all in favour of clustered indexes, it just seemed to me that there would be a point on a small table (caveats of data, usage, etc...) at which the clustered index wouldn't be of benefit and I wondered if there was a rule of thumb and judging by the response there isn't other than "always use a clustered index".



Gazareth
Gazareth
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2873 Visits: 5359
Seems that clustered vs heap consideration is more a product of usage rather than size.
I also suspect that any difference in overheads/benefits/etc on small tables would be so minuscule as to be irrelevant.
scogeb
scogeb
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 420
Michael Valentine Jones (11/2/2012)
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?





Some kind of log table. A table that would only be used on a very rare occaision, and hopefully never, but the data would be there if needed.

Say you have a table like that, that is constantly getting inserts, but you many never read from it. What would be the benefit of a clustered index? I see more harm then good.
CapnHector
CapnHector
SSC Eights!
SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)SSC Eights! (917 reputation)

Group: General Forum Members
Points: 917 Visits: 1789
One thing to note is that sql server defaults to a clustered primary key. If the tables have a Primary key they most likely (Note: not always and certainly could have been created with a nonclustered primary key) are clustered on that key if they have no other indexes. i know when im creating tables i just declare a primary key and let sql server use it as its clustering key unless i know what data will be going into the table and that a different column will make a better clustering key.


EDIT: After posting and then looking at sys.indexes after creating a test table the primary key index is shown as clustered. so depends on where you looked as to the validity of my comment to the OP's situation.


For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden for the best way to ask your question.

For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw

Need to Split some strings? Jeff Moden's DelimitedSplit8K
Jeff Moden's Cross tab and Pivots Part 1
Jeff Moden's Cross tab and Pivots Part 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search