Reputation: 452
Looking to create a SQL query that rebuilds indexes in SQL on only one table within my database. Can anyone point me in the right direction. Someone earlier suggested Ola Hallengren SQL maintenance, but I think that is too robust for what I'm after.
Upvotes: 24
Views: 78291
Reputation: 144
If the purpose of Rebuilding index is performance then you can first try in these following order on table:
UPDATE STATISTICS mySchema.myTable;
ALTER INDEX ALL ON mySchema.myTable REORGANIZE;
ALTER INDEX ALL ON mySchema.myTable REBUILD;
ALTER INDEX ALL ON mySchema.myTable REBUILD WITH (ONLINE = ON, RESUMABLE = ON, MAX_DURATION = 10);
Notes:
Ref:
Upvotes: 0
Reputation: 1523
There's a difference between REORGANIZE and REBUILD. See this blog post for details.
If you truly want to REBUILD, and you want to do it for ALL indexes on a given table, then the command would be (straight from the official docs that npe pointed you at):
ALTER INDEX ALL ON mySchema.myTable REBUILD
Upvotes: 47
Reputation: 15699
Try this:
ALTER INDEX indexName ON mySchema.myTable REORGANIZE;
For more instructions see the official docs. The link points to SQL Server 2014 docs, but the syntax should work on 2005 and 2008 as well.
Upvotes: 8