You are on page 1of 36

CD161

SQL Script 基础和调试


陈奕玮, SAP 中国研究院
3月, 2014
声明

This presentation outlines our general product direction and should not be relied on in making a
purchase decision. This presentation is not subject to your license agreement or any other agreement
with SAP. SAP has no obligation to pursue any course of business outlined in this presentation or to
develop or release any functionality mentioned in this presentation. This presentation and SAP's
strategy and possible future developments are subject to change and may be changed by SAP at any
time for any reason without notice. This document is provided without a warranty of any kind, either
express or implied, including but not limited to, the implied warranties of merchantability, fitness for a
particular purpose, or non-infringement. SAP assumes no responsibility for errors or omissions in this
document, except if such damages were caused by SAP intentionally or grossly negligent.

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 2


议程

SQLScript 基础
R语言集成
应用函数库(AFL)
用户自定义方法
触发器

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 3


SQLScript 基础
SQLScript
什么是SQLScript?

SQL Script 是应用程序访问HANA数据库的一种接口


对标准ANSI SQL的扩展
用来创建HANA中的存储过程:
 声明逻辑包括SELECT查询, 绑定计算引擎函数( Calculation Engine functions)
 编制逻辑包括数据定义语言(DDL), 数据操纵语言(DML), 赋值, 命令式逻辑

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 5


SQLScript
为什么要有SQLScript?

SQLScript最主要的目标:为了能够在HANA数据库内执行大量复杂的计算
从而达到性能最优:
 消除了大量数据从数据库到中间层的传输成本
 把计算推送到数据库层为了最大限度地发挥SAP HANA内存数据库的优势,例如:列操作, 查询优化和并行执行。
相反,若在中间层处理这些数据,将无法利用这些性能特点。

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 6


SQLScript
优势

相比较于最普通的SQL 查询, SQL Script 具有如下优势:


 函数可以返回多个结果集, 相比普通SQL只能返回一个结果集。
 一个复杂的函数可以拆分成多个方法。这样做更有利于模块化编程,代码重用以及对复杂函数功能有更好的抽象
理解。对于复杂的结构化查询,标准SQL只允许定义SQL视图。然而,SQL 视图是没有变量的。
 SQLScript 支持隐式声明本地变量对中间结果进行赋值。 与标准SQL相比,它需要定义全局的可见的视图,即使只
为了操作某个中间步骤。
 SQL Script支持流程控制例如if/else,这点在标准SQL中是不支持的。

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 7


SQLScript
性能加速

表现层逻辑

控制逻辑

HANA
计算逻辑

数据 SQLScript

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 8


SQLScript
传统模型vs. HANA 模型

传统模式: “Data to Code” HANA模式: “Code to Data”

应用层 应用层
代码
传输最小
大数据传输
存在瓶颈 结果集

数据层 数据层
代码

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 9


SQLScript
代码案列
BEGIN
... Products
-- Query 1
product_ids = select "ProductId", "Category", "DescId"
from "SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::products"
where "Category" = 'Notebooks'
or "Category" = 'PC'; Q1
-- Query 2
product_texts = select "ProductId", "Category", "DescId", "Text"
from :product_ids as prod_ids
inner join "SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::texts" Q2
as texts on prod_ids."DescId" = texts."TextId";

-- Query 3
out_notebook_count = select count(*) as cnt from
:product_texts where "Category" = 'Notebooks';
Q3 Q4
-- Query 4
out_pc_count = select count(*) as cnt from
:product_texts where "Category" = 'PC';

...
Notebooks PCs
END;

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 10


SQLScript
并行化处理

SELECT 语句除了在如下情况下都将并行执行:
 在存储过程中有用到任何的本地标量或变量
 在同一存储过程中混合读写操作,混合使用DML/DDL
 在存储过程中含有命令式逻辑
 存在SQL语句结果未分配给某一变量

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 11


SQLScript
CE(Calculation Engine) 函数
SQL CE-Built In Function
SELECT on column table out = SELECT A, B, C from "COLUMN_TABLE" out = CE_COLUMN_TABLE("COLUMN_TABLE", [A, B, C])

SELECT on attribute view out = SELECT A, B, C from "ATTRIBUTE_VIEW" out = CE_JOIN_VIEW("ATTRIBUTE_VIEW", [A, B, C])

SELECT on olap view out = SELECT A, B, C, SUM(D) from out = CE_OLAP_VIEW("ANALYTIC_VIEW", [A, B, C]);
"ANALYTIC_VIEW" GROUP BY A, B, C

WHERE HAVING out = SELECT A, B, C, SUM(D) from col_tab= CE_COLUMN_TABLE("COLUMN_TABLE"); out =


