我从来没有真正理解这两个索引之间的区别,谁能解释一下区别是什么(性能方面,索引结构在数据库中的外观,存储方面等)?
I've never really understood the difference between these two indexes, can someone please explain what the difference is (performance-wise, how the index structure will look like in db, storage-wise etc)?
包含索引
CREATE NONCLUSTERED INDEX IX_Address_PostalCode
ON Person.Address (PostalCode)
INCLUDE (AddressLine1, AddressLine2, City, StateProvinceID);
'普通'索引
CREATE NONCLUSTERED INDEX IX_Address_PostalCode
ON Person.Address (PostalCode, AddressLine1, AddressLine2, City, StateProvinceID);
索引的内部存储采用 B-Tree 结构,由索引页"(根页和所有中间页)和索引数据页"(仅叶页).
The internal storage of indexes uses a B-Tree structure and consists of "index pages" (the root and all intermediate pages) and "index data pages" (the leaf pages only).
注意不要将索引数据页"与存储大部分实际数据列的数据页"(聚集索引的叶页)混淆.
Note do not confuse "index data pages" with the "data pages" (leaf pages of clustered indexes) which store most of the columns of actual data.
INCLUDE 部分放置一些列,每个索引键存储在每个页面上的数据更少.
INCLUDE section, less data per index key is stored on each page.当使用索引时,索引键用于通过索引页导航到正确的索引数据页.
When an index is used, the index key is used to navigate through the index pages to the correct index data page.
INCLUDE 列,则该数据在查询需要时立即可用.INCLUDE 列中不可用的列,则需要对聚集索引中的正确行(或堆,如果没有聚集索引)进行额外的书签查找"已定义索引).INCLUDE columns, that data is immediately available should the query need it.INCLUDE columns, then an additional "bookmark lookup" is required to the correct row in the clustered index (or heap if no clustered index defined).一些注意事项,希望能解决您的一些困惑:
Some things to note that hopefully addresses some of your confusion:
INCLUDE 列中有什么内容).INCLUDE 列.)INCLUDE columns).INCLUDE columns as well.)值得注意的是,在 INCLUDE 列被添加为一项功能之前:
It's worth noting that before INCLUDE columns were added as a feature:
INCLUDE 列基本上可以更有效地获得相同的好处.INCLUDE columns basically allow the same benefit more efficiently.注意 需要指出的一点很重要.如果您养成总是将查询编写为 SELECT * ... 的懒惰习惯,那么您通常从索引中的 INCLUDE 列中获得零收益.通过返回所有列,您基本上可以确保在任何情况下都需要进行书签查找.
NB Something very important to point out. You generally get zero benefit out of
INCLUDEcolumns in your indexes if you're in the lazy habit of always writing your queries asSELECT * .... By returning all columns you're basically ensuring a bookmark lookup is required in any case.
这篇关于包含列的索引,有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
截断时的 SQL 触发器SQL trigger on Truncate(截断时的 SQL 触发器)
具有多个可选搜索参数的 sql 搜索查询sql search query with multiple optional search parameters(具有多个可选搜索参数的 sql 搜索查询)
SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUPSQL Efficiency: WHERE IN Subquery vs. JOIN then GROUP(SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP)
使用 t-SQL 检索 XML 元素名称Retrieving XML element name using t-SQL(使用 t-SQL 检索 XML 元素名称)
在 SQL 输出中插入双引号Insert double quotes into SQL output(在 SQL 输出中插入双引号)
从 SQL SERVER 中的 CTE 中删除行Delete rows from CTE in SQL SERVER(从 SQL SERVER 中的 CTE 中删除行)