snippetModerate
How do I alter index rebuild an entire database?
Viewed 0 times
entirerebuilddatabasehowindexalter
Problem
I want to reorganize the indexes of all tables in my SQL Server 2008 database, named 'db'. I try the following command:
And I get the following error:
Cannot find the object "db" because it does not exist or you do not have permissions
What am I doing wrong?
ALTER INDEX all ON db REBUILDAnd I get the following error:
Cannot find the object "db" because it does not exist or you do not have permissions
What am I doing wrong?
Solution
To do all tables:
There is no statement to do all tables in the database like you tried above
However, sometimes you don't want this, say for read only tables or huge tables. Also, some tables/indexes will hardly be fragmented.
A more intelligent way is to check fragmentation first then rebuild or reorganise. One common and widely used example is SQL Fool's script
EXEC sp_MSForEachTable 'ALTER INDEX ALL ON ? REBUILD'There is no statement to do all tables in the database like you tried above
However, sometimes you don't want this, say for read only tables or huge tables. Also, some tables/indexes will hardly be fragmented.
A more intelligent way is to check fragmentation first then rebuild or reorganise. One common and widely used example is SQL Fool's script
Code Snippets
EXEC sp_MSForEachTable 'ALTER INDEX ALL ON ? REBUILD'Context
StackExchange Database Administrators Q#2456, answer score: 14
Revisions (0)
No revisions yet.