SQL 服务器等同于 MySQL 的 OPTIMIZE 命令是什么?

What is SQL Server's equivalent to MySQL's OPTIMIZE command?

SQL 服务器是否有与 MySQL 的 OPTIMIZE 命令等效的命令?

在 T-SQL 中,您可以像这样重建和重组索引:

ALTER INDEX ALL ON [dbo].[SomeTable] REBUILD

您可能还想查看这篇关于指定索引填充因子的 MSDN 文章。

https://msdn.microsoft.com/en-us/library/ms177459.aspx

这取决于:

  1. 如果当前 table 有聚簇索引,那么 ALTER INDEX ALL ON [Schema].[Table] REBUILD 是一种解决方案。

If ALL is specified and the underlying table is a heap, the rebuild operation has no effect on the table. Any nonclustered indexes associated with the table are rebuilt.

(source)

因此,对于堆 tables,此语句也不会重建堆结构。关于此解决方案,请参阅 All 部分的所有警告(如果 table 有一个或多个,则使用此操作指定 ALL 会失败)。

但是

  1. 如果当前 table 是堆(它没有聚集索引)那么我会使用 ALTER TABLE [Schema].[Table] REBUILD 在最后一种情况下,对于堆 tables,此语句重建堆结构以及所有非聚集索引 (source).