2011-10-31 61 views
8

有這樣的電話SYSDATE差異

some_proc(sysdate, sysdate); 
select some_func(sysdate, sysdate) from dual 

我不知道是否有任何可能兩個SYSDATE通話將給予不同的價值觀?由於執行速度,sysdate是否不會改變?

+0

你打算用可變l_sysdate替換此(爲你一開始選擇SYSDATE的程序/功能)呢? –

+0

如果可以,我會這樣做。但是我只有一個需要sysdate的sql查詢。我不想爲sysdate添加新的查詢。如果這樣做(聲明變量,賦值變量,執行sql查詢)可以在一個完美的查詢中完成。 –

+0

@nisha,在下面添加我的答案。 –

回答

11

some_proc(sysdate, sysdate);- SYSDATE不會總是相同的PL/SQL語句中使用時

select some_func(sysdate, sysdate) from dual;- SYSDATE總是會在一個SQL語句(用時相同,即使該SQL語句調用PL/SQL)

概念指南的Statement-Level Read Consistency部分:「Oracle始終強制執行語句級別的讀取一致性。這保證了單個查詢返回的所有數據都來自單個時間點 - 查詢開始了。「

這種頁面類型意味着對於純粹的PL/SQL函數也是如此。

我們可以通過創建接受許多時間戳參數的大函數(SQL上下文)和過程(PL/SQL上下文)來演示這一點,然後比較輸入參數的差異。我使用時間戳而不是日期,因爲它們在一致性方面應該以相同的方式工作,但時間戳(9)比日期更改的可能性是十億倍。 (大概 - 大概有很多取整和內部時鐘的細節,我不知道那使這個複雜得多。)

--First, use functions like these to create long strings of variables. 
--PL/SQL can go to 64K parameters, but luckily differences appear much sooner. 

--Parameters for function and procedure 
select listagg('a'||level||' timestamp', ',') within group(order by level) 
from dual connect by level <= 100 order by level; 

--Columns for least/greatest 
select listagg('a'||level, ',') within group(order by level) 
from dual connect by level <= 100; 

--Systimestamps for select/exec 
select listagg('systimestamp',',') within group(order by 1) 
from dual connect by level <= 100; 




--FUNCTION - SQL context 
create or replace function function_test(
    a1 timestamp,a2 timestamp,a3 timestamp,a4 timestamp,a5 timestamp,a6 timestamp,a7 timestamp,a8 timestamp,a9 timestamp,a10 timestamp,a11 timestamp,a12 timestamp,a13 timestamp,a14 timestamp,a15 timestamp,a16 timestamp,a17 timestamp,a18 timestamp,a19 timestamp,a20 timestamp,a21 timestamp,a22 timestamp,a23 timestamp,a24 timestamp,a25 timestamp,a26 timestamp,a27 timestamp,a28 timestamp,a29 timestamp,a30 timestamp,a31 timestamp,a32 timestamp,a33 timestamp,a34 timestamp,a35 timestamp,a36 timestamp,a37 timestamp,a38 timestamp,a39 timestamp,a40 timestamp,a41 timestamp,a42 timestamp,a43 timestamp,a44 timestamp,a45 timestamp,a46 timestamp,a47 timestamp,a48 timestamp,a49 timestamp,a50 timestamp,a51 timestamp,a52 timestamp,a53 timestamp,a54 timestamp,a55 timestamp,a56 timestamp,a57 timestamp,a58 timestamp,a59 timestamp,a60 timestamp,a61 timestamp,a62 timestamp,a63 timestamp,a64 timestamp,a65 timestamp,a66 timestamp,a67 timestamp,a68 timestamp,a69 timestamp,a70 timestamp,a71 timestamp,a72 timestamp,a73 timestamp,a74 timestamp,a75 timestamp,a76 timestamp,a77 timestamp,a78 timestamp,a79 timestamp,a80 timestamp,a81 timestamp,a82 timestamp,a83 timestamp,a84 timestamp,a85 timestamp,a86 timestamp,a87 timestamp,a88 timestamp,a89 timestamp,a90 timestamp,a91 timestamp,a92 timestamp,a93 timestamp,a94 timestamp,a95 timestamp,a96 timestamp,a97 timestamp,a98 timestamp,a99 timestamp,a100 timestamp 
) return varchar2 is 
    v_min timestamp(9); 
    v_max timestamp(9); 
