Optimizing Complex Queries in SAP HANA for Real-Time Analytics
SAP HANA is a high-performance in-memory relational database. However, even with the power of in-memory processing, poorly structured queries containing heavy Joins, nested sub-selections, or inefficient calculations can degrade the performance of corporate ERP instances and freeze Power BI dashboards.
In this technical article, I detail some key SQL syntax and tuning strategies in SAP HANA that I applied to optimize analytical reports and data pipelines.
1. Avoid Joining Row and Column Store Tables
SAP HANA stores tables physically in two formats: Row Store and Column Store. Performing heavy Joins between tables of opposite types requires the database engine to perform in-memory conversions at runtime.
- Best practice: Keep analytical queries operating preferentially on Column Store tables.
2. Prefer Native HANA Functions
Instead of performing string manipulations and complex date conversions using generic SQL logic, use optimized HANA kernel functions, such as DAYS_BETWEEN or UTCTOLOCAL.
-- Optimized invoice aging calculation example
SELECT
"DocEntry",
"DocDate",
DAYS_BETWEEN("DocDueDate", CURRENT_DATE) AS "DaysOverdue"
FROM "OINV"
WHERE "DocStatus" = 'O';
Read the plan before blaming Power BI
DAYS_BETWEEN only helps if the optimizer does not materialize all of OINV. This statement has no join. In the plan, look for COLUMN SEARCH and an early DocStatus predicate. A row-store to column-store conversion shows up when the query joins the two formats. That operator is the one I reject. The password does not go on the command line: hdbuserstore SET ANALYTICS hana-prod:30015 ANALYTICS stores the key, and hdbsql -U uses it. -p shows up in the process list.
hdbuserstore SET ANALYTICS hana-prod:30015 ANALYTICS
hdbsql -m -U ANALYTICS <<'SQL'
EXPLAIN PLAN SET STATEMENT_NAME = 'oinv_aging' FOR
SELECT "DocEntry",
DAYS_BETWEEN("DocDueDate", CURRENT_DATE) AS "DaysOverdue"
FROM "SBO_PROD"."OINV"
WHERE "DocStatus" = 'O';
SELECT OPERATOR_NAME, OPERATOR_DETAILS, TABLE_NAME
FROM EXPLAIN_PLAN_TABLE
WHERE STATEMENT_NAME = 'oinv_aging';
SQL
If the plan shows COLUMN SEARCH on OINV and the DocStatus predicate applied early, the refresh stops scanning the whole table. On the report that joined row store to column store, that cut took the load from 15 minutes to under 45 seconds. The figure belongs to that dashboard.
Conclusion
The query optimization reduced the Power BI load refresh time from over 15 minutes to less than 45 seconds, relieving stress on production ERP CPUs.
Related Articles
SAP Business One with SAP HANA on AWS: Architecture and Day-to-Day Operations
How to run SAP Business One on SAP HANA on AWS: instance, data and log volumes, port 30015, and backup. Power BI reads the replica, not production HANA.
Predictive Billing AI in Telecom/Energy Running on Oracle Solaris
Case study of a Python script using RandomForest for predicting default, integrated with the SAP HANA database in a UNIX Oracle Solaris environment.