【发布时间】:2020-10-29 19:01:14
【问题描述】:
背景:我是一名长期的 MSSQL 开发人员...我想知道的是如何从 SAP HANA 实现只读一次选择。
高级伪代码:
- 通过 db proc(查询)收集请求
- 通过请求调用 API
- 存储请求(响应)的结果
我有一个表 (A),它是流程输入的来源。进程完成后,会将结果写入另一个表 (B)。
如果我只是在表 A 中添加一列以避免并发处理器从 A 中选择相同的记录,也许这一切都解决了?
我想知道如何在不将列添加到源表 A 的情况下执行此操作。
我尝试的是表 A 和 B 之间的左外连接,以从 A 中获取在 B 中没有(尚未)对应行的行。这不起作用,或者我没有实现这样的行被处理任何处理器仅 1 次。
我有一个存储过程来处理批量选择:
/*
* getBatch.sql
*
* SYNOPSIS: Retrieve the next set of criteria to be used in a search
* request. Use left outer join between input source table
* and results table to determine the next set of inputs, and
* provide support so that concurrent processes may call this
* proc and get their inputs exclusively.
*/
alter procedure "ACOX"."getBatch" (
in in_limit int
,in in_run_group_id varchar(36)
,out ot_result table (
id bigint
,runGroupId varchar(36)
,sourceTableRefId integer
,name nvarchar(22)
,location nvarchar(13)
,regionCode nvarchar(3)
,countryCode nvarchar(3)
)
) language sqlscript sql security definer as
begin
-- insert new records:
insert into "ACOX"."search_result_v4" (
"RUN_GROUP_ID"
,"BEGIN_DATE_TS"
,"SOURCE_TABLE"
,"SOURCE_TABLE_REFID"
)
select
in_run_group_id as "RUN_GROUP_ID"
,CURRENT_TIMESTAMP as "BEGIN_DATE_TS"
,'acox.searchCriteria' as "SOURCE_TABLE"
,fp.descriptor_id as "SOURCE_TABLE_REFID"
from
acox.searchCriteria fp
left join "ACOX"."us_state_codes" st
on trim(fp.region) = trim(st.usps)
left outer join "ACOX"."search_result_v4" r
on fp.descriptor_id = r.source_table_refid
where
st.usps is not null
and r.BEGIN_DATE_TS is null
limit :in_limit;
-- select records inserted for return:
ot_result =
select
r.ID id
,r.RUN_GROUP_ID runGroupId
,fp.descriptor_id sourceTableRefId
,fp.merch_name name
,fp.Location location
,st.usps regionCode
,'USA' countryCode
from
acox.searchCriteria fp
left join "ACOX"."us_state_codes" st
on trim(fp.region) = trim(st.usps)
inner join "ACOX"."search_result_v4" r
on fp.descriptor_id = r.source_table_refid
and r.COMPLETE_DATE_TS is null
and r.RUN_GROUP_ID = in_run_group_id
where
st.usps is not null
limit :in_limit;
end;
当运行 7 个并发处理器时,我得到了 35% 的重叠。也就是说,在 5,000 个输入行中,生成的行数为 6,755。运行时间约为 7 分钟。
目前我的解决方案包括向源表添加一列。我想避免这种情况,但它似乎是一个更简单的实现。我将很快更新代码,但它在插入之前包含一个更新语句。
有用的参考资料:
【问题讨论】:
-
7 分钟。处理 5000 条记录太慢了。不知道为什么需要在“search_results_v4”表中实际存储数据,因为这似乎是一个临时的记录列表。
-
@LarsBr。我更新了我的问题以提供此时间的背景信息。 Db 有 API 调用请求的来源。存储响应以供最新分析。 API 调用时间是最大的成本。
标签: sql hana hana-sql-script