begin 
    v_min := least(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68,a69,a70,a71,a72,a73,a74,a75,a76,a77,a78,a79,a80,a81,a82,a83,a84,a85,a86,a87,a88,a89,a90,a91,a92,a93,a94,a95,a96,a97,a98,a99,a100 
     ); 
    v_max := greatest(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68,a69,a70,a71,a72,a73,a74,a75,a76,a77,a78,a79,a80,a81,a82,a83,a84,a85,a86,a87,a88,a89,a90,a91,a92,a93,a94,a95,a96,a97,a98,a99,a100 
     ); 
    if v_min <> v_max then 
     return 'different'; 
    else 
     return 'the same'; 
    end if; 
end; 
/

--No matter how many times you run this, you'll always get "the same". 
select function_test(systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp) 
from dual; 




--PROCEDURE - PL/SQL context 
set serveroutput on; 

create or replace procedure procedure_test(
     a1 timestamp,a2 timestamp,a3 timestamp,a4 timestamp,a5 timestamp,a6 timestamp,a7 timestamp,a8 timestamp,a9 timestamp,a10 timestamp,a11 timestamp,a12 timestamp,a13 timestamp,a14 timestamp,a15 timestamp,a16 timestamp,a17 timestamp,a18 timestamp,a19 timestamp,a20 timestamp,a21 timestamp,a22 timestamp,a23 timestamp,a24 timestamp,a25 timestamp,a26 timestamp,a27 timestamp,a28 timestamp,a29 timestamp,a30 timestamp,a31 timestamp,a32 timestamp,a33 timestamp,a34 timestamp,a35 timestamp,a36 timestamp,a37 timestamp,a38 timestamp,a39 timestamp,a40 timestamp,a41 timestamp,a42 timestamp,a43 timestamp,a44 timestamp,a45 timestamp,a46 timestamp,a47 timestamp,a48 timestamp,a49 timestamp,a50 timestamp,a51 timestamp,a52 timestamp,a53 timestamp,a54 timestamp,a55 timestamp,a56 timestamp,a57 timestamp,a58 timestamp,a59 timestamp,a60 timestamp,a61 timestamp,a62 timestamp,a63 timestamp,a64 timestamp,a65 timestamp,a66 timestamp,a67 timestamp,a68 timestamp,a69 timestamp,a70 timestamp,a71 timestamp,a72 timestamp,a73 timestamp,a74 timestamp,a75 timestamp,a76 timestamp,a77 timestamp,a78 timestamp,a79 timestamp,a80 timestamp,a81 timestamp,a82 timestamp,a83 timestamp,a84 timestamp,a85 timestamp,a86 timestamp,a87 timestamp,a88 timestamp,a89 timestamp,a90 timestamp,a91 timestamp,a92 timestamp,a93 timestamp,a94 timestamp,a95 timestamp,a96 timestamp,a97 timestamp,a98 timestamp,a99 timestamp,a100 timestamp 
) is 
    v_min timestamp(9); 
    v_max timestamp(9); 
begin 
    v_min := least(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68,a69,a70,a71,a72,a73,a74,a75,a76,a77,a78,a79,a80,a81,a82,a83,a84,a85,a86,a87,a88,a89,a90,a91,a92,a93,a94,a95,a96,a97,a98,a99,a100 
     ); 
    v_max := greatest(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31,a32,a33,a34,a35,a36,a37,a38,a39,a40,a41,a42,a43,a44,a45,a46,a47,a48,a49,a50,a51,a52,a53,a54,a55,a56,a57,a58,a59,a60,a61,a62,a63,a64,a65,a66,a67,a68,a69,a70,a71,a72,a73,a74,a75,a76,a77,a78,a79,a80,a81,a82,a83,a84,a85,a86,a87,a88,a89,a90,a91,a92,a93,a94,a95,a96,a97,a98,a99,a100 
     ); 
    if v_min <> v_max then 
     dbms_output.put_line('different'); 
    else 
     dbms_output.put_line('the same'); 
    end if; 
end; 
/

--I see "different" about 50% of the time. 
--The ratio of differences decreases as you decrease the number of parameters. 

exec procedure_test(systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp); 
0

當語句被解析並執行時,Oracle將使用SYSDATE的所有實例的當前系統日期和時間。執行速度不是一個因素,因爲值替換髮生在執行之前。

+0

Tawman - sysdate的值是在運行時計算的,而不是解析時間。 Oracle僅在分析時檢查它是否是sysdate的有效使用(或者sysdate是有效的關鍵字/命令)。 –

+0

語義,但隱含的問題是在評估SYSDATE期間併發執行,而不是這種情況。 Oracle會同時替換SYSDATE的所有值,而不是一個長的子選擇之前和另一個之後等。 – tawman