I have a mssql database, we'll call it: mssqlDB01. I've been tasked with performing defragmentation on all of the tables. This database has a few hundred tables and each table has a range of 1 to 15 indexes per table.
Google led me to discover a practice to defrag all indexes per table, but I can't figure out how to do it on all tables.
ALTER INDEX ALL ON TABLENAME REBUILD;
what i'm looking for is
ALTER INDEX ALL ON * REBUILD;
but it complains
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '*'.`
below lets me find all tables in my DB
SELECT * FROM information_schema.tables WHERE TABLE_TYPE='BASE TABLE'
can I somehow push this into the command?
ALTER INDEX ALL ON (SELECT * FROM information_schema.tables WHERE TABLE_TYPE='BASE TABLE'; ) REBUILD;
You could probably write a script that uses dynamic SQL to do that, but why do that when you can use someone else's? Ola Hallengren's are the best known and free, but Minion Ware also has a free reindex script.
If you insist on writing it yourself, something like this might work:
Run this query:
Copy the output and paste it into the SQL window, then click on run.
Building on @Firdaus nice and simple answer:
If your database has schemas, try running the following in SSMS:
Normally DBAs create an automated function or have tools to rebuild the indexes.
This one is a functional version to rebuild all indexes to all tables in your schema:
This will rebuild the indexes and keep the compression settings if you have any: