SQLSERVER Index Summary

The TEST TABLE the CREATE (the INT ID, the UNAME VARCHAR (10));
- non-clustered index
CREATE INDEX IDX_TONY ON TEST (ID) ;

View TEST table IDX_TONY1 index fragmentation information for
the DBCC SHOWCONTIG (TEST, IDX_TONY1);
/ *
Scan Pages: If you know the approximate line refers to the number of rows and tables or indexes in, then you can estimate the number of pages in the index, If you are significantly higher than the estimated number of pages, indicating the presence of internal fragmentation
number of scan area: divided by the number of scanned pages 8, rounded to the next highest value. This value should be returned and the number of scanning DBCC SHOWCONTIG same extent. If the number of DBCC SHOWCONTIG returned high, indicating the presence of external fragmentation. Severe degree of fragmentation depends on the estimated value than just show much higher values of
the handover number: This number should be equal to 1 minus the number of scanning area, then there is a high external fragmentation.
Average number of pages of each zone: The number of pages is divided by the number of scanning scanning extent, usually 8, less than 8 illustrate the external fragmentation
scanning density [optimal value: actual value]: This is the most extents best ratios and actual values, and this percentage should be as close as possible to 100%, then there is a low external debris
fragments logic scan: percentage of this destructurized page, this percentage should be between 0% and 10%, then the high there are external fragmentation
zone scan fragmentation: percentage share of disorderly extents in scanning the leaf pages in the index, the percentage should be 0%, then there is high external fragmentation
average number of available bytes per page: scanned the average number of bytes available on the page, the higher the explanation there is internal fragmentation, but before you decide to use this number if there is internal fragmentation, you should consider fill factor (fill factor)
average page density (full): average available on each page opposite number, the lower the percentage of the number of bytes to specify the percentage of internal fragmentation
* /

CREATE TABLE TEST(ID INT,UNAME VARCHAR(10));
--非聚集索引
CREATE INDEX IDX_TONY ON TEST(ID);
CREATE INDEX IDX_TONY2 ON TEST(UNAME);
--带填充因子的索引
CREATE INDEX IDX_ID1 ON TEST(ID) WITH (PAD_INDEX=ON, FILLFACTOR=20);
--填充因子一般设置的原则是,数据变化较大,填充因子设较小值,而数据变化较小,填充因子设较大值,只读表的填充因子可设置为100

--删除索引
DROP INDEX TEST.IDX_TONY;
DROP INDEX TEST.IDX_ID1;

--唯一聚集索引
CREATE UNIQUE CLUSTERED INDEX IDX_UN_TONY ON TEST(ID);
DROP INDEX TEST.IDX_UN_TONY;

--聚集索引的顺序就是数据的物理存储顺序,对于一个表来说,只有一个聚集索引(类似字典字母顺序)
--非聚集索引的顺序跟数据的物理顺序无关,索引与数据存放在不同的物理区域,一个表可以有多个(类似字典偏旁部首)

--查看索引定义
EXEC SP_HELPINDEX TEST;
--TEST表的索引情况
SELECT * FROM sys.indexes WHERE object_id=object_id('TEST');

--修改索引名
EXEC SP_RENAME 'TEST.IDX_TONY2','IDX_TONY1';

--更新TEST表中全部索引的统计信息
UPDATE STATISTICS TEST;

--增加AGE字段
ALTER TABLE TEST ADD AGE INT NOT NULL DEFAULT 0;

--查看IO信息
SET STATISTICS IO ON;
SELECT * FROM TEST WHERE ID>4;
DBCC TRACEON(3604,2588);
DBCC IND(test,TEST,-1);

DBCC SHOW_STATISTICS(TEST,'IDX_ID2');

--复合索引
CREATE INDEX IDX_UNAME_AGE ON TEST(UNAME,AGE);
--覆盖索引
CREATE INDEX IDX_UNAME_AGE2 ON TEST(ID) INCLUDE(UNAME,AGE);

/*
如何选择:
1、覆盖索引可以使得索引键变化引起的波动更小。因为如果索引列发生变化,那么索引结构就要调整,如果包含列的值发生变化,整个索引的结构不会发生变化,只是包含列中的值进行更新而已
2、索引中的数据列越少,数据分布的统计维护的成本就越小
3、如果是频繁的写操作,一般索引列不要太多;如果是频繁大量数据读取,可以考虑把一些列包含在索引列
其实都是一种“空间换时间”的策略,提高查询速度
*/

Rebuild all indexes and single-table database traversing the index of all tables

DBCC SHOWCONTIG(表名);

EXEC sp_helpindex 表名;

--单个表的所有索引重建
ALTER INDEX ALL ON 表名 REBUILD;


--重建指定数据库的所有表的所有索引
USE 数据库名
DECLARE @name varchar(100)
DECLARE tony_cursor CURSOR FOR
SELECT [name] FROM sysobjects WHERE xtype='u'
OPEN tony_cursor
FETCH NEXT FROM tony_cursor  INTO @name
WHILE @@FETCH_STATUS = 0 BEGIN
PRINT @name
DBCC DBREINDEX (@name, '', 90)
FETCH NEXT FROM tony_cursor  INTO @name END
CLOSE tony_cursor
DEALLOCATE tony_cursor

 

Published 46 original articles · won praise 9 · views 3657

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/100939372