操作blob字段是否会产生大量redo,答案是不会,下面来做一个实验,测试数据库版本是11.2.0.1.0:
--创建一张表做测试之用
create table test_blob
(
id number,
tupian blob
);
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.sql.BLOB;
public class BlobExample {
static final String driver_class = "oracle.jdbc.driver.OracleDriver";
static final String connectionURL = "jdbc:oracle:thin:@10.10.15.25:1521:orcl";
static final String userID = "test";
static final String userPassword = "test";
private void insertTestBlob() {
Connection conn=null;
Statement stm=null;
ResultSet rs=null;
BLOB blob = null;
FileInputStream fin=null;
OutputStream out=null;
try{
conn = DriverManager.getConnection(connectionURL, userID, userPassword);
stm = conn.createStatement();
conn.setAutoCommit(false);
String sql = "insert into test_blob values(1,EMPTY_BLOB())";
stm.executeUpdate(sql);
rs = stm.executeQuery("SELECT tupian FROM test_blob WHERE id=1 FOR UPDATE ");
fin = new FileInputStream("d://20130317.jpg");
byte[] blobBuf = new byte[(int)fin.available()];
fin.read(blobBuf);
fin.close();
if(rs.next()) {
blob = (oracle.sql.BLOB)rs.getBlob(1);
out = blob.getBinaryOutputStream();
out.write(blobBuf);
out.close();
conn.commit();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
rs.close();
stm.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]){
BlobExample blobClobExample = new BlobExample();
blobClobExample.insertTestBlob();
}
}
--做很多次
insert into test_blob select * from test_blob;
insert into test_blob select * from test_blob;
insert into test_blob select * from test_blob;
.......
commit;
--准备dump block
select rowid,
dbms_rowid.rowid_object(rowid) object_id,
dbms_rowid.rowid_relative_fno(rowid) file_id,
dbms_rowid.rowid_block_number(rowid) block_id,
dbms_rowid.rowid_row_number(rowid) num
from test_blob;
--update之前blob的状态
alter session set tracefile_identifier = 'Look_For_Me';
alter system switch logfile;
alter system switch logfile;
alter system dump datafile 5 block 3274932;
--update之后blob的状态,同时测试一下,此时update产生了多少redo
select name, value
from v$mystat, v$statname
where v$mystat.statistic# = v$statname.statistic#
and v$statname.name = 'redo size'
update test_blob set tupian = null;
commit;
select name, value
from v$mystat, v$statname
where v$mystat.statistic# = v$statname.statistic#
and v$statname.name = 'redo size'
alter system switch logfile;
alter system switch logfile;
alter session set tracefile_identifier = 'Look_For_Me1';
alter system dump datafile 5 block 3274932;
测试结果:我传的图片是5.1M,一共产生了350条数据,都把blob置为空以后,共产生了7.6M的redo,很显然是blob的内容是没有产生redo的。
分析原理,得借助分析dump block的内容,可以看到设置blob字段为null后产生的redo只是类似col 1: [84]这些信息。
blob设置为空以前:
Block header dump: 0x0171f8b4
Object id on Block? Y
seg/obj: 0x17f1d csc: 0x9a8.7256c728 itc: 2 flg: E typ: 1 - DATA
brn: 0 bdba: 0x171f8b0 ver: 0x01 opc: 0
inc: 0 exflg: 0
Itl Xid Uba Flag Lck Scn/Fsc
0x01 0x0003.01b.000033cf 0x00c001f5.132c.39 --U- 1 fsc 0x0000.7256c775
0x02 0x0000.000.00000000 0x00000000.0000.00 ---- 0 fsc 0x0000.00000000
bdba: 0x0171f8b4
data_