"ANALYTIC_VIEW" WHERE B = 'value' AND C = CE_PROJECTION(col_tab, [A, B, C], ' "B" = ''value'' AND "C" =
'value' ''value'' ');

GROUP BY out = SELECT A, B, C, SUM(D) FROM"COLUMN_TABLE" col_tab= CE_COLUMN_TABLE("COLUMN_TABLE"); out =


GROUP BY A, B, C CE_AGGREGATION( (col_tab, SUM(D), [A, B, C]);

INNER JOIN out = SELECT A, B, Y, SUM(D) from "COLTAB1" out = CE_JOIN("COLTAB1","COLTAB2", [KEY1, KEY2], [A, B, Y, D])
INNER JOIN "COLTAB2" WHERE "COLTAB1"."KEY1" =
"COLTAB2"."KEY1" AND "COLTAB1"."KEY2" =
"COLTAB2"."KEY2"

LEFT OUTER JOIN out = SELECT A, B, Y, SUM(D) from "COLTAB1" out = CE_LEFT_OUTER_JOIN("COLTAB1","COLTAB2", [KEY1, KEY2],
LEFT OUTER JOIN "COLTAB2" WHERE [A, B, Y, D])
"COLTAB1"."KEY1" = "COLTAB2"."KEY1" AND
"COLTAB1"."KEY2" = "COLTAB2"."KEY2"
SQL Expressions out = SELECT A, B, C, SUBSTRING(D,2,5) FROM proj_tab = CE_COLUMN_TABLE("COLUMN_TABLE"); out =
"COLUMN_TABLE" CE_PROJECTION( :proj_tab, ["A", "B", "C",
CE_CALC('midstr("D",2,5)', string) ]);

UNION ALL col_tab1 = SELECT A, B, C, D FROM col_tab1 = CE_COLUMN_TABLE("COLUMN_TABLE1",[A,B,C,D]);


"COLUMN_TABLE1"; col_tab2 = SELECT A, B, C, D col_tab2 = CE_COLUMN_TABLE("COLUMN_TABLE2",[A,B,C,D]); out =
FROM "COLUMN_TABLE2"; out = SELECT * FROM CE_UNION_ALL(:col_tab1,:col_tab2);
:col_tab1 UNION ALL SELECT * FROM :col_tab2;

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 12


SQLScript
CE 函数代码举例
• 尽可能分开使用CE Function
• 将获得更好的查询性能(优化,并行)

bp_addresses =
select a."PartnerId", a."PartnerRole", a."EmailAddress", a."CompanyName",
a."AddressId", b."City", b."PostalCode", b."Street"
from "SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::businessPartner" as a
inner join "SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::addresses" as b
SQL
on a."AddressId" = b."AddressId" where a."PartnerRole" = :partnerrole;

lt_bp = CE_COLUMN_TABLE("SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::businessPartner",
["PartnerId", "PartnerRole", "EmailAddress", "CompanyName", "AddressId" ]);

lt_bp_proj = CE_PROJECTION(:lt_bp, ["PartnerId", "PartnerRole", "EmailAddress" , "CompanyName",


"AddressId" ], '"PartnerRole" = :partnerrole' ); CE
lt_address = CE_COLUMN_TABLE("SAP_HANA_EPM_DEMO"."sap.hana.democontent.epm.data::addresses",
["AddressId", "City", "PostalCode", "Street"]);
Functions
bp_addresses = CE_JOIN(:lt_bp_proj, :lt_address, ["AddressId"],
["PartnerId", "PartnerRole", "EmailAddress", "CompanyName",
"AddressId", "City", "PostalCode", "Street"]);

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 13


SQLScript
开发界面
• 语法校验
• 代码批注
• 语法关键词高亮
• 本地表变量

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 14


SQLScript
调试界面

变量评估

设置断点

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 15


HANA 集成 R语言
R 语言
什么是R语言
开源编程语言,主要应用于计算统计和图像处理领域,目
前拥有超过 3000个功能包. http://www.r-project.org/
其功能包覆盖多个领域-
 聚类分析
 概率分布
 计算计量经济学
 实证金融
 统计遗传学
 图像分析
 机器学习&统计学习
 医学图像分析
 多元统计
 自然语言处理
 社会科学统计
 时间序列分析

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 17


R 语言
R 的功能特点

• 数据处理和存贮: 数值的和文本的
• 矩阵计算
• 支持Hash表和正则表达式
• 提供专业的数据分析统计函数
• 编程语言:循环,分支, 子程序, 面对对象

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 18


R 语言
代码案例

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 19


R 语言
与HANA的集成
SAP HANA SQLScript 实例代码

NewDB Space OpenSource R Space

Calc. Engine
Join OLAP
R External
OP OP
Packages
ROP (Forecasting,
Parallelism,
statistics, etc.)

2 Run the R scripts

RClient
1 Send data SAP RHANA
and R script
Package

3 Get back the


result from R to
SAP HANA

NewDB R Integration Open Source R

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 20


应用函数库(AFL)
应用函数库(AFL)

Application Function Library (AFL) – 什么是应用函数库?


在SAP HANA中预置了目前通用的一些商业函数库和预测分析库,应用于项目开发或行业解决方案之
中。

AFL在SAP HANA内存数据库中执行。

AFL所带来的便利
AFL提供的算法可以直接应用与项目开发中,无需额外耗费人力财力去实现这些复杂的逻辑算法来满
足客户的业务需求,从而加快项目的开发进度。 AFL 带来更好的性能, 其根据HANA内存数据库特性
量身开发。

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 22


应用函数库(AFL)

Application Functions (C++)

Predictive Analysis Library Business Function Library

• 商业函数库(BFL) 是 AFL中的一大类,主要提供商业金融相关的分析算法。
• 预测分析库(PAL) 是AFL中的另一大集合,主要提供预测分析,数据挖掘领域的分析算法。

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 23


应用函数库(AFL)

AFL 技术框架包括
HANA Clients (App Server, Analytics Technology, etc)
Application Functions
– AFL底层由C++实现
– PAL和BFL已经在HANA SPS05发布,并且在SPS06中加入了数 SAP HANA
据质量函数库(Data Quality Functions Library)
SQLScript

AFL 框架 Parameter Table


– AFL 需要通过调用SQLScript 来使用 。

– 在SAP HANA SP06版本中将推出图形界面,来更好地部署AFL AFL Framework


存储过程,并供其他开发者使用。

Application Functions (C++)

Predictive Analysis Business Function


Library Library

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 24


商业函数库(BFL)

• 集成于HANA数据库当中
• 支持多元输入变量
• 内嵌计算引擎(calculation engine)

帮助客户解决。。。。。。
重用这些内置函数,减少额外开发成本。
快速构建应用

提高计算性能 利用内存技术真正实现实时计算。

提高商业解决能力 把决策支持功能以一种简明扼要的方式带给用户

*no update to the BFL with SPS06.

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 25


预测分析库(PAL)

• 集成于HANA数据库当中
• 支持的算法: K-Means, 关联分析, C4.5 决策树, 线性回归, 指数平滑…

帮助客户解决。。。。。。
深入了解业务 更深层次的商业视角: 关联规则, 客户群体, 或销售预测

科学决策 让决策更具科学性

提高计算性能 利用内存技术真正实现实时计算。

提高商务解决能力 把决策支持功能以一种简明扼要的方式带给用户

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 26


应用函数建模(AFM)

 通过图形界面,更快速方便地进行AFL开发。
 AFL 模型存储在资源库中,以便其他开发者使用。

功能列表

 模型编辑器

SQL示例代码

参数设置

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 27


用户自定义函数(UDF)
用户自定义函数(User Defined Functions)
综述

通过SQL 控制台对用户自定函数进行管理。
目前仅支持用SQL Script编写用户自定义函数。
只读,支持两种变量的输出:
 表变量
 标量

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 29


用户自定义函数
返回表变量

• 支持多个输入参数
• 返回一个表变量
• 支持各类表间操作

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 30


用户自定义函数
返回标量

• 支持多个输入参数
• 返回一个标量
• 支持表达式
• 不支持游标,CE function 和数组操作
• 输入参数不能是表类型

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 31


触发器
触发器
综述

• 一种特殊的存储过程,一旦某一数据库事件发生
(例如一条记录被插入某张表中),这一存储过
程将被自动触发执行
• 通过SQL 控制台对触发器对象进行管理。

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 33


Further Information

SAP Public Web


scn.sap.com
www.sap.com

SAP Education and Certification Opportunities


www.sap.com/education

Watch SAP D-code Online


www.sapteched.com/online

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 34


SAP TechEd Virtual Hands-on Workshops and SAP D-code Online
Continue your SAP D-code education after the event!

SAP D-code Virtual Hands-on Workshops SAP D-code Online


 Access hands-on workshops post-event  Access replays of keynotes, Demo Jam, SAP D-code
 Available January – March 2014 LIVE interviews, select lecture sessions, and more!
 Complementary with your SAP D-code registration  View content only available online
http://saptechedhandson.sap.com/ http://sapteched.com/online

© 2013 SAP AG or an SAP affiliate company. All rights reserved. 35


反馈
请完成CDC161的课程评估.

感谢您的参与。

You might also like