For outstanding
technology
results...

Rebuilding indexes

"Want to rebuild all the indexes in your MS SQL database? The script below will rebuild all the indexes in your SQL Server 2008 or SQL Server 2005 (SP2) database and offers a number of enhancements that make this the best SQL Server index rebuild script in all the land and the first choice of SQL Server consultants "

Nice script. http://www.sqldbpros.com/sql-server-rebuild-indexes-the-fastest-way

I’ve only used it once. YMMV. Don’t blame me if it turns your cat into a packet of oreos. Brad McGehee (MVP, Works for Redgate, general all round SQL Server Guru) has some good tips at Rebuilding Indexes There’s also gold at http://benchmarkitconsulting.com/colin-stasiuk/2009/02/11/update-statistics-before-or-after-an-index-rebuild/ regarding index rebuilding.

The comments are pure geekery. Before rebuilding the indexes, you’ll probably want to put the database into single user mode. This’ll speed things up

  1. -- take to single user mode
  2. ALTER DATABASE DBNAME SET SINGLE_USER WITH NO_WAIT
  3. -- do your magic reindexing here
  4. -- let everyone back in
  5. ALTER DATABASE  DBNAMESET MULTI_USER WITH NO_WAIT

  You might also want to kill all the connections that aren’t you. I tend to do this before putting it in single user mode.

  1. use master
  2. go
  3. declare @command varchar(100)
  4. declare @spid int
  5. DECLARE active_users CURSOR LOCAL FOR
  6. SELECT spid FROM master.dbo.sysprocesses
  7. WHERE (dbid = (SELECT dbid FROM master.dbo.sysdatabases WHERE (name = 'DBNAME')))
  8. AND spid <> @@spid
  9. OPEN active_users FETCH NEXT FROM active_users INTO @spid
  10. WHILE (@@fetch_status = 0) BEGIN
  11. SET @command = 'kill ' + cast(@spid as varchar)
  12. print @command
  13. exec(@command)
  14. FETCH NEXT FROM active_users INTO @spid
  15. END
  16. DEALLOCATE active_users
  17. GO
 

Add comment


Security code
Refresh

Make Contact

We provide a free workshop for
  • Your ideas and requirements
  • The process we use
  • Understanding of the costs

  • Call us on +61 3 8352 6222

    Redgum Technologies Pty Ltd on LinkedIn

    Companies
    we have
    worked with: