How does SQL covering index work?

做~自己de王妃 提交于 2019-12-24 09:08:27

问题


I know, when we use a covering index, sql server uses only index seek(nonclustered) or index scan (nonclustered) operator in execution plan without retrieving data by lookup operator. But why is it possible not to look up values in clustered index? Nonclustered index doesn't store data on leaf level so regardless the number of columns it contains it has to ask clustered index to return data rows so it should be lookup operator in execution plan. Am I right? I've read https://www.red-gate.com/simple-talk/sql/learn-sql-server/using-covering-indexes-to-improve-query-performance/ and other articles but there is no explanation.


回答1:


Any index, at all levels, stores the values for the column(s) defining that index's key. In addition, at the leaf level of the non-clustered index, the leaves store the values for any additional column(s) which are part of the clustered index key1 and were not part of the non-clustered index key, because that is how the clustered index lookup is then performed.

If the only column(s) that the query needs to retrieve are either part of the non-clustered index key or are part of the clustered index key then we've already obtained all of those column values by navigating the non-clustered index.

Queries, in general, are not trying to retrieve rows, only row values from particular columns.


As an analogy, consider that you're running a census for an entire town, and are storing all of the data and physical cards. These cards contain the person's name, address, date of birth, current occupation, etc. Assume further that every individual has a unique address and so you decide to store all of these cards in address order, in a big box file. This is your clustered index.

You frequently want to locate people based on their names. So you create another set of index cards that tell you, for any particular combination of surname and firstname all of the addresses at which someone with that name resides. You put these cards in a second box file and sort them by surname, firstname values. This is your non-clusered index.

Finally, suppose your task is to identify the street on which all people with the surname Radish live. You can obviously use your non-clustered index to identify all of the people with the surname Radish. But remember, the cards in this secondary index gives you the addresses for these people. If our only task is to identify their street, we already have that information at hand. There's no need for us to go and look up all of the original census cards, containing all kinds of information that we've not been asked for, just to complete this query.


1And since 2012, any additional columns identified in an INCLUDE clause for the index definition.




回答2:


Since covering index includes all the SELECT items of the query, there is no need to go to table data and try to read from data files. The data you try to read is already contained in the Covering Index.

So you either read all covering index by index scan or you directly read by using index seek.



来源:https://stackoverflow.com/questions/48059124/how-does-sql-covering-index-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!