1. 按需取資料,減少網路開銷。
a 表:有 100 多個字段,儲存在資料庫 a 上 id
property1
property2 …
property120
b 表:只有 10 個字段,儲存在資料庫 b 上 id
property1
property2 …
property10
要從 a 表中查詢一些資料在插入到 b 表中。
懶的做法:把所有資料全讀出來,當獲取 2000 條資料時,網路耗時約 15 秒。在專案過程中,有開發人員偷懶,就寫了如下的 sql ,導致呼叫遠端服務取資料時,設定服務超時時間為 3 秒,結果總是超時。
1. select * from a where …
2. 把從 a 表獲得的資料轉換成符合 b **式的資料
3. insert into b(id,property1,…,property10) valuses(1,』test』,…,』2009-11-9』)
好的做法:只取 b 表需要的資料,耗時從 15 秒減少至 2 秒。
selectid,property1,…,property10 from a where …
2.在 dao 層 ,spring+ibatis 批量插入資料 , 減少獲取資料庫連線池的次數,提高效能 .
sqlmapclienttemplate sqlmapclienttemplate = getsqldaobasesupport()
.getsqlmaptemplate(subdbroute.getcrmroute ());
sqlmapclienttemplate.execute(newsqlmapclientcallback()
executor.executebatch();
returnnull;
} });
3.使用 spring 的事務,保證資料的一致性 .
publicbooleanatomicinsert ()throwsregruleserviceexceptioncatch(daoexception e)
returnret;
} });
return(boolean) object; }
4. 插入資料時 spring template 的 update 和 insert 的區別。在專案過程中,開發人員在寫**時,忽視了這兩個方法的區別,導致需要自動生成主鍵的插入不成功,花費了很長時間才找出原因。下次引以為戒。
< insert id
="ibatistestdao.inserttest"
parameterclass
="testdo"
>
<
selectkey
resultclass
="long"
keyproperty
="id"
>
select seq_test_id.nextval as id from dual
selectkey
>
insert into test(id,name) values(#id#,#name#)
insert >
呼叫 spring template 的 update 方法,也可以成功插入一條資料,返回的是被更新的記錄數。這個時候 selectkey 的自動生成主鍵不起作用。
// 呼叫 spring template
int updaterows = sqlmapclienttemplate.update(statementname, parameterobject);
呼叫 spring template 的 insert 方法,成功插入一條資料,並且返回新增加記錄的主鍵。
// 呼叫 spring template
object back = sqlmapclienttemplate.insert(statementname, parameterobject);
spring ibatis 批量處理資料
本人在最近的專案中發現這樣的乙個問題。在spring ibatis的配置下,存在多種進行資料操作的方法。各種方法在效率上以及是否存在潛在bug上,也各不相同。以下述 為例 public void insert siterank param public void insertall listsite...
spring ibatis 批量提交資料
在系統中,提取資料迴圈計算後,每次需要有大概3000條左右的資料需要提交到資料庫。以前在迴圈中單條插入,開始只有200條左右的資料,看不出效能上的問題,現在資料量增長了很多,所以需要對提交功能做一下優化。spring整合了ibatis的批量提交的功能,我們只要呼叫api就可以了 首先在你的dao中需...
Spring ibatis批量儲存心得
程式功能 使用ibatis spring將oracle資料庫中的tfile表中的資料抽取到db2資料庫的tfile表,這兩個表的結構相同。測試環境 celeron m 1.4 512m mysql 5.0資料庫 public static void main string args test2fil...