SQL Server index introduction

One, the classification of the index

  • Clustered index

The clustered index in SQL Server is stored in a B-tree data structure. Each data page in the B-tree is an index node, the uppermost index node is called the root node, the lowermost index node is called the leaf node, and the index node between the root node and the leaf node is an intermediate node.

In a clustered index, the leaf node contains all row record data.

The clustered index is stored in order according to the index key. Compared with heap storage, the range query of clustered index is very efficient.

A table can only have one clustered index, try to set the clustered index column to IDENTITY.

Clustered index fields should not be changed frequently, and it is not recommended to use large and wide columns as a clustered index.

When the clustered index has only one field partition, its B-tree structure is shown in the figure below, and index_id=1 is the index of the field partition. When the clustered index has multiple field partitions, each field partition has an independent B-tree structure. If a clustered index has 4 field partitions, then the table will create 4 B-tree structures.

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-6kbsC4q8-1594707963215)(http://note.youdao.com/yws/res/59746/0D8CD630FF3142BD9E0D0FE023C59725)]

  • Nonclustered index

Non-clustered index can be in two situations, one is index-organized table storage, and the other is heap table storage.

Whether it is a heap table or an index-organized table, the storage structure of a non-clustered index is B-tree storage, which is stored in order according to the index key. The difference between a heap table and an index-organized table lies in the storage content of its leaf nodes. The heap table points to its corresponding RID through a row pointer, and the index-organized table points to its corresponding clustered index value through a row pointer.

When using a non-clustered index to query data, traverse the non-clustered index to locate the data that meets the conditions, and then use Bookmark to find other field data outside the non-clustered index according to the corresponding clustered index.

The upper limit of the index length of a non-clustered index is 900 bytes, the include field is not included in the index key, and its field length is not calculated

如:Title nvarchar(50)、Revision nchar(5)、FileName nvarchar(400)
可以创建复合索引(Title,Revision) include (FileName),其索引长度为(50+5)*2

CREATE INDEX IX_Document_Title   
ON Production.Document (Title, Revision)   
INCLUDE (FileName);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-3nYGOMwG-1594707963218)(http://note.youdao.com/yws/res/59794/E30AAE6A15B341268519CF2AE9BE349B)]

  • Unique index

To ensure the uniqueness of index fields, a table can have multiple unique indexes.

The unique index can also use the include field as an inclusive index

  • Compound index

An index created by multiple fields is called a compound index. The compound index needs to pay attention to the order of the fields. It is recommended to put fields with high selectivity and fields with frequent queries in the prefix column

  • Inclusive index

The key word of an inclusive index is include, which is similar to a covering index and is used to avoid back-to-table operations. Unlike the covering index, the include field is not in the index key column, but in the non-key column, so it is impossible to conditionally filter the include field, just to avoid returning to the table for the select query field.

In an inclusive index, the maximum limit for non-key columns is 1023

The maximum upper limit of the index key column is 16, the field length is 900 bytes

The same column cannot appear in the index key and include non-key columns at the same time

  • Covering index

Query and filter fields can be returned through the index key, no need to return to the table operation, reasonable use of covering index for optimization.

  • Filter index

Also known as a filtered index, the creation is to filter through the where keyword, and the records that meet the conditions are created for the index, and the records that do not meet the conditions are not included in the index.

-- 在view_count字段中满足>50的记录上创建索引
create nonclustered index idx_count on deadlock_test(view_count) where view_count>50;

-- 有效利用筛选索引
select id from deadlock_test where view_count>60
-- 无法有效利用筛选索引
select id from deadlock_test where view_count<30
  • Calculated column index

Create a new field in the table to save the results of the column only after the calculation, after creating an index for the field, this index becomes a calculation index

The calculated column data is actually stored in the table, and an additional field needs to be created to add an index. The function usage is not as good as the design of the virtual column of mysql5.7.

CREATE TABLE TestTable (a int, b varbinary(4));

CREATE INDEX TestTabIndex ON dbo.TestTable(a,b)  
WHERE b = CONVERT(Varbinary(4), 1);
  • Index view

Materialized views, standard views will not permanently store the result set in the database, but dynamically generated each time it is used, and the indexed view creates a unique clustered index for the view through the with schemabinding keyword to save the result set in the database in.

  • Column store index

Two, clustered index table and heap table

1, the clustered index table

Also called index-organized table, the storage of the table is stored in the order of the clustered index, and the leaf nodes of the clustered index contain all records. The leaf node in the secondary index points to the corresponding clustered index value, and when the query needs to return to the table, the clustered index returns to the table to query.

2. Heap table

In SQL Server, if a clustered index is not created for the table, the storage method of the table is heap table storage. Heap is a collection of unordered data. Indexing is to make data ordered. In the index, the key values ​​are ordered, but the data is still out of order. In the heap table, each key of the secondary index points to a RID, and if you need to query the data of other fields outside the index, you can use the RID to query the table back.

Three, index related commands

1. Index missing statistics

-- 索引缺失统计
select ddmid.statement,ddmid.equality_columns,ddmid.inequality_columns,ddmid.included_columns,
ddmigs.user_seeks,ddmigs.user_scans,ddmigs.avg_total_user_cost,ddmigs.avg_user_impact
from sys.dm_db_missing_index_details ddmid
join sys.dm_db_missing_index_groups ddmig
on ddmid.index_handle=ddmig.index_handle
join sys.dm_db_missing_index_group_stats ddmigs
on ddmig.index_group_handle=ddmigs.group_handle
Field meaning
statement Table name with missing index
user_seeks From the start of the server to the present, the number of times that the missing index can be requested by the user for the seek operation
user_scans From the start of the server to the present, the number of times that the missing index can be requested by the user for the scan operation
avg_total_user_cast After the missing index is created, the average cost of resource consumption is reduced. The larger the parameter, the higher the efficiency after the index is created.
avg_user_impact After the missing index is created, the average cost reduction percentage

2. Invalid index statistics

-- 无效索引统计
select i.name,ddius.user_seeks,ddius.user_lookups,ddius.user_updates
from sys.dm_db_index_usage_stats ddius
join sys.indexes i
on ddius.index_id=i.index_id and ddius.object_id=i.object_id
order by ddius.user_seeks

3. View the statistics of the index

-- 查看具体索引的统计信息
dbcc show_statistics('deadlock_test','idx_info_uuid')

-- 查看表中所有索引的统计信息
select * 
from sys.stats as s
where s.object_id=object_id('table_name')

-- 更新指定索引的统计信息
UPDATE STATISTICS dbo.table_name idx_name;

-- 更新表中所有统计信息
UPDATE STATISTICS Sales.SalesOrderDetail;

--更新整个数据库上的所有统计信息
EXEC sp_updatestats;

Guess you like

Origin blog.csdn.net/weixin_37692493/article/details/107337391