SQL Index space and unused table space

Hollisorama 381 Reputation points
2021-01-12T17:24:34.027+00:00

I've got a table in a SQL database that has a large unused space that corresponds to the amount of space allocated to indexes

55842-image.png

I was working on this by running a reindex on the specific table, but I'm running into space issues and I'm not sure if my approach is effective.

SQL Server Other
{count} votes

6 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-01-12T22:48:41.84+00:00

    When you rebuilds an index, SQL Server creates a new version of the index elsewhere in the database, so you need the space to fit that new version of the index, even if you will end with more than free space than before.

    The alternative would be to export the data with BCP, truncate the table and reload.

    1 person found this answer helpful.
    0 comments No comments

  2. Hollisorama 381 Reputation points
    2021-01-12T23:30:10.757+00:00

    Am I correct that the unused space is most likely from the index?

    I'm thinking to create a temporary table to load the data, truncate the existing table and then copy the data back. It seems like I will need about 40GB to accomplish that. Does that sound right?

    0 comments No comments

  3. Cris Zhan-MSFT 6,661 Reputation points
    2021-01-13T02:45:19.48+00:00

    Hi @Hollisorama ,

    >I was working on this by running a reindex on the specific table, but I'm running into space issues and I'm not sure if my approach is effective.

    Usually rebuilding the clustered index should be an effective approach. But rebuilding the index requires a certain amount of space.
    Disk space is an important consideration when you create, rebuild, or drop indexes. Inadequate disk space can degrade performance or even cause the index operation to fail. More details please refer to following documents.
    https://learn.microsoft.com/en-us/sql/relational-databases/indexes/disk-space-requirements-for-index-ddl-operations?view=sql-server-ver15
    https://learn.microsoft.com/en-us/sql/relational-databases/indexes/index-disk-space-example?view=sql-server-ver15

    If there are data types such as text or image in your table, index rebuilding will not affect them in some cases. You may need to consider rebuilding objects.
    https://dba.stackexchange.com/questions/52317/freeing-unused-space-sql-server-table

    0 comments No comments

  4. Olaf Helper 47,436 Reputation points
    2021-01-13T07:36:14.177+00:00

    "Unused space" often occurs on "heap tables" = a table without a clustered index; is it a heap table? In that case you have to rebuild the table as it to get rid of the unused space; which is similar to rebuild an index = it requires additional space.

    See also Heaps (Tables without Clustered Indexes)

    0 comments No comments

  5. Hollisorama 381 Reputation points
    2021-01-13T19:56:40.61+00:00

    Here's the indexes on one of the tables

    56336-image.png

    I'm thinking to copy the data to a new table, truncate the existing table, reduce the database file size and then reimport the data. This is a vendor's application so I may reengage them. I had already opened a ticket with them and they said the table usage was normal. But it doesn't seem normal to have a large index size and corresponding free space in the database. Can you confirm that is unusual?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.