Sub topic 12
Performance and Extensibility Practices
This chapter covers the habits that keep analytical queries fast and let you change them safely. It assumes you can read CDS view entities, understand the cube, dimension and query pattern from earlier chapters, and know basic SQL. It also assumes you have seen how queries are consumed in Fiori apps.
Why this matters in embedded analytics
Embedded analytics runs on live transactional data. There is no separate warehouse to absorb load, and no overnight load job to precompute results. Every query you expose is executed by the SAP HANA database against the same tables that sales orders and postings are written to. A badly shaped view slows reports and can also compete with business transactions for memory and CPU.
Extensibility matters for a related reason. SAP delivers many views, and customers add fields and logic. If you change delivered objects in place, upgrades break your work. If you build on stable extension points, upgrades are routine.
How a query is executed
When a Fiori app or another consumer calls a query view, the analytical engine reads the query definition. It then generates a request to the database, and the database resolves the whole view stack into one SQL statement. The cube, its dimensions and the associations behind them are all unfolded into that statement.
The important consequence is that the database optimizer sees the entire stack at once. It can push filters down to the tables and skip joins that are not needed. Your goal is to write views that let it do so.
Performance practices
Keep the query narrow
A query should expose only the fields users need. Each extra dimension on the rows, or in the drilldown list, widens the grouping and can multiply the number of result rows. Two habits help:
- Place only the essential fields on the initial axes. Leave other fields available for drilldown, but off by default.
- Prefer a few focused queries over one query that tries to answer every question.
Use associations, not joins, for dimensions
In the cube you saw this pattern:
association [0..1] to ZI_CustomerDim as _Customer
on $projection.CustomerId = _Customer.CustomerIdAn association is resolved lazily. The join to the customer dimension is added to the SQL only when a field of _Customer is actually requested. If a query uses just NetAmount grouped by CustomerId, the customer table is not touched. An inner or left outer join written directly in the from clause is always executed, even when no consumer needs the data.
Use a join in the cube only when you need it for every request, for example to derive a column that is part of the cube itself.
Declare cardinality accurately
The cardinality in an association, such as [0..1] or [1..1], is information for the optimizer. If you declare [0..1] but the data can produce several matches, the cube can return duplicate rows and wrong sums. If you declare [0..*] where only one match exists, the optimizer cannot simplify the join. Declare what the data model guarantees, and verify it.
Aggregate early and filter early
Filters on fields that come from the underlying tables are pushed down to the database. Filters that depend on calculations are harder to push down. Two practices follow:
- Put the fields you expect users to filter on, such as company code, fiscal period or sales organization, directly in the cube as plain fields from the base table.
- Avoid wrapping a filter field in a function in the query. For example, filtering on a calculated string built from a date prevents the database from using it efficiently. Expose the date itself and let the front end filter on the date.
Be careful with calculated fields
Calculations are fine, but where they run changes cost. A calculation in the cube runs for every row before aggregation. A calculation in the query on an aggregated value runs after aggregation, on far fewer rows. Compare these two situations:
| Calculation | Where it runs | Rows involved |
|---|---|---|
| `net_amount * exchange_rate` in the cube | Before aggregation | One per item row |
| Ratio of two aggregated measures in a query | After aggregation | One per result row |
Do row-level arithmetic in the cube only if the result is additive. Ratios and percentages are not additive. Sum of ratios is not the ratio of sums. Define such values in the query as calculated key figures so they are computed from the aggregated parts.
Control currency and unit conversion
Converting currency per row is expensive because each row needs a rate lookup. If a report always needs one target currency, make the conversion part of the query definition so the engine applies it once with the aggregation logic. Do not stack your own conversion on top of delivered views that already convert.
Mind the depth of the view stack
Each layer should have a purpose: interface views expose data, composite views combine it, and consumption views serve a specific need. Stacking additional layers that only rename fields adds no value and makes the generated SQL harder to read and tune. If a view in your own stack does nothing but pass fields through, remove it.
Avoid heavy logic in access controls
Row-level authorization is applied to the generated SQL. A condition that needs a large lookup, or a complex pattern, is evaluated for every request. Keep conditions simple and based on fields in the cube. Use the authorization fields defined at the right layer, as described in the authorization chapter.
Measure with the right tools
Do not guess. Use these tools to find where time goes:
- In the ABAP development tools in Eclipse, open the SQL console or the data preview for the view and check the runtime with realistic filters.
- Use the SQL trace or runtime analysis (transactions ST05 and SAT) to see the statement the system actually sends.
- Use the HANA plan visualization to see which joins and filters appear in the plan.
- In the query monitor transaction, check how long queries take and whether the cache is used. Transaction names can differ by release, so verify them in your system.
Test with production-sized data. A query that returns in 200 ms on 10,000 rows may take minutes on 200 million.
Extensibility practices
Use the released extension points
SAP separates objects that are safe to build on from those that are not. Released views carry a release contract, which tells you whether you may use them in your own views and whether they will stay stable. Build on released views. Do not copy SAP views into the customer namespace unless there is no other way, because a copy no longer receives SAP's improvements.
Add fields with extension includes and extend views
When a customer needs a new field in a delivered cube or dimension, there are two supported ways:
- Key user extensibility. The Custom Fields app adds a field to a business context. The system extends the underlying table, the related CDS views and the services. Use this for simple fields on standard business objects.
- Developer extensibility with view extensions. A developer writes an extend view entity that adds elements to a delivered view that allows extension. This suits calculated fields or fields from your own tables.
A view extension looks like this:
@EndUserText.label: 'Extension of sales item cube'
extend view entity ZI_SalesItemCube with
{
zsales_item.cost_center as CostCenter
}The extended element then flows into every view above it that selects all fields of the extended view. Views that list their fields explicitly need the new field added by their own extension.
Prefer extension to modification
Never edit a delivered view in place. Modifications must be re-evaluated on every upgrade, and they can be overwritten. An extension is stored as a separate object and is merged by the system, so upgrades keep it.
Plan for field naming and namespace
Use a clear prefix for your own fields so they cannot collide with fields SAP adds later. In an extension, an element name beginning with your customer prefix, such as Z, is safe. Without a prefix, a later SAP release may add a field with the same name and produce an activation error.
Keep the layers stable for consumers
Query views are consumed by apps, tiles and external tools. Treat their names and field names as an interface:
- Do not rename or delete a field that apps depend on. Add a new field instead, and retire the old one after you have updated consumers.
- Keep semantics stable. A measure that suddenly includes taxes changes every report built on it.
- Use clear labels with
@EndUserText.label, since they appear in front ends.
Reuse instead of copy
If two queries differ only in a filter or a few columns, build both on the same cube. Do not create a second cube over the same table. Copies diverge over time, and measure definitions end up inconsistent between reports.
A review checklist
Use this list before you transport a new analytical model:
| Check | Why |
|---|---|
| Dimensions linked by associations, not joins | Joins run only when needed |
| Cardinality declared and verified | Prevents duplicates and enables optimization |
| Ratios defined in the query, not summed in the cube | Gives correct aggregated results |
| Filter fields exposed as plain columns | Filters push down to the database |
| Tested with production-sized data | Reveals real runtime |
| Built on released views, extended not modified | Survives upgrades |
| Own fields carry a customer prefix | Avoids naming collisions |
| Field names of queries kept stable | Protects consumers |
Common errors
- Wrong totals after adding a join. A one-to-many join in the cube repeats item rows, and sums are inflated. Use an association with correct cardinality, or aggregate before joining.
- Slow query with a small result. The database may read many rows to produce few. Check that filter fields come directly from tables and that no function blocks pushdown.
- Extension field missing in a report. The upper view lists its fields explicitly, so it does not pick up the new field. Extend that view too.
- Activation error after an upgrade. An unprefixed custom field collides with a new SAP field. Rename it with your prefix.
Exercise
A colleague adds a one-to-many join from the sales item cube to a delivery table in the from clause so that a delivery date is available in reports. Afterwards, total net amount in the query is higher than before. What caused it, and how would you fix it while keeping delivery date available?
Show answer
Each sales item now appears once per matching delivery row, so its net amount is counted several times. Remove the join from the cube. Expose delivery data through an association with the cardinality it really has, or build a separate cube at the delivery level, so that the sales item measures are not repeated. Then verify the total against the earlier figure.
Exercise
Name two reasons to extend a delivered view with extend view entity instead of copying it into a Z view.
Show answer
An extension is kept and merged by the system during upgrades, so it stays compatible. A copy no longer receives SAP's changes and fixes, and it can drift from the standard definition of measures and fields.