Effects of Clustered Index on DB Performance

I recently became involved with a new software project which uses SQL Server 2000 for its data storage.

In reviewing the project, I discovered that one of the main tables uses a clustered index on its primary key which consists of four columns:

  • Any hidden pitfalls changing a column from varchar(8000) to varchar(max)?
  • Using sqlcmd with FOR XML returns Invalid Object name 'dbo.Table' yet Query runs in SSMS
  • 2 Databases. If value does not exists in the first one then look/search in the second one
  • SQL Server Object Names
  • get data for per year
  • Entity Framework Migrations renaming tables and columns
  • Sequence  numeric(18, 0)
    Date      datetime
    Client    varchar(9)
    Hash      tinyint

    This table experiences a lot of inserts in the course of normal operation.

    Now, I’m a C++ developer, not a DB Admin, but my first impression of this table design was that that having these fields as a clustered index would be very detrimental to insert performance, since the data would have to be physically reordered on each insert.

    In addition, I can’t really see any benefit to this since one would have to be querying all of these fields frequently to justify the clustered index, right?

    So basically I need some ammunition for when I go to the powers that be to convince them that the table design should be changed.

  • Storing data in a side table that may change in its main table
  • Creating object counters in Entity Framework and Sql Server
  • How to optimise SQL table for read speed for point to point distance data
  • storing weekly targets in database
  • SQL - Best practise for a Friendship table
  • Changing Opening Hours without affecting historic data
  • 5 Solutions collect form web for “Effects of Clustered Index on DB Performance”

    The clustered index should contain the column(s) most queried by to give the greatest chance of seeks or of making a nonclustered index cover all the columns in the query.

    The primary key and the clustered index do not have to be the same. They are both candidate keys, and tables often have more than one such key.

    You said

    In addition, I can’t really see any benefit to this since one would have to be querying all of these fields frequently to justify the clustered index, right?

    That’s not true. A seek can be had just by using the first column or two of the clustered index. It may be a range seek, but it’s still a seek. You don’t have to specify all the columns of it in order to get that benefit. But the order of the columns does matter a lot. If you’re predominantly querying on Client, then the Sequence column is a bad choice as the first in the clustered index. The choice of the second column should be the item that is most queried in conjunction with the first (not by itself). If you find that a second column is queried by itself almost as often as the first column, then a nonclustered index will help.

    As others have said, reducing the number of columns/bytes in the clustered index as much as possible is important.

    It’s too bad that the Sequence is a random value instead of incrementing, but that may not be able to be helped. The answer isn’t to throw in an identity column unless your application can start using it as the primary query condition on this table (unlikely). Now, since you’re stuck with this random Sequence column (presuming it IS the most often queried), let’s look at another of your statements:

    having these fields as a clustered index would be very detrimental to insert performance, since the data would have to be physically reordered on each insert.

    That’s not entirely true.

    The physical location on the disk is not really what we’re talking about here, but it does come into play in terms of fragmentation, which is a performance implication.

    The rows inside each 8k page are not ordered. It’s just that all the rows in each page are less than the next page and more than the previous one. The problem occurs when you insert a row and the page is full: you get a page split. The engine has to copy all the rows after the inserted row to a new page, and this can be expensive. With a random key you’re going to get a lot of page splits. You can ameliorate the problem by using a lower fillfactor when rebuilding the index. You’d have to play with it to get the right number, but 70% or 60% might serve you better than 90%.

    I believe that having datetime as the second CI column could be beneficial, since you’d still be dealing with pages needing to be split between two different Sequence values, but it’s not nearly as bad as if the second column in the CI was also random, since you’d be guaranteed to page split on every insert, where with an ascending value you can get lucky if the row can be added to a page because the next Sequence number starts on the next page.

    Shortening the data types and number of all columns in a table as well as its nonclustered indexes can boost performance too, since more rows per page = fewer page reads per request. Especially if the engine is forced to do a table scan. Moving a bunch of rarely-queried columns to a separate 1-1 table could do wonders for some of your queries.

    Last, there are some design tweaks that could help as well (in my opinion):

    • Change the Sequence column to a bigint to save a byte for every row (8 bytes instead of 9 for the numeric).
    • Use a lookup table for Client with a 4-byte int identity column instead of a varchar(9). This saves 5 bytes per row. If possible, use a smallint (-32768 to 32767) which is 2 bytes, an even greater savings of 7 bytes per row.

    Summary: The CI should start with the column most queried on. Remove any columns from the CI that you can. Shorten columns (bytes) as much as you can. Use a lower fillfactor to mitigate the page splits caused by the random Sequence column (if it has to stay first because of being queried the most).

    Oh, and get your online defragging going. If the table can’t be changed, at least it can be reorganized frequently to keep it in best possible shape. Don’t neglect statistics, either, so the engine can pick appropriate execution plans.


    Another strategy to consider is if the composite key used in the table can be converted to an int, and a lookup table of the values is created. Let’s say some combination of less than all 4 columns is repeated in over 100 rows, for example, Sequence + Client + Hash but only with varying Date values. Then an insert to a separate SequenceClientHash table with an identity column could make sense, because then you could look up the artificial key once and use it over and over again. This would also get your CI to add new rows only on the last page (yay) and substantially reduce the size of the CI as repeated in all nonclustered indexes (yippee). But this would only make sense in certain narrow usage patterns.

    Now, marc_s suggested just adding an additional int identity column as the clustered index. It is possible that this could help by making all the nonclustered indexes get more rows per page, but it all depends on exactly where you want the performance to be, because this would guarantee that every single query on the table would have to use a bookmark lookup and you could never get a table seek.

    About “tons of page splits and bad index fragmentation”: as I already said this can be ameliorated somewhat with a lower fill factor. Also, frequent online index reorganization (not the same as rebuilding) can help reduce the effect of this.

    Ultimately, it all comes down to the exact system and its unique pattern of data access combined with decisions about which parts you want optimized. For some systems, having a slower insert isn’t bad as long as selects are always fast. For others, having consistent but slightly slower select times is more important than having slightly faster but inconsistent select times. For others, the data isn’t really read until it’s pushed to a data warehouse anyway so the inserts need to be as fast as possible. And adding into the mix is the fact that performance isn’t just about user wait time or even query response time but also about server resources especially in the case of massive parallelism, so that total throughput (say, in client responses per time unit) matters more than any other factor.

    Clustered indexes (CI) work best over ever-increasing, narrow, rarely changing values. You’ll want your CI to cover the column(s) that get hit the most often in queries with >=, <=, or BETWEEN statements.

    I’m not sure how your data normally gets hit. Most often you’ll see a CI on an IDENTITY column or another narrow column (because this column will also be returned “tacked on” to all non-clustered indexes, and we don’t want a ton of data added on to every fetch if it isn’t needed). It’s possible the data might be getting queried most often on date, and that may be a good choice, but all four columns is likely not correct (I stress likely, because I don’t know the set-up; this may not have anything wrong with it). There are some pointers here: http://msdn.microsoft.com/en-us/library/aa933131%28SQL.80%29.aspx

    There are a few things you are misunderstanding about how SQL creates and uses indexes.

    Clustered indexes aren’t necessarily physically ordered on disk by the clustered index, at least not in real-time. They are just a logical ordering.

    I wouldn’t expect a major performance hit based on this structure and removing the clustered index before you have actually identified a performance issue related to that index is clearly premature optimization.

    Also, an index can be useful (especially one with several fields in it) even for searches that don’t sort or get queried on all columns included in it.

    Obviously, there should be a justification for creating a multi-part clustered index, just like any index, so it makes sense to ask for that if you think it was added capriciously.

    Bottom line: Don’t optimize the indexes for insert performance until you have actually detected a performance problem with inserts. It usually isn’t worth it.

    If you have only that single clustered index on your table, that might not be too bad. However, the clustering index is also used for looking up the real data page for any hit in a non-clustered index – therefor, the clustered index (all its columns) are also part of each and every non-clustered index you might have on your table.

    So if you have a few nonclustered indices on your table, then you’re definitely a) wasting a lot of space (and not just on disk – also in your server’s RAM!), and b) your performance will be bad.

    A good clustered index ought to be:

    • small (best bet: a 4-byte INT) – yours is pretty bad with up to 28 bytes per entry
    • unique
    • stable (never change)
    • ever-increasing

    I would bet your current setup violates at least two if not more of those requirements. Not following these recommendations will lead to waste of space, and as you rightfully say, lots of page and index fragmentation and page splits (having to “rearrange” the data when an insert happens somewhere in the middle of the clustered index).

    Quite honestly: just add a surrogate ID INT IDENTITY(1,1) to your table and make that the primary clustered key – you should see quite a nice boost in performance, just from that, if you have lots of INSERT (and UPDATE) operations going on!

    See some more background info on what makes a good clustering key, and what is important about them, here:

    • GUIDs as PRIMARY KEYs and/or the clustering key
    • The Clustered Index Debate Continues…
    • Ever-increasing clustering key – the Clustered Index Debate……….again!

    I ultimately agree with Erik’s last paragraph:

    “Ultimately, it all comes down to the exact system and its unique pattern of data access combined with decisions about which parts you want optimized…”

    This is the basic thing I force people to learn: there’s no universal solution.

    You have to know your data and the actions performed against it. You have to know how frequent different type of actions are and their impact and expected execution times (you don’t have to hard tune some rarely executed query and impact everything else if the end user agrees the query execution time is not so important–let’s say waiting for few minutes for some report once per week is okay). Of course, as Erik said

    “performance isn’t just about user wait time or even query response time but also about server resources”

    If such a query affects overall server performance, it should be considered as a serious candidate for optimization, even if execution time is fine. I’ve seen some very fast queries that used huge amount of CPU on multiprocessor servers, while slightly slower solution were incomparable “lighter” from resource utilization point of view. In that case I almost always go for the slower one.

    Once you know what is your goal you can decide how many indexes you need and which one should be clustered. Unique constraints, filtered indexes, indexes with included columns are quite powerful tools for tuning. Choosing proper columns is important, but often choosing proper order of columns is even more important. And at the end, don’t kill insert/update performance with tons of indexes if the table is frequently modified.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.