【问题标题】:Overhead of an If statement inside cursor loop vs a cursor select游标循环内的 If 语句与游标选择的开销
【发布时间】:2013-07-25 18:06:27
【问题描述】:

假设我有一个接收 siteId 和 empId 的过程 这使得主键 (siteId emp.site_id%TYPE, empId emp.emp_id%TYPE) 我正在尝试创建一个 xml clob,我有一个名为 createParent() 的方法,它创建一个父节点,另一个名为 put_child_node 的方法,它接受 parentNode、子元素名称和子元素值,但是名称和属性取自两个表的连接。见代码:

DECLARE 
cursor has_emp_attribute
select 1
from emp_attribute 
where site_id = siteId
and emp_id = empId; <-- quick select

cursor get_emp_attributes
select an.name, ea.attribute_value
from attribute_name an, emp_attribute ea
where an.attribute_id = ea.attribute_id
and   ea.site_id = siteId
and   ea.emp_id = empId;

hasAttribute boolean;
parentNode xmldom.domnode;
BEGIN
  hasAttribute := false
  for has_emp_attribute_rec in has_emp_attribute
  loop
    hasAttribute := true;
    parentNode = createParentNode();
    exit;
  end loop;
  if (hasAttribute) then
    for get_emp_rec in get_emp_attributes
        loop
           put_child_node(parentNode, get_emp_rec.name, get_emp_rec.attribute_value);
        end loop;
  end if;
END;

如果我使用 if 语句检查第二个游标内是否有记录,并在其中创建 parentNode,这将是一个更好的解决方案:

cursor get_emp_attributes
select an.name, ea.attribute_value
from attribute_name an, emp_attribute ea
where an.attribute_id = ea.attribute_id
and   ea.site_id = siteId
and   ea.emp_id = empId;

hasAttribute boolean;
parentNode xmldom.domnode
BEGIN
  hasAttribute := false
  for get_emp_rec in get_emp_attributes
        loop
           if(hasAttribute = false) then
           parentNode := createParentNode();
               hasAttribute := true;
           end if;
           put_child_node(parentNode, get_emp_rec.name, get_emp_rec.attribute_value);
        end loop;
 END;

【问题讨论】:

    标签: if-statement plsql oracle11g cursor


    【解决方案1】:

    如果您在一次调用中只创建一个子节点,则第二种方式效率较高,因为游标和循环的数量较少。只需确保您的游标查询获取特定父节点的子节点即可。

    【讨论】:

    • 如果只有一个子节点是这样,但创建的子节点可能很多。
    • 如果子节点属于同一个父节点,它也可以正常工作。总的来说,这个块将采用父节点并将所有节点创建为子节点,这些节点将在光标中获取
    猜你喜欢
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2013-05-16
    相关资源
    最近更新 更多