7 years ago meme (imported from SE)

Batch Mode Adaptive Joins are part of a family of features in the 2017 query processor that consists of:

So how do Adaptive Joins work?

Top Answer
7 years ago meme (imported from SE)

Batch Mode Adaptive Joins

For Batch Mode Adaptive Joins, the goal is to not pin join choice to a specific type at compile time.

When available, Adaptive Joins allow the optimizer to choose between Nested Loops Joins and Hash Joins based on row thresholds at run time.

At this time, Merge Joins are not considered. Pure speculation is that needing data to be sorted, or needing to inject a sort into the plan would add too much overhead when changing the course of a query.

When Do Batch Mode Adaptive Joins Occur?

At this time, Batch Mode query processing requires the presence of a ColumnStore index. They also require, well, a join, and an index that allows for the choice of a Nested Loops or Hash Join.

How do I know if my Join is Adaptive?

Query plans for adaptive joins are quite distinctive.

NUTS

The Adaptive Join operator is new to SQL Server 2017, and currently has the following properties in actual execution plans.

NUTS

  • Physical Operation: Adaptive Join

  • Actual Join Type: Will be Hash match or Nested Loops

  • Adaptive Threshold Rows: Signifies the tipping point when the join type will switch to Hash Match

  • Is Adaptive: True for Adaptive Joins

  • Estimated Join Type: Rather self-explanatory!

In an estimated or cached plan, there is rather less information:

NUTS

Most notably, the Actual Join Type is missing.

What breaks Batch Mode Adaptive Joins?

To monitor this, there’s an Extended Event session called adaptive_join_skipped, and it has the following reasons for skipping a Batch Mode Adaptive Join:

  • eajsrExchangeTypeNotSupported
  • eajsrHJorNLJNotFound
  • eajsrInvalidAdaptiveThreshold
  • eajsrMultiConsumerSpool
  • eajsrOuterCardMaxOne
  • eajsrOuterSideParallelMarked
  • eajsrUnMatchedOuter

Aside from those, Batch Mode Adaptive Joins may be skipped for other reasons. Take these two queries for example:

They’re identical except that the second query selects the DisplayName column, which has a type of NVARCHAR(40).

NUTS

The Batch Mode Adaptive Join is skipped for the second query, but no reason is logged to the XE session. It appears that string data remains the steadfast enemy of ColumnStore indexes.

There are other query patterns that fail to get Adaptive Joins, that also do not trigger events.

Some examples:

  • CROSS APPLY with a TOP

  • OUTER APPLY

eajsrExchangeTypeNotSupported

One thing that will trigger this event appears to be the presence of a Repartition Streams operator. In this query, the partitioning type is Hash Match. Special thanks to the Intergalactic Celestial Being masquerading as a humble blogging man known as Paul White for the bizarre query.

eajsrHJorNLJNotFound

No queries have triggered this XE yet. What doesn’t work:

  • Merge Join hint

  • Query patterns that rule out join type, for example Hash and Merge joins require at least one equality predicate. Writing a join on >= and <= does not trigger the event.

eajsrInvalidAdaptiveThreshold

This event can be triggered by various TOP, FAST N, and OFFSET/FETCH queries. Here are some examples:

In some circumstances, they can also be triggered by paging-style queries:

eajsrMultiConsumerSpool

No known query patterns have triggered this event yet.

What hasn’t triggered it so far:

  • Recursive CTEs

  • Grouping sets/Cube/Rollup

  • PIVOT and UNPIVOT

  • Windowing functions

eajsrOuterCardMaxOne

A couple different types of queries have triggered this event. A derived join with a TOP 1, and a join combined with a WHERE clause with an equality predicate on a unique column:

eajsrOuterSideParallelMarked

One type of query that can trigger this event is a Recursive CTE.

The reason here appears to be that the recursive portion of the CTE, which causes a serial zone in the plan, disallows Batch Mode Adaptive Join choice.

eajsrUnMatchedOuter

This is the most common, and seems to occur when an index is used for the join that cannot support a seek. For instance, this query causes a Key Lookup:

NUTS

The resulting query chooses a Row Mode Nested Loops join to execute both the Key Lookup and the table joins, which triggers the event.

Another example is a query that skips the narrow nonclustered index in favor of the PK/CX. In this case, the PK/CX does not lead with OwnerUserId, so the only join choice is a Hash Join.

In both cases, the “unmatched outer” seems to indicate that the index chosen does not sufficiently cover our query.

Do Batch Mode Adaptive Joins work with multiple joins?

Yes, but as of this writing, there appears to be a limitation:

Joining from one ColumnStore index to multiple Row Store indexes will yield multiple Adaptive Joins, whereas a join between multiple ColumnStore indexes will not be Adaptive.

For example, these two queries

The first query joins one ColumnStore index (on Users) to two Row Store indexes on Posts and Comments. This yields two Adaptive Join operators.

The second query joins two ColumnStore tables (Users and Comments) to one Row Store table (Posts), and yields one Adaptive Join.

NUTS

Is there any overhead to Batch Mode Adaptive Joins?

Yes, all Batch Mode Adaptive Join plans receive a memory grant. This isn’t always the case with Nested Loops join, unless they receive the Nested Loops prefetch optimization. The memory grant is to support a Hash Join should the plan require one based on row thresholds.

This room is for discussion about this question.

Once logged in you can direct comments to any contributor here.

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.