Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebuild Index in SQL Only On One Table

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.

like image 487
E. Peterson Avatar asked Apr 28 '15 20:04

E. Peterson


People also ask

What is the difference between an index reorg and an index rebuild?

"Reorganize index" is a process of cleaning, organizing, and defragmenting of "leaf level" of the B-tree (really, data pages). Rebuilding of the index is changing the whole B-tree, recreating the index.

Does index rebuild lock table?

SQL Server index reorganizes operation is always executed online. On the other hand, rebuilding an index can be executed online, without locking other queries when using SQL Server Enterprise edition, or offline, by holding locks on the database objects during the rebuild operation.

How does index rebuild work in SQL Server?

The index rebuild operation can be performed online, without locking other queries when using the SQL Server Enterprise edition, or offline by holding locks on the database objects during the rebuild operation. In addition, the index rebuild operation can use parallelism when using Enterprise edition.

What happens when index is rebuild?

Performing an index rebuild eliminates fragmentation, compacts the pages based on the existing fill factor setting to reclaim storage space, and also reorders the index rows into contiguous pages.


1 Answers

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
like image 75
Rajeev Goel Avatar answered Oct 03 '22 14:10

Rajeev Goel