Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2005: Index bigger than data stored

I created 1 database with 2 file groups: 1 primary and 1 index.

  • Primary file group includes 1 data file (*.mdf): store all tables
  • Index file group includes 1 index file (*.ndf): store all indexes

Most of indexes are non-clustered indexes

After a short time using the database, the data file is 2GB but the index file is 12 GB. I do not know what problem happened in my database.

I have some questions:

  1. How do I reduce the size of the index file?
  2. How do I know what is stored in the index file?
  3. How do I trace all impacts to the index file?
  4. How do I limit size growing of index file?
like image 652
RPS Avatar asked Sep 16 '10 14:09

RPS


People also ask

How do I fix index fragmentation in SQL Server?

Rebuild and Reorganize Index using SQL Server Management Studio (SSMS) Find and expand the table in Object Explorer >> Open Indexes >> Right-click on the target index >> Rebuild or Reorganize.

What is index space and data space in SQL Server?

Data space includes leaf level clustered index pages, since these pages are the data pages. Index space excludes the leaf pages of clustered indexes.

Does indexing increase database size?

Answers. Hi RobNZ, The increase in database size due to the index rebuilt is expected. While index is rebuilt, parallel indexing structure is created and after it is created it switches as new cluster index.

Does indexing reduce table size?

No, number of columns in a table has no bearing on benefits from having an index. An index is solely on the values in the column(s) specified; it's the frequency of the values that will impact how much benefit your queries will see.


2 Answers

How do I reduce size of index file ?

Drop some unneeded indexes or reduce the number of columns in existing ones. Remember that the clustered index column(s) is a "hidden" included column in all non clustered indexes.

If you have an index on a,b,c,d and an index on a,b,c you might consider dropping the second one as the first one covers the second one.

You may also be able to find potential unused indexes by looking at sys.dm_db_index_usage_stats

How to know what is stored in index file?

It will store whatever you defined it to store! The following query will help you tell which indexes are using the most space and for what reason (in row data, lob data)

SELECT  convert(char(8),object_name(i.object_id)) AS table_name, i.name AS index_name, 
    i.index_id, i.type_desc as index_type,
    partition_id, partition_number AS pnum,  rows, 
    allocation_unit_id AS au_id, a.type_desc as page_type_desc, total_pages AS pages
FROM sys.indexes i JOIN sys.partitions p  
      ON i.object_id = p.object_id AND i.index_id = p.index_id
    JOIN sys.allocation_units a
      ON p.partition_id = a.container_id
      order by pages desc
like image 57
Martin Smith Avatar answered Nov 12 '22 05:11

Martin Smith


My guess (which I think is where marc_s is also headed) is that you've declared your clustered indexes for at least some of your tables to be on the index file group. The clustered index determines how (and where) the actual data for your table is stored.

Posting some of your code would certainly help others pinpoint the problem though.

I think that Martin Smith answered your other questions pretty well. I'll just add this... If you want to limit index sizes you need to evaluate your indexes. Don't add indexes just because you think that you might need them. Do testing with realistic (or ideally real-world) loads on the database to see which indexes will actually give you needed boosts to performance. Indexes have costs to them. In addition to the space cost which you're seeing, they also add to the overhead of inserts and updates, which have to keep the indexes in sync. Because of these costs, you should always have a good reason to add an index and you should consciously think about the trade-offs.

like image 34
Tom H Avatar answered Nov 12 '22 05:11

Tom H