The following db<>fiddle:
...produces an execution plan with an *RID Lookup* like:
![Plan with RID Lookup](/image?hash=102934261ef688343e3d23039f340f296a830999da4f3cf0b54d2fa0529976e3 "Plan with RID Lookup")
The *RID Lookup* is expected, because the nonclustered index is not covering for the query. The execution engine has to fetch the `padding` column from the heap base table.
My question is about the *Compute Scalar*:
![Compute Scalar](https://i.imgur.com/RWsigPa.png "Compute Scalar with BmkToPage")
**What is the `BmkToPage` function, and what purpose does it serve in this plan?**
The *Nested Loops Join* has the *Optimized* property:
![Optimized property](https://i.imgur.com/QtpPDZy.png "Optimized Nested Loops Join")
This means SQL Server sorts rows arriving from the *Index Seek* to promote a sequential I/O data access pattern at the *RID Lookup*. The sort is invisible, and known as a *batch sort*. Craig Freedman discusses optimized nested loops and batch sorting in the article [OPTIMIZED Nested Loops Joins].
The invisible batch sort is unlike a normal *Sort* plan operator in that it can never spill to *tempdb*. A batch sort makes a best effort to sort rows into the desired order using the memory grant it is given. When full sorting cannot be achieved, the partially sorted output is still helpful to the *RID Lookup*. The optimizer chooses a batch sort when it thinks sorting could be useful, but it cannot justify the cost of a full Sort operator
The general idea is to sort the upcoming *RID Lookups* into heap page id order, to promote sequential I/O. The heap page numbers are stored in the nonclustered index as part of the heap bookmark (labelled `Bmk1000` in this plan). These page numbers are extracted from the bookmark by the intrinsic function `BmkToPage`.
The function itself is trivial, but you may encounter adverse performance effects due to the invisible sorting. Older versions of SQL Server could also suffer from an [excessive memory grant] for the hidden sort.
Optimized nested loops can be disabled in modern SQL Server versions with the query hint:
OPTION (USE HINT ('DISABLE_OPTIMIZED_NESTED_LOOP'));
Note that this hint will also prevent an explicit *Sort* operator before the *Nested Loops Join*.
Disabling optimized nested loops alone can be achieved with [documented trace flag 2340]:
>Causes SQL Server not to use a sort operation (batch sort) for optimized Nested Loops joins when generating a plan.
### Further reading
- [Batch Sort and Nested Loops] by Dmitry Piliugin.
- [Addressing large memory grant requests from optimized Nested Loops] by Pedro Lopes.
- [Nested Loops Prefetching] by me.