Association Lazy Loading and CDS Compiler Behavior in ABAP CDS View Entities

55 min read
Association Lazy Loading
Understand why Associations don't immediately generate SQL JOINs and how the CDS compiler decides when related business objects should be accessed.
Click image to enlarge

Association Masterclass

Lesson 4 of 15 – Association Lazy Loading & CDS Compiler Behavior

So far, we've learned how to create Associations and navigate them using path expressions. One important question still remains.

If Associations eventually become SQL JOINs, why doesn't SAP simply generate the JOIN immediately?

The answer lies in one of the smartest design decisions of ABAP CDS: Lazy Loading.

Learning Objectives

By the end of this lesson you will be able to:

  • Explain Association Lazy Loading.
  • Understand how the CDS compiler evaluates Associations.
  • Predict when SQL JOINs will be generated.
  • Understand why Associations improve reusability.
  • Think like the CDS compiler instead of thinking in SQL.

Prerequisites

  • Completion of Lessons 1–3.
  • Understanding of Association syntax.
  • Knowledge of Association Navigation.

Introduction

Developers who come from traditional SQL backgrounds often expect an Association to behave exactly like a JOIN. They assume that as soon as an Association is defined, SAP immediately joins both business objects.

That assumption is incorrect.

An Association is not a SQL JOIN. It is simply metadata describing a reusable relationship between two business objects. At the time the CDS View is activated, no SQL JOIN is generated merely because an Association exists.

Instead, SAP waits until a consumer actually requests data from the target business object. Only then does the CDS compiler decide whether a SQL JOIN is necessary.

Architect Perspective

One of the biggest mindset changes when learning ABAP CDS is moving away from thinking in terms of SQL JOINs.

Instead, think in terms of business relationships.

Associations define relationships.
The CDS compiler decides when those relationships should become SQL.

What is Lazy Loading?

Lazy Loading is a software design principle in which work is postponed until it is actually required.

Rather than retrieving every possible piece of related information, the system retrieves only the data requested by the current consumer.

SAP applies exactly the same principle to Associations.

Association Definition
ABAP CDS
association [0..1] to I_Customer as _Customer

on $projection.SoldToParty = _Customer.Customer

At this point, SAP knows that a relationship exists between Sales Orders and Customers.

However, the compiler still does nothing because no customer fields have been requested.

Traditional SQL Thinking

In traditional SQL, the developer explicitly decides which tables should be joined.

Traditional SQL
SQL
SELECT
    so.SalesDocument,
    c.CustomerName
FROM SalesOrder so

LEFT OUTER JOIN Customer c

ON so.SoldToParty = c.Customer

As soon as this query executes, the database must perform the JOIN because the developer explicitly requested it.

Association Thinking

ABAP CDS follows a completely different philosophy.

CDS Association
ABAP CDS
association [0..1] to I_Customer as _Customer

on $projection.SoldToParty = _Customer.Customer

{

    key SalesDocument,

    SoldToParty

}

Although the Association exists, no Customer fields are requested.

Therefore, the compiler has no reason to generate a SQL JOIN.

Compiler Decision

The relationship is stored for future use, but the target business object is not accessed because no consumer requires it.

🧠

INSIDE THE CDS COMPILER

No Navigation Found

💭 Compiler Thought Process

An Association exists, but no path expression references any fields from the target business object.

Compiler Decision Flow

1

Read the CDS View.

2

Detect the Association definition.

3

Validate the relationship condition.

4

Search for path expressions.

5

No navigation found.

6

No related data required.

✅ Final Compiler Decision

Skip SQL JOIN generation and execute the query using only the source CDS View.

⚡ SQL Optimization Insight

Associations themselves have almost no runtime cost. The compiler postpones SQL generation until a consumer actually navigates the relationship.

What Changes When an Association is Navigated?

Let's compare two nearly identical CDS Views.

The only difference is that the second CDS View accesses a field from the Association.

Example 1 - Association Defined Only
ABAP CDS
{

    key SalesDocument,

    SoldToParty

}

