資料庫效能和一些小總結
關於資料庫效率
資料庫索引:
只有當資料查詢的結果集比較小(3%),null比較多,資料波動範圍不是很大的時候,索引的效果才明顯。
如果要解決效率低的情況,從根本上來說還是需要
分表:
例如,
江蘇 南京
江蘇 蘇州
江蘇 鹽城
不如分成兩個表:
A表就一條資料:
江蘇 1
B表:
南京 1
蘇州 1
鹽城 1
然後用的時候用 a.id =b.id聯合查詢
關於MERGE INTO的用法:
merge into一個最大的用處是批量操作insert/update語句。
例如:
MERGE INTO temp_each_ec_appcode p
USING (select eccode, appcode
from t_ec where APPCODE IS NULL)) ecappt
ON (p.eccode = ecappt.eccode)
WHEN MATCHED THEN
UPDATE SET p.appcode = ecappt.appcode;
p的整張表將被update
關於左右連線和where的區別:
A
a b c 1
d e f 1
q q q 2
B
s r 1
w w 3
A left join B on A.id=B.id
結果:
a b c s r
d e f d c
q q q null null
左邊的全保留,右邊的有就填,沒有就是null
where 和 內連線類似:
where A.id=B.id
結果:
a b c s r
d e f d c
oracle處理大欄位
資料庫中提供了兩種欄位型別 Blob 和 Clob 用於儲存大型字串或二進位制資料。
Blob 採用單位元組儲存,適合儲存二進位制資料,如圖片檔案。
Clob 採用多位元組儲存,適合儲存大型文字資料。
正確的做法是:
1.建立一個空 Blob/Clob 欄位。
2.對讀出Blob/Clob控制代碼.
3.將字串寫入Clob.
4.再將Blob/Clob欄位更新到資料庫.
例如下面的程式碼:
PreparedStatement ps = conn.prepareStatement( " insert into PICTURE(image,resume) values(?,?) " );
// 通過oralce.sql.BLOB/CLOB.empty_lob()構造空Blob/Clob物件
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob());
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob());
ps.excuteUpdate();
ps.close();
// 再次對讀出Blob/Clob控制代碼
ps = conn.prepareStatement( " select image,resume from PICTURE where id=? for update " );
ps.setInt( 1 , 100 );
ResultSet rs = ps.executeQuery();
rs.next();
oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 );
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 );
// 將二進位制資料寫入Blob
FileInputStream inStream = new FileInputStream( " c://image.jpg " );
OutputStream outStream = imgBlob.getBinaryOutputStream();
byte [] buf = new byte [ 10240 ];
int len;
while (len = inStream.read(buf) > 0 ) {
outStream.write(buf, 0 ,len);
}
inStream.close();
outStream.cloese();
// 將字串寫入Clob
resClob.putString( 1 , " this is a clob " );
// 再將Blob/Clob欄位更新到資料庫
ps = conn.prepareStatement( " update PICTURE set image=? and resume=? where id=? " );
ps.setBlob( 1 ,imgBlob);
ps.setClob( 2 ,resClob);
ps.setInt( 3 , 100 );
ps.executeUpdate();
ps.close();
mageinto和update的區別