Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-indexing large table - how screwed am I?

I have a 1 TB, 600m row, table which has a misguided choice of indexed columns, specifically a clustered index on the primary key column which is never used in a select query.

I want to remove the clustered index from this row and create it on a number of other rows.

Table is currently like this:

  • colA (PK, nvarchar(3)) [clustered index pt b]

  • colB (PK, bigint) [clustered index pt a]

  • colC (DateTime) [non-clustered index]

  • colD (Money) [non-clustered index]

  • colE (bit) [no index]

  • colF (bit) [no index]

  • colG (int) [no index]

  • more non-indexed columns

I would like to change it to look like this:

  • colA (PK, nvarchar(3)) [clustered index pt a]

  • colB (PK, bigint) [non-clustered index]

  • colC (DateTime) [non-clustered index]

  • colD (Money) [clustered index pt d]

  • colE (bit) [clustered index pt b]

  • colF (bit) [clustered index pt c]

  • colG (int) [clustered index pt e]

  • more non-indexed columns

Two questions: 1) How long would you guesstimate that this change will take (server spec at end of message). Unfortunately it is a live DB and I can't have downtime without some idea of how long it will be down for.

2) Is it a terrible idea to add so many columns to a clustered index? Updates are nearly never performed. There are many inserts and many selects which always use all of the proposed indexed rows as select parameters.

Server spec: 5 x 15kRPM drives in RAID 5, MS-SQL Sever 2005 and some bits to keep them running.

like image 956
Mr. Flibble Avatar asked Mar 27 '09 16:03

Mr. Flibble


People also ask

How long it will take to create index on a large table?

Creating indexes on a very large table takes over 5 hours.

How many indexes on a table is too many?

To start, I'd say that most tables should have fewer than 15 indexes. In many cases, tables that focus on transaction processing (OLTP) might be in the single digits, whereas tables that are used more for decision support might be well into double digits.

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.

What happens when you have too many indexes on a table?

The more nonclustered indexes you have on a table, the slower your inserts and deletes will go. It's that simple. If you have a table with 10 nonclustered indexes, that's 10x (or more) writes an insert has to do for all of the data pages involved with each index.


1 Answers

For one thing, I would AVOID making the clustered index wider than it absolutely has to be. Making it into five parts seems about contra-productive. Are ALL the columns in this compound clustered index stable, e.g. never change??

If not, I would avoid them at all costs. A clustered index should be:

  • unique
  • stable
  • as narrow as possible

You can change your non-clustered indices - no problem. But avoid making the clustered index messy! That'll definitely bring down your performance!

Check out Kimberly Tripp's excellent blog articles on indexing:

  • main link here
  • best practices for clustering index here

Marc

like image 147
marc_s Avatar answered Oct 27 '22 00:10

marc_s