我正在尝试调整一段用 Pro*C 编写的大型代码,特别是瓶颈循环和其中的UPDATE
语句。通过“主机结构数组”for loop
循环,该数组可能包含数千条有时数百万条记录,并且更新执行次数过多。更新可以在整个程序中更频繁地执行,但这需要对代码进行重大更改,我无权进行重大更改。
所以我有这样的东西
....
#define NULL_REF_NO 10
#define NULL_ERR 256
....
....
struct s_errors
{
char s_ref_id [NULL_REF_NO];
char s_ref_seq_no [NULL_REF_NO];
char s_err_msg [NULL_ERR];
};
....
....
struct s_errors *ps_errors = NULL;
....
....
/*The part below happens throughout the program to collect all errors*/
/*ls_ref_id, ls_ref_seq_no, and ls_err_msg are local variables of same data type. and i_curr_index is the array index variable*/
strcpy(ls_ref_id, ps_errors[i_curr_index].s_ref_id);
strcpy(ls_ref_seq_no, ps_errors[i_curr_index].s_ref_seq_no);
strcpy(ls_err_msg, ps_errors[i_curr_index].s_err_msg);
.....
/* At this point ps_error contains thousands or even millions of rows*/
/* The final part is to update all these rows back to the table like below*/
/* pl_err_count is a Global var which keeps track of the total number of records in the host structure array*/
int i_curr_index = 0;
char l_ref_id [NULL_REF_NO];
char l_ref_seq_no [NULL_REF_NO];
char l_err_msg [NULL_ERR];
for(i_curr_index = 0; i_curr_index < pl_err_count; i_curr_index++)
{
strcpy(l_ref_id, ps_errors[i_curr_index].s_ref_id);
strcpy(l_ref_seq_no, ps_errors[i_curr_index].s_ref_seq_no);
strcpy(l_err_msg, ps_errors[i_curr_index].s_err_msg);
EXEC SQL
UPDATE some_table
SET status = 'E',
error_message = :l_err_msg
WHERE ref_id = :l_ref_id
AND ref_seq_no = :l_ref_seq_no;
if (SQL_ERROR_FOUND)
{
sprintf(err_data, "Updation failed with sql errors ");
strcpy(table, "some_table");
WRITE_ERROR(SQLCODE,function,"",err_data);
return(FATAL);
}
}
瓶颈是上面的 for 循环(它是程序的最后一步),它循环太多次导致程序运行时间过长。我想知道是否有一种方法可以CAST
将主机结构数组ps_errors
转换为 Oracle 表类型,这样我就可以轻松地进行批量UPDATE
操作,甚至可以MERGE
使用一些并行 DML 进行操作,而无需遍历每条记录。