新聞中心
通過(guò)將多個(gè) UPDATE 或 INSERT 語(yǔ)句分組為一個(gè)批處理,將整個(gè)批處理發(fā)送到數(shù)據(jù)庫(kù)并一起處理,可以減少數(shù)據(jù)庫(kù)的往返處理次數(shù),從而提高應(yīng)用程序性能。

OceanBase Connector/J 使用服務(wù)器 PrepareStatement 和 Statements 作為與數(shù)據(jù)庫(kù)進(jìn)行批處理通信的標(biāo)準(zhǔn),并通過(guò) allowMultiQueries 和 rewriteBatchedStatements 兩個(gè)參數(shù)進(jìn)行管理。如果將 allowMultiQueries 或 rewriteBatchedStatements 選項(xiàng)設(shè)置為 true,則 OceanBase Connector/J 將僅使用文本協(xié)議。PrepareStatement(參數(shù)替換)在客戶(hù)端由驅(qū)動(dòng)程序處理。
rewriteBatchedStatements 對(duì)于插入查詢(xún),用于重寫(xiě) batchedStatement 以在單個(gè) executeQuery 中執(zhí)行。當(dāng)它處于活躍狀態(tài)時(shí),useServerPrepStmts 選項(xiàng)被設(shè)置為 false。allowMultiQueries 為 true 時(shí)允許多值查詢(xún)。
在 Oracle 和 MySQL 兩種模式下,OceanBase Connector/J 對(duì)于批處理的設(shè)置會(huì)有不同。
Oracle 模式下的批處理
PrepareStatement 處理方式
|
useServerPrepStmts |
rewriteBatchedStatements |
allowMultiQueries |
INSERT 操作 |
UPDATE 操作 |
|---|---|---|---|---|
|
true |
true |
true |
Prepare 的 SQL String 會(huì)變成 |
Prepare 的 SQL String 是正常格式,之后通過(guò)多次 |
true |
false |
Prepare 的 SQL String 會(huì)變成 |
Prepare 的 SQL String 是正常格式,之后通過(guò)多次 | |
false |
true |
Prepare 的 SQL String是正常格式,之后通過(guò)多次 |
Prepare 的 SQL String是正常格式,之后通過(guò)多次 | |
false |
false |
Prepare 的 SQL String 是正常格式,之后通過(guò)多次 |
Prepare 的 SQL String 是正常格式,之后通過(guò)多次 | |
|
false |
true |
true |
拼成一個(gè)完整的 |
拼成一個(gè)完整的 |
true |
false |
拼成一個(gè)完整的 |
執(zhí)行多條 | |
false |
true |
執(zhí)行多條 |
執(zhí)行多條 | |
false |
false |
執(zhí)行多條 |
執(zhí)行多條 |
Statement 處理方式
|
useServerPrepStmts |
rewriteBatchedStatements |
allowMultiQueries |
INSERT 操作 |
UPDATE 操作 |
|---|---|---|---|---|
|
true |
true |
true |
SQL String 會(huì)變成 |
SQL String 會(huì)變成 |
true |
false |
逐一執(zhí)行 SQL。 |
逐一執(zhí)行 SQL。 | |
false |
true |
SQL String 會(huì)變成 |
SQL String 會(huì)變成 | |
false |
false |
逐個(gè)執(zhí)行 |
逐個(gè)執(zhí)行 | |
|
false |
true |
true |
SQL String 會(huì)變成 |
SQL String 會(huì)變成 |
true |
false |
逐一執(zhí)行 SQL。 |
逐一執(zhí)行 SQL。 | |
false |
true |
SQL String 會(huì)變成 |
SQL String 會(huì)變成 | |
false |
false |
逐一執(zhí)行 |
逐一執(zhí)行 |
MySQL 模式下的批處理
PrepareStatement 處理方式
|
useServerPrepStmts |
rewriteBatchedStatements |
allowMultiQueries |
INSERT 操作 |
UPDATE 操作 |
|---|---|---|---|---|
|
true |
true |
true |
SQL String 會(huì)變成 |
逐一 Prepare 的 SQL String 是正常格式,之后通過(guò)多次 |
true |
false |
SQL String 會(huì)變成 |
逐一 Prepare 的 SQL String 是正常格式,之后通過(guò)多次 | |
false |
true |
Prepare 的 SQL String 是正常格式,之后通過(guò)多次 |
Prepare 的 SQL String 是正常格式,之后通過(guò)多次 | |
false |
false |
Prepare 的 SQL String 是正常格式,之后通過(guò)多次 |
Prepare 的 SQL String 是正常格式,之后通過(guò)多次 | |
|
false |
true |
true |
拼成一個(gè)完整的 |
拼成一個(gè)完整的 |
true |
false |
拼成一個(gè)完整的 |
執(zhí)行多條 | |
false |
true |
執(zhí)行多條 |
執(zhí)行多條 | |
false |
false |
執(zhí)行多條 |
執(zhí)行多條 |
Statement 處理方式
|
useServerPrepStmts |
rewriteBatchedStatements |
allowMultiQueries |
INSERT 操作 |
UPDATE 操作 |
|---|---|---|---|---|
|
true |
true |
true |
SQL String 會(huì)變成 |
SQL String 會(huì)變成 |
true |
false |
逐一執(zhí)行 SQL。 |
逐一執(zhí)行 SQL。 | |
false |
true |
SQL String 會(huì)變成 |
SQL String 會(huì)變成 | |
false |
false |
逐一執(zhí)行 |
逐一執(zhí)行 | |
|
false |
true |
true |
SQL String 會(huì)變成 |
SQL String 會(huì)變成 |
true |
false |
逐一執(zhí)行 SQL。 |
逐一執(zhí)行 SQL。 | |
false |
true |
SQL String 會(huì)變成 |
SQL String 會(huì)變成 | |
false |
false |
逐一執(zhí)行 |
逐一執(zhí)行 |
示例
public void test_execute_batch() {
Connection conn = null;
try {
for (int q = 0; q < 2; q++) {
for (int w = 0; w < 2; w++) {
for (int e = 0; e < 2; e++) {
/**
* useServerPrepStmts = true / false
* rewriteBatchedStatements = true / false
* allowMultiQueries = true / false
*/
boolean uSPS = q == 0 ? false : true;
String rBS = w == 0 ? "false" : "true";
String aMQ = e == 0 ? "false" : "true";
String other_condition = "&rewriteBatchedStatements=" + rBS
+ "&allowMultiQueries=" + aMQ;
conn = ConnectionUtils.getDefObOracleConnection(uSPS, true, other_condition);
Statement statement = conn.createStatement();
try {
statement.execute("DROP TABLE test_batch_t");
} catch (SQLException exp) {
//e.printStackTrace();
}
String sql = "create table test_batch_t(id int primary key, c1 varchar2(10))";
statement.execute(sql);
PreparedStatement ps = conn
.prepareStatement("insert into test_batch_t(id, c1) values (?, ?)");
for (int j = 0; j < 10; j++) {
ps.setInt(1, j);
ps.setString(2, j + "_test");
ps.addBatch();
}
ps.executeBatch();
ps = conn.prepareStatement("update test_batch_t set id = ? where c1 = ?");
for (int j = 0; j < 10; j++) {
ps.setInt(1, j);
ps.setString(2, j + "_test");
ps.addBatch();
}
ps.executeBatch();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} 標(biāo)題名稱(chēng):創(chuàng)新互聯(lián)OceanBase教程:OceanBase批處理
文章網(wǎng)址:http://www.dlmjj.cn/article/dheddci.html


咨詢(xún)
建站咨詢(xún)
