在日常开发中,对于用到游标的情况,使用批量操作比逐条处理效果更好, 如下例子:
DECLARE
TYPE prod_tab IS TABLE OF products%ROWTYPE;
products_tab prod_tab := prod_tab();
start_time NUMBER;
end_time NUMBER;
CURSOR products_data IS
SELECT * FROM products;
BEGIN
start_time := dbms_utility.get_time;
OPEN products_data;
LOOP
products_tab.extend;
FETCH products_data
INTO products_tab(products_tab.last);
IF products_data%NOTFOUND
THEN
products_tab.delete(products_tab.last);
EXIT;
END IF;
END LOOP;
CLOSE products_data;
end_time := dbms_utility.get_time;
dbms_output.put_line('conventional(' || products_tab.count || ')
:' || to_char(end_time - start_time));
start_time := dbms_utility.get_time;
OPEN products_data;
FETCH products_data BULK COLLECT
INTO products_tab;
CLOSE products_data;
end_time := dbms_utility.get_time;
dbms_output.put_line('bulk COLLECT(' || products_tab.count || ')
:' || to_char(end_time - start_time));
END;