![](/img/trans.png)
[英]Using jdbc I am using oracle plsql function to retrieve result set, it is not giving me all records in java
[英]Convert PLSQL function to process a jdbc statement that utilizes an Oracle ARRAY
我有以下PL / SQL塊,該塊創建了一個插入新貓的過程:
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。
創建一個數據庫對象類型:
CREATE TYPE CAT AS OBJECT(
AGE NUMBER, WEIGHT NUMBER, NAME VARCHAR2
);
然后創建一個相同的集合類型:
CREATE TYPE CATS IS TABLE OF CAT;
在您的PL / SQL過程中,接收cat列表並使用FORALL- >這對性能很重要。
從Java使用StructDescriptor
和ArrayDescriptor
構建CAT對象和CATS集合。
我可以使用以下步驟解決問題:
創建Oracle對象以引用我的Cat屬性:
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.