Rebuild all indexes in the current database

Source: Internet
Author: User
Rebuilding all indexes in the current database without DECLARE @ sqlnvarchar (500) Partition (SCHEMA_NAME (tbl. schema_id) +. + QUOTENAME (tbl. NAME) tb_name, QUOTENAME (I. NAME) idx_namefromsys.tablesastblin

Rebuilding all indexes in the current database without DECLARE @ SQL nvarchar (500) DECLARE RebuildAllTableIndexes CURSORREAD_ONLYFOR SELECT QUOTENAME (SCHEMA_NAME (tbl. schema_id) + '. '+ QUOTENAME (tbl. NAME) tb_name, QUOTENAME (I. NAME) idx_nameFROM sys. tables AS tblINNE

Rebuild all indexes in the current database
<无>
DECLARE @sql nvarchar(500) DECLARE RebuildAllTableIndexes CURSORREAD_ONLYFOR SELECT QUOTENAME(SCHEMA_NAME(tbl.schema_id)) + '.' + QUOTENAME(tbl.NAME) tb_name,QUOTENAME(i.NAME) idx_nameFROM sys.tables AS tblINNER JOIN sys.indexes AS i ON (i.index_id > 0AND i.is_hypothetical = 0)AND (i.object_id = tbl.object_id) DECLARE @tb_name nvarchar(255), @idx_name nvarchar(255)OPEN RebuildAllTableIndexes FETCH NEXT FROM RebuildAllTableIndexes INTO @tb_name, @idx_nameWHILE (@@fetch_status <> -1)BEGINIF (@@fetch_status <> -2)BEGINSET @sql = N'ALTER INDEX ' + @idx_name + N' ON ' + @tb_name + N' REBUILD PARTITION = ALL WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90)'EXEC sp_executesql @sqlENDFETCH NEXT FROM RebuildAllTableIndexes INTO @tb_name, @idx_nameEND CLOSE RebuildAllTableIndexesDEALLOCATE RebuildAllTableIndexesGO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.