Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

How many rows in a table before using a clustered index? Expand / Collapse
Author
Message
Posted Friday, November 2, 2012 10:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 7, 2014 9:04 AM
Points: 32, Visits: 214
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!



Post #1380487
Posted Friday, November 2, 2012 10:21 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1380494
Posted Friday, November 2, 2012 10:28 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, May 30, 2014 8:23 AM
Points: 336, Visits: 388
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.
Post #1380498
Posted Friday, November 2, 2012 10:41 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 9:35 AM
Points: 65, Visits: 396
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.
Post #1380504
Posted Friday, November 2, 2012 10:49 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 9:00 PM
Points: 3,122, Visits: 11,409
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?



Post #1380510
Posted Friday, November 2, 2012 10:56 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 9:00 PM
Points: 3,122, Visits: 11,409
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.


Post #1380511
Posted Friday, November 2, 2012 11:10 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, July 7, 2014 9:04 AM
Points: 32, Visits: 214
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".



Post #1380518
Posted Friday, November 2, 2012 11:20 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 8:48 AM
Points: 1,872, Visits: 2,999
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.
Post #1380524
Posted Friday, November 2, 2012 11:20 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 9:35 AM
Points: 65, Visits: 396
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.
Post #1380525
Posted Friday, November 2, 2012 11:24 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Sunday, July 20, 2014 7:16 AM
Points: 945, Visits: 1,769
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

Jeremy Oursler
Post #1380527
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse