本地 PL/SQL 数组与 SQL 调用的性能
Performance of Local PL/SQL Array Vs SQL Call
如果我使用 oracle sql 和 plsql 对员工进行计算,然后 select 根据这些计算的结果获取更多数据...当 select 对员工进行 select 我可能需要的所有数据时(假设有 20 行,每行 5 列),然后将该数据用作本地数组,会不会更快,或者 select 完成后我需要的那一行?
-- Example with multiple selects
declare
l_employeeID number;
l_birthday date;
l_horoscope varchar2;
begin
select employeeID into l_employeeID from employeeTbl t where t.rownum = 1;
l_birthday := get_birthdayFromID(l_employeeID);
select horoscope into l_horoscope from horoscopeTable t where l_birthday between l_horoscope.fromDate and l_horoscope.toDate;
return l_horoscope;
end;
-- Example with table selection, and loop iteration
declare
l_empolyeeID number;
l_birthday date;
l_horoscope varchar2;
l_horoscopeDates date_table;
begin
select employeeID, cast(multiset(select horoscopeDate from horoscopeTable)) as date_table into l_employeeID, l_horoscopeDates from employeeTbl t where t.rownum = 1;
l_birthday := get_birthdayFromID(l_employeeID);
for i in 1 .. l_horoscopeDates.count - 1 loop
if l_birthday between l_horoscopeDates(i) and l_horoscopeDates(i + 1) then
return l_horoscopeDates(i);
end if;
end loop;
return null;
end;
我知道我要为 select 额外的数据支付更多的 ram 和 IO,但是当额外数据不是很明显时,它是否比调用另一个上下文切换来调用 sql 更有效比需要的大?
使用 Oracle 时,上下文切换被认为非常广泛。如果 table 不包含大量数据,您应该明确地查询更多数据以减少 PL/SQL 进行 SQL 查询的次数。
话虽如此,我认为您的问题应该反过来,当您的整个逻辑可以总结为一个 SQL 语句时,您为什么还要使用 PL/SQL?
示例 -
select horoscope
from horoscopeTable
where (select get_birthdayFromID(employeeID)
from employeeTbl
where t.rownum = 1) between l_horoscope.fromDate and l_horoscope.toDate;
语法可能需要一些修改,但主要思想对我来说似乎是正确的。
您还可以在这篇文章中找到关于调整 PL/SQL 代码的更多观察 - http://logicalread.solarwinds.com/sql-plsql-oracle-dev-best-practices-mc08/
如果我使用 oracle sql 和 plsql 对员工进行计算,然后 select 根据这些计算的结果获取更多数据...当 select 对员工进行 select 我可能需要的所有数据时(假设有 20 行,每行 5 列),然后将该数据用作本地数组,会不会更快,或者 select 完成后我需要的那一行?
-- Example with multiple selects
declare
l_employeeID number;
l_birthday date;
l_horoscope varchar2;
begin
select employeeID into l_employeeID from employeeTbl t where t.rownum = 1;
l_birthday := get_birthdayFromID(l_employeeID);
select horoscope into l_horoscope from horoscopeTable t where l_birthday between l_horoscope.fromDate and l_horoscope.toDate;
return l_horoscope;
end;
-- Example with table selection, and loop iteration
declare
l_empolyeeID number;
l_birthday date;
l_horoscope varchar2;
l_horoscopeDates date_table;
begin
select employeeID, cast(multiset(select horoscopeDate from horoscopeTable)) as date_table into l_employeeID, l_horoscopeDates from employeeTbl t where t.rownum = 1;
l_birthday := get_birthdayFromID(l_employeeID);
for i in 1 .. l_horoscopeDates.count - 1 loop
if l_birthday between l_horoscopeDates(i) and l_horoscopeDates(i + 1) then
return l_horoscopeDates(i);
end if;
end loop;
return null;
end;
我知道我要为 select 额外的数据支付更多的 ram 和 IO,但是当额外数据不是很明显时,它是否比调用另一个上下文切换来调用 sql 更有效比需要的大?
使用 Oracle 时,上下文切换被认为非常广泛。如果 table 不包含大量数据,您应该明确地查询更多数据以减少 PL/SQL 进行 SQL 查询的次数。
话虽如此,我认为您的问题应该反过来,当您的整个逻辑可以总结为一个 SQL 语句时,您为什么还要使用 PL/SQL? 示例 -
select horoscope
from horoscopeTable
where (select get_birthdayFromID(employeeID)
from employeeTbl
where t.rownum = 1) between l_horoscope.fromDate and l_horoscope.toDate;
语法可能需要一些修改,但主要思想对我来说似乎是正确的。
您还可以在这篇文章中找到关于调整 PL/SQL 代码的更多观察 - http://logicalread.solarwinds.com/sql-plsql-oracle-dev-best-practices-mc08/