Resolve fragmentation problems for SQL Server tables or indexes

Source: Internet
Author: User

Long-term modification or deletion of a table can produce a large amount of fragmentation that affects database performance. The solution is to rebuild the table or index, eliminate fragmentation, and achieve optimization.

Directly on the code:

* Query Fragmentation, avg_fragmentation_in_percent is the percentage of the index, more than 30 is not normal, need to rebuild * *

DECLARE @db_id int;

DECLARE @object_id int;

SET @db_id = db_id (N ' adventureworks2008r2 ');

SET @object_id = object_id (N ' adventureworks2008r2. Person.Address ');

IF @db_id is NULL

BEGIN;

PRINT N ' Invalid database ';

End;

ELSE IF @object_id is NULL

BEGIN;

PRINT N ' Invalid object ';

End;

ELSE

BEGIN;

SELECT * from Sys.dm_db_index_operational_stats (@db_id, @object_id, NULL, NULL);

End;

Go

/* Rebuild Table * *

ALTER TABLE < table name > rebuild

/* Rebuild Index * *

Alter INDEX < index name > on < table name > rebuild

Related Article

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.