2017-02-23 64 views
-2

我正在处理pl/sql代码。请帮助计算时间以下两种方法。此Oracle查询需要多少额外的计算时间?

方法1:

if count(x)>1 
then 
    select sum(price) into vprice from table1 where x between var1 and var2; 
else 
    select price into vprice from table1 where x=var1; 
end; 

方法2:

select sum(price) from table1 where x between var1 and var2; 

我们确定基于其它条件的代码以上某处VAR1和VAR2的值。五种情况中大约四种情况下,var1和var2的值将相同。

P.S .:请忽略任何语法或逻辑错误。我尝试了解我的计划中发生了什么,以了解实现更高效率的更好方法。

+1

你就不能进行测试,以发现的? –

+2

Method1似乎根据某个值执行两个查询之一。 '如果count(x)> 1'不是有效的PL/SQL,我知道你说我忽略了,但我不知道它要做什么。 Method2似乎与Method1中的第一个查询类似。这两种'方法'与'计算时间'有什么关系,不管它是什么,都不清楚。 –

+1

我假设你问是否用'='而不是'between'(如果有的话)节省了多余的PL/SQL逻辑来决定要遵循哪个分支? –

回答

-1
DECLARE 
    tsStartTime TIMESTAMP; 
    tsEndTime TIMESTAMP; 
BEGIN 
    -- Start the timer 
    tsStartTime := CURRENT_TIMESTAMP; 
    DBMS_OUTPUT.PUT_LINE(tsStartTime); 

    -- DO SOMETHING HERE WHICH YOU WANT TO TIME <------ 

    -- Display some information, and how long "something" took 
    tsEndTime := CURRENT_TIMESTAMP; 
    DBMS_OUTPUT.PUT_LINE(tsEndTime); 

    DBMS_OUTPUT.PUT_LINE('Time elapsed:' || to_char(tsEndTime - tsStartTime)); 
END; 
/
0
EXPLAIN PLAN FOR select sum(price) from table1 where x between var1 and var2; 
SELECT * FROM TABLE (dbms_xplan.display); -- take this output and compare it to the next one 

EXPLAIN PLAN FOR select price from table1 where x=var1; 
SELECT * FROM TABLE (dbms_xplan.display); -- compare it to this output 
+0

上面给出了估计的执行计划,以获得实际的 - 请参阅或者,您可以使用http://stackoverflow.com/questions/14480264/get-runtime-execution-plan-of-a-query – user1327961