2012-08-17 57 views
0

我有一個創建一個可插入一個新的貓以下PL/SQL塊中的JDBC聲明:轉換PLSQL函數來處理,利用一個Oracle ARRAY

CREATE OR REPLACE PROCEDURE INSERT_NEW_CAT(AGE NUMBER, WEIGHT NUMBER, NAME VARCHAR2) 
AS 
BEGIN  
    INSERT INTO CATS(cat_id,age,weight,name) 
    VALUES(cat_id_sequence.nextval,age,weight,name); 
END INSERT_NEW_CAT; 

而且下面的Java模塊批量插入這些貓:

CallableStatement statement = conn.prepareCall("{CALL insert_new_cat(?,?,?)}"); 
for(int i = 0; i < 50000;i++) 
{ 
    statement.setInt(1, i); 
    statement.setInt(2,i); 
    statement.setString(3,"Test"); 
    statement.addBatch(); 
    if(i % 16382== 0) //max value my batch can support on my hardware 
    { 
     statement.executeBatch(); 
    } 
} 

statement.executeBatch(); 

所以這個問題是我只能堅持700記錄秒,這是很慢。我相信問題是我每行調用一次上述PL/SQL函數(或者每批次輸入一次)。如何重新編寫上述函數來獲取這些語句的數組,並進行批量插入以將對函數的調用次數減少到N模16382 + 1?數據庫是Oracle 10g。

+0

只是一個問題。如果您每100條記錄執行一次批處理,而不是在16000條奇數記錄之後執行,那麼您認爲它可能更具性能嗎? – sethu 2012-08-17 15:16:38

+0

@sethu我相信對數據庫的呼叫越多,性能就會越差,因此不會。也作爲供參考性能不是一個字:) – Woot4Moo 2012-08-17 15:27:24

+0

表現最肯定是一個字。而且,我從記憶的角度來看更多。內存將被保留,直到您執行批處理。這可能會降低系統的速度。您可能需要嘗試並獲得正確的批量大小。 – sethu 2012-08-18 08:37:52

回答

0

我可以使用以下步驟來解決問題:

Oracle對象的創建,以引用我的貓屬性:

Create Type age is table of number 
Create Type weight is table of number 
Create Type name is table of varchar(50) 

然後在我的Java代碼,我做了以下內容:

ArrayDescriptor ageCollection = ArrayDescriptor.createDescriptor("AGE", conn); 
ArrayDescriptor weightCollection = ArrayDescriptor.createDescriptor("WEIGHT", conn); 
ArrayDescriptor nameCollection = ArrayDescriptor.createDescriptor("NAME", conn); 
int[] ageArray = new int[50000]; 
int[] weightArray = new int[50000]; 
String[] nameArray = new String[50000]; 
for(int i = 0; i <50000;i++) 
{ 
    ageArray[i]=i; 
    weightArray[i]=i; 
    nameArray[i] = "Test"; 
} 
ARRAY oraAge = new ARRAY(ageCollection,conn,ageArray); 
ARRAY oraWeight = new ARRAY(weightCollection,conn,weightArray); 
ARRAY oraName = new ARRAY(nameCollection,conn,nameArray);  

CallableStatement statement = conn.prepareCall("{CALL insert_new_cat(?,?,?)}"; 

statement.setObject(1,oraAge); 
statement.setObject(2,oraWeight); 
statement.setObject(3,oraName); 
statement.execute(); 
conn.commit(); 

SQL過程:

CREATE OR REPLACE PROCEDURE INSERT_NEW_CAT (age age, weight weight, name name) 
AS 
BEGIN 
    forall i in 1..age.last 
     insert into cats(id,age,weight,name) 
     vales(cat_sequence.nextval,age(i),weight(i),name(i); 
END INSERT_NEW_CAT; 

重要的是要注意行(年齡)是指我在oracle數據庫中創建的年齡數據類型。通過執行上述操作,我能夠將完全索引表上的插入提高到〜45000秒。在非索引表上,這個值變成〜70000每秒。

1

創建一個數據庫對象類型:

CREATE TYPE CAT AS OBJECT(
    AGE NUMBER, WEIGHT NUMBER, NAME VARCHAR2 
); 

然後創建相同的集合類型:

CREATE TYPE CATS IS TABLE OF CAT; 

在你的PL/SQL程序接收貓的列表,並使用FORALL - >此對於性能非常重要。

從java使用StructDescriptorArrayDescriptor構建您的CAT對象和CATS集合。