它们实际上是相同的。 CPU 周期的任何微小差异都会被执行查询所需的 I/O、网络开销等所掩盖。
我想between 版本可以让您将最频繁出现的值放在首位,理论上如果大部分值落在一个或两个范围内,则效率会提高。再说一次,< 版本每个案例只需要一次比较,而不是两次。
我还是给<版加分吧:)
我在 PL/SQL 游标循环中测试了这两个版本以消除网络问题,并多次运行测试。结果在超过 1000 万次迭代中彼此相差半秒之内,这与多次运行的变化大致相同(因为在任何服务器或笔记本电脑上总是在运行其他东西)。 p>
create table t1 (c1, c2, c3) pctfree 0 nologging
as
select round(dbms_random.value(1,100))
, cast(dbms_random.string('X',40) as varchar2(40))
, cast(dbms_random.string('X',40) as varchar2(40))
from xmltable('1 to 1000000');
declare
type test_rectype is record(num number);
totalTime1 simple_integer := 0;
totalTime2 simple_integer := 0;
time1Percentage simple_integer := 0;
time2Percentage simple_integer := 0;
function testCursor
( cursorNum in integer )
return sys_refcursor
is
testCursor sys_refcursor;
begin
if cursorNum = 1 then
open testCursor for
select case
when c1 < 20 then 0
when c1 < 50 then 20
when c1 < 100 then 40
when c1 < 200 then 75
when c1 < 1000 then 86
end as t_c1
from t1;
elsif cursorNum = 2 then
open testCursor for
select case
when c1 < 20 then 0
when c1 < 50 then 20
when c1 < 100 then 40
when c1 < 200 then 75
when c1 < 1000 then 86
end as t_c1
from t1;
end if;
return testCursor;
end testCursor;
-- Fetch all rows from a cursor and return time in hundredths of a second:
procedure time_cursor
( inCursor in sys_refcursor
, outTime in out nocopy simple_integer )
is
startTime simple_integer := dbms_utility.get_time;
begin
-- 21c new iterator syntax
for r test_rectype in values of inCursor loop
null; -- Could also compare rowcounts here
end loop;
outTime := dbms_utility.get_time - startTime;
close inCursor;
end time_cursor;
-- Report timing difference:
procedure print_comparison
( time1 simple_integer
, time2 simple_integer )
is
begin
time1Percentage := 100 * time1 / (time1 + time2);
time2Percentage := 100 * time2 / (time2 + time2);
dbms_output.put_line('Between: '||to_char(time1/100,'900d00')|| rpad(' |',time1Percentage,'|'));
dbms_output.put_line('LessThan: '||to_char(time2/100,'900d00')|| rpad(' |',time2Percentage,'|'));
end print_comparison;
procedure compare_cursors
( runningTime1 in out nocopy simple_integer
, runningTime2 in out nocopy simple_integer )
is
testCursor1 sys_refcursor := testCursor(1);
testCursor2 sys_refcursor := testCursor(2);
time1 simple_integer := 0;
time2 simple_integer := 0;
time1Percentage simple_integer := 0;
time2Percentage simple_integer := 0;
begin
time_cursor(testCursor1, time1);
time_cursor(testCursor2, time2);
print_comparison(time1, time2);
-- Update running totals:
runningTime1 := nvl(runningTime1,0) + nvl(time1,0);
runningTime2 := nvl(runningTime2,0) + nvl(time2,0);
dbms_output.new_line;
end compare_cursors;
begin
for i in 1..10 loop
compare_cursors(totalTime1, totalTime2);
end loop;
dbms_output.put_line('Total:'||chr(10));
print_comparison(totalTime1, totalTime2);
end;
Between: 02.18 ||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.25 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.03 |||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.03 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.13 |||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.13 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.13 ||||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.07 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.16 ||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.27 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.28 |||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.28 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.17 ||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.27 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.24 ||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.30 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.24 |||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.27 |||||||||||||||||||||||||||||||||||||||||||||||||
Between: 02.25 ||||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 02.18 |||||||||||||||||||||||||||||||||||||||||||||||||
Total:
Between: 21.81 |||||||||||||||||||||||||||||||||||||||||||||||||
LessThan: 22.05 |||||||||||||||||||||||||||||||||||||||||||||||||