Sub topic 12
Performance and Extensibility Practices
This chapter assumes you can write CDS view entities with associations, know the interface, composite and consumption layers of the Virtual Data Model, and have built a cube, a dimension and a query view as in the earlier chapters. It covers habits that keep analytical queries fast and let you change them later without breaking consumers.
Why these practices matter
Embedded analytics runs on live transactional data. Every query you open is a database request against the same tables that post sales orders. A query that reads too much data, or a model that cannot be extended cleanly, becomes a problem in production. Both are cheaper to prevent in design than to fix later.
Performance practices
The SAP HANA database does the aggregation work, so your goal is to let it work on as little data as possible and to push as much logic as possible down to it.
- Keep cubes narrow. Expose only the fields that analysts need. Every extra field in a cube is one more column that queries may need to read.
- Use associations instead of joins. An association is only resolved when a query uses one of its fields. A join in the cube is always executed. In the cube from earlier chapters,
_Customercosts nothing for a query that never uses customer attributes. - Aggregate in the query, not in the cube. Declare measures with
@DefaultAggregation: #SUMand let the analytical engine group the data by the fields on the query axes. Avoid hand-writtenGROUP BYin the cube. - Filter early. Restrict the data in the query view, in a filter or a variable, so that the database reads fewer rows. A query that returns all years by default reads all years.
- Avoid heavy calculations on every row. Casts, string operations and nested
CASEexpressions on large item tables run once per row. Move them into a dimension where possible, or compute them after aggregation. - Be careful with stacked views. Each layer is useful for reuse, but a chain of many layers hides what the database must do. Check the generated plan when a query is slow.
Where cost comes from
The table summarizes common causes of slow queries and the usual remedy.
| Symptom | Likely cause | Remedy |
|---|---|---|
| Slow with no filters | Query reads all periods or all organizations | Add a default filter or a mandatory variable |
| Slow only when a text is shown | Text association on a very large dimension | Keep the dimension small, select only needed fields |
| Slow after adding a field | New join or calculation in the cube | Move it to an association or a later layer |
| Slow for some users only | Complex access control conditions | Simplify the DCL rules, use fewer nested conditions |
Extensibility practices
Analytical views are used by many apps and users, so changes must not break them. These practices help you extend a model safely.
- Follow the layering. Put reusable logic in interface and composite views. Put report-specific settings in the query view. A change to one query then does not affect other queries.
- Use extension views for released SAP content. Do not copy a delivered view to add a field. Create a CDS extension (
extend view entity) so that your field is added and SAP updates still apply. - Prefer adding to removing. Adding a field rarely breaks a consumer. Renaming or deleting one does. If you must change a name, add the new field first and retire the old one later.
- Name things consistently. Use the prefix conventions of your project, for example
ZI_for interface andZC_for consumption views, so that the role of a view is clear. - Use key user tools for small changes. Custom fields and custom analytical queries made by key users are managed by the system, which keeps them upgrade-safe.
The following example adds a field to a view entity with an extension. It assumes that ZI_CustomerDim allows extensions, and that a field region exists in zcustomer.
@AbapCatalog.viewEnhancementCategory: [#PROJECTION_LIST]
define view entity ZI_CustomerDim
as select from zcustomer
{
key customer_id as CustomerId,
customer_name as CustomerName,
country as Country
}extend view entity ZI_CustomerDim with
{
zcustomer.region as Region
}Review flow
Common errors
- Putting every field of the source table into the cube "just in case".
- Replacing an association with a join to get a field into the cube.
- Copying a released view instead of extending it.
- Changing a field name that other queries or apps already use.
- Testing only with small data sets.
Exercise
A query on your sales cube is slow, and it shows data for all years by default. Name two changes that would help and say which layer each belongs in.
Show answer
First, add a default filter or a mandatory variable on the fiscal year. This belongs in the query view, because it is report-specific. Second, check whether the cube contains joins or per-row calculations. Replace joins with associations, or move calculations to a later layer, so the cube stays reusable and narrow.
Exercise
You need a new field on a delivered SAP view entity that supports extensions. Should you copy the view or extend it, and why?
Show answer
Extend it with extend view entity. The extension keeps the delivered view intact, so SAP updates still apply. A copy would not receive those updates and would need manual maintenance.