近一直在做一些ORACLE SQL性能量化方面的研究,要实现较准确的SQL性能量化很复杂,现在一直在小心推进,做了一下一些机器的两个参数测试:
  一个是oracle数据库1毫秒可执行多少次内存hash运算操作,这个主要用于量化hash join的时间;
  一个是oracle数据库1毫秒可执行多少次内存排序的比较运算,这个主要用于量化order by的时间。
  以下是测试结果,让我非常意外。

  以下是测试代码,测试采用单会话sqlplus执行,因此只能衡量主机CPU单核的性能。
  下面这个是hash次数的测试代码:


declare
i             integer;
int1          integer;
t1            number;
t2            number;
t_all         number;
t_select      number;
execute_count number := 10;
function getnumfromtimestamp(iTime timestamp) return number is
Result        number;
v_hour        number;
v_minute      number;
v_second      number;
v_millisecond number;
begin
v_hour   := EXTRACT(hour from iTime);
v_minute := EXTRACT(minute from iTime);
v_second := EXTRACT(second from iTime);
Result   := (v_hour + 1) * 3600 + (v_minute + 1) * 60 + v_second;
return(Result);
end;
begin
select /*+use_hash(a,b)*/
count(*)
into int1
from (select rownum + 20000000 rn from dual connect by rownum <= 1000000) a,
(select rownum + 10000000 rn from dual connect by rownum <= 1000000) b
where a.rn = b.rn;
t1 := getnumfromtimestamp(systimestamp);
for i in 1 .. execute_count loop
select /*+use_hash(a,b)*/
count(*)
into int1
from (select rownum + 20000000 rn
from dual
connect by rownum <= 1000000) a,
(select rownum + 10000000 rn
from dual
connect by rownum <= 1000000) b
where a.rn = b.rn;
end loop;
t2    := getnumfromtimestamp(systimestamp);
t_all := t2 - t1;
select count(*)
into int1
from (select rownum + 20000000 rn from dual connect by rownum <= 1000000);
t1 := getnumfromtimestamp(systimestamp);
for i in 1 .. execute_count loop
select count(*)
into int1
from (select rownum + 20000000 rn
from dual
connect by rownum <= 1000000);
end loop;
t2       := getnumfromtimestamp(systimestamp);
t_select := t2 - t1;
dbms_output.put_line('hash_times:' ||
trunc(1000 /
((t_all - t_select) / (2 * execute_count))));
end;
/

  下面这个是sort次数的测试代码: