0

I have a question regarding an issue I am having.

I have a table that has 2 indexes - a clustered index and one non-clustered index.

  • The clustered index key column is of type INT
  • The non-clustered is on a varchar(80) column

After I insert rows to the table, when I check the index stats, I see that the non-clustered index is bigger than the clustered index in terms of size.

When I do an index rebuild, it gets smaller.

Then when I add more rows, it gets bigger again. Why is that?

Note that it's a new table with 0 deletes, is it fragmentation? Is it because of the index key types?

I set fill factor to 0 to reduce fragmentation

5
  • 2
    It's unsurprising the clustered index is larger, it contains every column in the table, while a non clustered index will only contain the columns it's ordered on and are explicitly INCLUDEd. Commented May 1, 2024 at 13:39
  • Are you using the default DATA_COMPRESSION? Commented May 1, 2024 at 13:43
  • sorry for sorry for the typo, its the other way around, the non clustered is bigger then the clustered, after a Rebuild of the non clustered index it gets normal. and no not using DATA_COMPRESSION Commented May 1, 2024 at 14:12
  • "when I check the index stats" - can you include the means by which you're doing this in your question? Ideally a query of some sort but if it's through a GUI (like SSMS), that's better than nothing. Commented May 1, 2024 at 14:43
  • Hello Ben, I Use Brent Ozars sp_BlitzIndex wich gives me the stats but also: SELECT i.[name] AS IndexName ,SUM(s.[used_page_count]) * 8 / 1024.0 AS IndexSizeMB FROM sys.dm_db_partition_stats AS s INNER JOIN sys.indexes AS i ON s.[object_id] = i.[object_id] AND s.[index_id] = i.[index_id] WHERE i.[name] IN ('Index_Name Here', 'Index_Name Here') GROUP BY i.[name] Commented May 2, 2024 at 6:55

1 Answer 1

1

see that the non-clustered index is bigger than the clustered index in terms of size. ... When I do an index rebuild, it gets smaller.

Probably fragmentation driven by page splits as the nonclustered index gets inserts at many different insert points in the index key ordering.

These will cause leaf-level page splits, causing many or all of the leaf pages to be only a little over 50% full.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you David, is there any way of reducing the space so the difference wont be so high other then an Index other then rebuild? I tried lowering the fill factor but it doesn't make a big difference
Not really. If you rebuild it, it will just re-fragment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.