Professional Documents
Culture Documents
▪ Weekly assignments / final exam Using the Optimizer for SQLScript Programming in SAP HANA
▪ Discussion forum
Week 1
▪ Use your own SAP HANA Understanding SAP HANA SQLScript Optimizer
system for practice
Week 2
SAP HANA SQLScript Supportability Features
Final Exam
open@sap.com
Follow all of SAP
www.sap.com/contactsap
By the end of this week you will Using the Optimizer for SQLScript Programming in SAP HANA
understand
Week 1
▪ How SQLScript is executed
Understanding SAP HANA SQLScript Optimizer
▪ How SQLScript optimizer works
▪ The difference between SQLScript
optimizer and SQL optimizer
Week 2
▪ How SQLScript works with SQL plan SAP HANA SQLScript Supportability Features
cache
▪ General analysis strategy for SQLScript
Week 3
performance issues
Investigating SQLScript Performance Caused by SQL
Final Exam
CALL HELLO('');
CALL HELLO('');
▪ Compilation
▪ Optimization CALL HELLO('');
▪ Execution
Compilation
Optimization
Execution
▪ Compilation
▪ Optimization CALL HELLO('');
▪ Execution
Compilation
Optimization
Execution
▪ Compilation Plan
Cache
▪ Optimization CALL HELLO('');
▪ Execution
Compilation
The SQLScript execution plan is cached in SQL
plan cache
Optimization
Execution
▪ Compilation Plan
Cache
▪ Optimization CALL HELLO('');
▪ Execution
Execution
“Execution” phase
Compilation Compilation
Optimization Optimization
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 11
SAP HANA SQLScript optimizer
CREATE PROCEDURE HELLO(IN P NVARCHAR(8))
SQLScript optimizer and SQL optimizer AS BEGIN
DECLARE V_NAME NVARCHAR(8) = :P;
IF :P = '' THEN
V_NAME = 'SAP HANA';
“Execution” phase
Compilation Compilation
Optimization Optimization
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 12
SAP HANA SQLScript optimizer
CREATE PROCEDURE HELLO(IN P NVARCHAR(8))
SQLScript optimizer and SQL optimizer AS BEGIN
DECLARE V_NAME NVARCHAR(8) = :P;
IF :P = '' THEN
V_NAME = 'SAP HANA';
“Execution” phase
Compilation Compilation
The SQLScript engine reshapes SQL
statements during the “Optimization” phase,
before passing them to the SQL engine
stmt Optimization Optimization
stmt’
stmt”
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 13
SAP HANA SQLScript optimizer
SQLScript and SQL plan cache
Plan Cache
1. The SQLScript execution plan for
CALL HELLO(‘’);
is inserted into the SQL plan cache.
CALL HELLO('');
Compilation Compilation
Optimization Optimization
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 14
SAP HANA SQLScript optimizer
SQLScript and SQL plan cache
Plan Cache
1. The SQLScript execution plan for
CALL HELLO(‘’);
is inserted into the SQL plan cache.
2. The SQLScript engine passes
CALL HELLO(''); SELECT 'HELLO, ' ... FROM DUMMY;
SELECT ‘HELLO, ’ … FROM DUMMY;
to the SQL engine.
Compilation Compilation
3. The SQL query plan for
SELECT ‘HELLO, ’ … FROM DUMMY;
is inserted into the SQL plan cache too.
Optimization Optimization
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 15
SAP HANA SQLScript optimizer
SQLScript and SQL plan cache
Plan Cache
1. The SQLScript execution plan for
CALL HELLO(‘’);
is inserted into the SQL plan cache.
2. The SQLScript engine passes
CALL HELLO('');
SELECT ‘HELLO, ’ … FROM DUMMY;
to the SQL engine.
Compilation Compilation
3. The SQL query plan for
SELECT ‘HELLO, ’ … FROM DUMMY;
is inserted into the SQL plan cache too.
4. For the next CALL HELLO(‘’), the execution plan Optimization Optimization
is reused without the SQLScript
compilation/optimization phases.
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 16
SAP HANA SQLScript optimizer
SQLScript and SQL plan cache
Plan Cache
1. The SQLScript execution plan for
CALL HELLO(‘’);
is inserted into the SQL plan cache.
2. The SQLScript engine passes
CALL HELLO(''); SELECT 'HELLO, ' ... FROM DUMMY;
SELECT ‘HELLO, ’ … FROM DUMMY;
to the SQL engine.
Compilation Compilation
3. The SQL query plan for
SELECT ‘HELLO, ’ … FROM DUMMY;
is inserted into the SQL plan cache too.
4. For the next CALL HELLO(‘’), the execution plan Optimization Optimization
is reused without the SQLScript
compilation/optimization phases.
5. For the next SELECT ‘HELLO, ’ … FROM
DUMMY, the query plan is reused without the SQL Execution Execution
compilation/optimization phases.
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 17
SAP HANA SQLScript Optimizer
Key takeaways
open@sap.com
Follow all of SAP
www.sap.com/contactsap
Constant
propagation
SQL statement
inlining and
parallelization
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2
SAP HANA SQLScript optimization rules – Part 1
Constant propagation
Evaluate the SQL expression and folding constant. CREATE PROCEDURE HELLO(IN P NVARCHAR(8))
AS BEGIN
Propagate the value through the value chain.
DECLARE V_NAME NVARCHAR(8) = :P;
IF :P = '' THEN
When an empty string ‘’ is given as an input V_NAME = 'SAP HANA';
parameter of the SQLScript procedure HELLO, this
END IF;
constant value is used in compile time.
SELECT 'HELLO, ' || :V_NAME FROM DUMMY;
END;
CALL HELLO('');
Evaluate the SQL expression and folding constant. CREATE PROCEDURE HELLO( '' )
AS BEGIN
Propagate the value through the value chain.
DECLARE V_NAME NVARCHAR(8) = '';
IF '' = '' THEN
When an empty string ‘’ is given as an input V_NAME = 'SAP HANA';
parameter of the SQLScript procedure HELLO, this
END IF;
constant value is used in compile time.
SELECT 'HELLO, ' || :V_NAME FROM DUMMY;
The SQLScript optimizer propagates the empty
END;
string ‘’ as a constant to all positions of variable P.
CALL HELLO('');
CALL HELLO('');
CALL HELLO('');
SQL statements with dependency can be merged RESULT = SELECT 'HELLO' FROM DUMMY;
into a single SQL statement. SELECT * FROM DUMMY, :RESULT;
END;
SQL statements with dependency can be merged WITH "_SYS_RESULT_2" AS (SELECT 'HELLO' FROM DUMMY)
into a single SQL statement. SELECT * FROM DUMMY, "_SYS_RESULT_2" "RESULT" ;
END;
Dead-code elimination
▪ Statements, if it is guaranteed that their
generated results will never be used.
▪ Unreachable branches, if it is determined that the
conditions for branches are evaluated as false
after constant propagation.
CALL HELLO('HI');
open@sap.com
Follow all of SAP
www.sap.com/contactsap
Constant
propagation
SQL statement
inlining and
parallelization
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC 2
SAP HANA SQLScript optimization rules – Part 2
When “CALL P1(‘TA’)” is executed
A base table and table type CREATE TABLE TAB1 (MATERIAL NVARCHAR(10), FABRIC_ID NVARCHAR(10));
INSERT INTO TAB1 VALUES ('ABC' , 'BI');
CREATE TYPE T_RESULT AS TABLE (MATERIAL NVARCHAR(10), FABRIC_ID NVARCHAR(10));
Constant propagation
Constant propagation
Constant propagation
Constant propagation
Procedure flattening
Procedure flattening
V_F_ID = 'BI';
V_F_ID = 'BI';
AS BEGIN
WITH "_SYS_T_TABLE_2" AS
(SELECT DISTINCT MATERIAL, FABRIC_ID FROM TAB1
WHERE 'BI' = '' OR 'BI' = FABRIC_ID),
"_SYS_T_RESULT_1" AS
(SELECT * FROM "_SYS_T_TABLE_2" "T_TABLE"
ORDER BY MATERIAL)
SELECT * FROM "_SYS_T_RESULT_1" "COMPOSITION"
ORDER BY MATERIAL;
END;
open@sap.com
Follow all of SAP
www.sap.com/contactsap
......
Compilation Compilation
Optimization Optimization
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 2
SQLScript general analysis strategy
Find a dominant step – Imperative constructs CREATE PROCEDURE HELLO(IN P NVARCHAR(8))
AS BEGIN
DECLARE V_NAME NVARCHAR(8) = :P;
......
▪ Execution in SQLScript
(= anti-patterns of SQLScript code) CALL HELLO(''); SELECT ...... ;
– Row-based calculation
– Inefficient usage of imperative constructs
Compilation Compilation
Optimization Optimization
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 3
SQLScript general analysis strategy
Find a dominant step – Imperative constructs CREATE PROCEDURE HELLO(IN P NVARCHAR(8))
AS BEGIN
DECLARE V_NAME NVARCHAR(8) = :P;
......
Optimization Optimization
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 4
SQLScript general analysis strategy
Find a dominant step – SQL statement CREATE PROCEDURE HELLO(IN P NVARCHAR(8))
AS BEGIN
DECLARE V_NAME NVARCHAR(8) = :P;
......
▪ Execution in SQLScript
(= anti-patterns of SQLScript code) CALL HELLO(''); SELECT ...... ;
– Row-based calculation
– Inefficient usage of imperative constructs
Compilation Compilation
▪ Processing in SQL engine
– Long compilation of passed SQL statements
– Long execution of passed SQL statements
Optimization Optimization
Execution Execution
© 2023 SAP SE or an SAP affiliate company. All rights reserved. ǀ PUBLIC SQLScript Engine SQL Engine 5
SQLScript general analysis strategy
Find a dominant step – SQL statement
open@sap.com
Follow all of SAP
www.sap.com/contactsap