No fields from the Customer business object are requested.

Example 2 - Association Navigated
ABAP CDS
{

    key SalesDocument,

    SoldToParty,

    _Customer.CustomerName

}

That single additional line completely changes how the CDS compiler processes the query.

The CDS Compiler Decision Process

Every time a CDS View is consumed, the CDS compiler evaluates whether related business objects are actually required.

Compiler CheckDecision
Association exists?✅ Yes
Path expression used?Only if a target field is requested.
Target data required?Compiler evaluates the requested elements.
SQL JOIN generated?Only when navigation requires it.

This decision process happens automatically. As developers, we simply define relationships and navigate them when necessary.

Conceptual SQL Generated by the Compiler

Although developers never write the SQL JOIN explicitly, the CDS compiler eventually transforms the Association into SQL when it is required.

Conceptual SQL
SQL
SELECT

    SalesDocument,

    SoldToParty,

    CustomerName

FROM I_SalesDocument

LEFT OUTER JOIN I_Customer

ON SoldToParty = Customer

This SQL is shown only to help understand the compiler's behavior. The exact SQL generated by SAP HANA may differ depending on optimization performed by the database.

Performance Benefits of Lazy Loading

Lazy Loading offers several important advantages over eagerly joining every related business object.

BenefitExplanation
Better PerformanceUnnecessary JOINs are avoided.
Reusable CDS ViewsDifferent consumers navigate only the Associations they need.
Cleaner CodeRelationships are defined once instead of repeating JOIN logic.
Better MaintainabilityBusiness relationships remain centralized.

Real Project Example: Manage Sales Orders Fiori Application

Consider a Fiori application that initially displays only Sales Order, Sold-To Party and Net Amount.

Customer Name is not displayed on the overview screen, so the Association remains unused and no SQL JOIN is generated.

Later, when the user opens the Object Page or requests additional customer information, the CDS View navigates_Customer.CustomerName.

Only then does the CDS compiler generate the SQL JOIN required to retrieve customer master data.

Architect Perspective

Technical Architect Insight

One of the biggest advantages of SAP's Virtual Data Model is that CDS Views are designed for multiple consumers.

A reporting application, a RAP service, a Fiori application and an analytical query can all consume the same CDS View while navigating completely different Associations.

The compiler generates SQL specifically for the current consumer rather than executing every possible JOIN upfront.

💡 SAP Best Practice

Define Associations whenever they represent genuine business relationships.

Navigate them only when related data is required.

Let the CDS compiler decide when SQL JOINs should be generated instead of trying to optimize the SQL manually.

Common Mistakes

  • Assuming Associations automatically improve performance.
  • Believing SQL JOINs are never generated.
  • Thinking Associations store data.
  • Navigating Associations even when related fields are not required.
BeginnerInterview Question

What is Lazy Loading in ABAP CDS?

Answer: Lazy Loading postpones SQL JOIN generation until data from the target Association is actually requested.

ExperiencedInterview Question

Why doesn't the CDS compiler immediately generate SQL JOINs for every Association?

Answer: Because Associations represent reusable relationship metadata. The compiler generates JOINs only when a consumer navigates the Association through a path expression.

ArchitectInterview Question

How does the CDS compiler decide whether an Association should become a SQL JOIN?

Answer: The compiler evaluates the requested elements of the CDS View. If no path expression accesses the target business object, the JOIN is skipped. If navigation occurs, the compiler generates the required SQL JOIN.

💡 Key Takeaway

Associations are intentionally lazy.

Defining an Association simply records a reusable business relationship. It does not retrieve any data.

When a path expression accesses fields from the target business object, the CDS compiler transforms that relationship into the appropriate SQL JOIN.

This compiler-driven approach is one of the key reasons SAP's Virtual Data Model remains reusable, maintainable and performant.

In the next lesson, we'll build on this understanding by exploring Association Cardinality, one of the most important concepts for correctness, performance and generated SQL behavior.