views:

119

answers:

3

I have a database in production with one table that has grown extremely large (lots of accumulated data).

To improve query performance I used the sql server optimizer which suggested a new index.

So I made a copy of the production database to test against and it does improve performance, however my problem is that it took about 24 hours to create the index and while the index is being created the application is unusable.

For this particular application, being down for a few hours is not a problem but a 24 hour downtime would be and I am looking for a way to create this index without having to do that.

I only have a few ideas at the moment.

One idea is to copy a backup to another server. Apply the new index and any other changes. Copy the backup back to the production server. Take the application down and merge over any new data since when I took the backup.

Of course this has its own set of problems like having to merge the data back together so I don't like this idea for that reason.

This is SQL Server 2008 Standard Ed.

I normally deploy database changes by script.

UPDATE: Another idea would be to move the archive data out of the main table over several days in chunks. Then create the index when the table got small enough. Then slowly migrate the data back.

A: 

If you were using Enterprise, you could use the ONLINE option of CREATE INDEX that builds the index without keeping long-term locks on the table. There are caveats around its use; see the linked article for details, and you might find performance impact to be too great. But it's academic as you've said you're using standard (sorry for missing that at first).

The fact it's a VM immediately makes one think in terms of temporarily "pumping up" the VM or even temporarily relocating to a maxed-out non-VM. For rebuilding an index on a very large table, I'd think RAM and I/O speed would be the biggest factors; is the VM using a drive directly or a virtualized drive? Can you temporarily relocate the data to a physical drive? That sort of thing.

FWIW, your take-it-offline-and-do-it idea is exactly what I'd do on a MySQL database (never had to on an SQL Server database): Take the main DB down, grab a snapshot, clear the binlogs/enable binlogging, and fire it back up. Make the index on a separate machine. When ready, take the DB down, make a backup of the updated DB (just in case), put back the snapshot, apply the binlogs, and bring the DB back up. It really is that easy; I expect you can do that with SQL Server as well. Of course, it does assume that you can apply 24 hours of binlogs against the (newly optimized) table within an acceptable time window!

T.J. Crowder
I think the online option is only available on enterprise ed. Also I think it only allows read access while it is creating the index so we wouldn't be able to record transactions for 24+ hours. Correct me if I am wrong, though.
Zack
The ONLINE option is a good suggestion - unfortunately it's not available in Standard Edition. This is one of the features I'm always wanting to be in Standard!
AdaTheDev
@Zack: Ah, sorry, didn't realize it was enterprise only (er, despite the big note in the article highlighted in yellow!). You're mistaken about query-only, though; the linked article clearly says it allows updates.
T.J. Crowder
+1  A: 

Another approach can be not to implement the indexes on all the tables suggested by the sql server optimizer, rather first implement this on one table or group of tables. As you mentioned that a few hours downtime is OK, so using these few hours plan out the various tables on which the indexing needs to be done. Now daily select those tables whose indexes can be build in the given downtime. Working smartly can easily resolve this problem.

Same scenario came up to us where we could get only 1 hr downtime daily and we made the same approach and within 9 days the new indexes were made and the downtime too was used effectively.

Hope this helps ...

Yes this can be one approach and is more practical.
HotTester
Thats a good idea, but in my case I am only creating one new index on one table.
Zack
It's a good idea, but I read the question as "the index" -- e.g., just one.
T.J. Crowder
A: 

Given the lack of processing power available on the VM machine, combined with what is no doubt pretty poor IO throughput, I would actually consider calculating the time to backup, restore to a half decent server, index and then backup / restore back to the VM machine.

To avoid the initial backup taking a long time, you could back it up one day and be moving it during the day, and then when the maintenance window starts, backup the transaction log and move that across - on the basis it will be a smaller move. (This assumes bulk / full log mode)

Andrew
This sounds like an interesting idea, although I've never dealt with moving transaction logs between servers before. Log mode is currently set to Simple but nothing is stopping me from changing it(?). Could you give me more information on the steps? Would the idea be to apply the transaction log from production to the newly patched copy that we updated on another server. How would you apply that transaction log? Would the log be ok attaching to a database that has a index that the database it came from did not? Thanks!
Zack
For it to work requires the database to be in bulk or full mode and then a full backup taken to properly start the mode. The database is then restored on the secondary server but not taken out of recovery mode and the transaction log backup can be further applied. This step instead of a straight backup / restore was to avoid a lenghty backup / transfer of backup.The issue is going to be that if you have never done this before, there is a lot of scope to get it wrong.
Andrew