前言
项目使用批量粘贴excel的单号进行查询,因业务量大,查询数量远超1000,为解决Oracle的使用限制,提供以下解决方法,经验证解决了使用限制、并且查询高效、稳定、简单。
1. 首先要创建一张session级别的临时表
create global temporary table CONDITIONS
(
id NUMBER,
key VARCHAR2(256),
value VARCHAR2(256),
attr1 VARCHAR2(256),
attr2 VARCHAR2(256),
attr3 VARCHAR2(256),
attr4 VARCHAR2(256),
attr5 VARCHAR2(256)
)
on commit preserve rows;
-- Create/Recreate indexes
create index KEY_INDEX1 on CONDITIONS(KEY);
create index KEY_INDEX2 on CONDITIONS(ID);
2. 写公用类
2.1. 定义一个javabean
package com.newkdd.common;
/**
*
* @author MIKE
*
*/
public class Conditions {
private long id;// 毫秒级别的时间搓
private String key;// 对应数据库的列名
private String value;// 对应key的value值
private String attr1;// 备注1
private String attr2;// 备注2
private String attr3;// 备注3
private String attr4;// 备注4
private String attr5;// 备注5
//此处省略get、setter方法
}
2.2. 定义一个公用方法操作类
package com.newkdd.common;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author MIKE
*
*/
public class ConditionUtil {
/**
* 添加数据关联条件
* @param con 数据库连接
* @param conditions 条件集合
* @return 影响的条数
*/
public static int addCondition(Connection con, List conditions) {
int result=0,count = 0;
PreparedStatement pst = null;
String sql = "insert into conditions(id,key,value,attr1,attr2,attr3,attr4,attr5) values(?,?,?,?,?,?,?,?)";
try {
pst = con.prepareStatement(sql);
for (Conditions condition : conditions) {
count++;
result++;
pst.setLong(1, condition.getId());
pst.setString(2, condition.getKey());
pst.setString(3, condition.getValue());
pst.setString(4, condition.getAttr1());
pst.setString(5, condition.getAttr2());
pst.setString(6, condition.getAttr3());
pst.setString(7, condition.getAttr4());
pst.setString(8, condition.getAttr5());
pst.addBatch();
if (count > 600) {
count=0;
pst.executeBatch();
}
}
if(count>0){
pst.executeBatch();
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 根据系统时间搓查询当前session的约束条件
* @param con 数据库连接
* @param id 时间搓
* @return 约束条件
*/
public static List getConditions(Connection con, long id) {
List conditions = new ArrayList();
String sql = "select * from conditions t where t.id=?";
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sql);
pst.setString(1, String.valueOf(id));
ResultSet rs = pst.executeQuery();
Conditions condition = null;
while (rs.next()) {
condition = new Conditions();
condition.setId(rs.getLong("id"));
condition.setKey(rs.getString("key"));
condition.setValue(rs.getString("value"));
condition.setAttr1(rs.getString("Attr1"));
condition.setAttr2(rs.getString("Attr2"));
condition.setAttr3(rs.getString("Attr3"));
condition.setAttr4(rs.getString("Attr4"));
condition.setAttr5(rs.getString("Attr5"));
conditions.add(condition);
}
} catch (SQLException e) {
e.printStackTrace();
}
return conditions;
}
}
3. 使用表关联替换‘in’操作
select * from table1 t1,conditions t2 where
t1.column1=t2.value
and t2.key='column1'
and t2.id=''
现在在一个Session连接里边就可以实现联合查询了,即解决了“in”的限制,又提升了查询的速度。