我有一条选择的sql查询语句,该语句返回900000+行。但是我希望使用limit / offset子句以小块形式检索数据,以便可以轻松地将其插入到另一个表中。
public class Snomedinfo {
public void snomedinfoinsert()
{
Root oRoot = null;
ResultSet oRsSelect = null;
PreparedStatement oPrStmt = null;
PreparedStatement oPrStmt2 = null;
PreparedStatement oPrStmtSelect = null;
String strSql = null;
int count=0;
int batchSize=1000;
ResultSet oRs = null;
String refid = null;
String id = null;
String effectivetime = null;
String active = null;
String moduleid = null;
String conceptid = null;
String languagecode = null;
String typeid = null;
String term = null;
String caseSignificanceid = null;
try{
oRoot = Root.createDbConnection(null);
strSql = "SELECT d.* FROM snomed_conceptdata c, snomed_descriptiondata d WHERE c.active = 1 and conceptid = c.id AND d.active = 1 ";//this is the query that returns 900000 rows
oPrStmt2 = oRoot.con.prepareStatement(strSql);
oRs = oPrStmt2.executeQuery();
String sql = "INSERT INTO snomedinfo_date (refid,id,effectivetime,active,moduleid,conceptid,languagecode,typeid,term,caseSignificanceid) VALUES( ?, ?, ?,?,?,?,?,?,?,?)";
oPrStmt = oRoot.con.prepareStatement(sql);
while (oRs.next())
{
refid = Root.TrimString(oRs.getString("refid"));
id = Root.TrimString(oRs.getString("id"));
effectivetime = Root.TrimString(oRs.getString("effectivetime"));
active = Root.TrimString(oRs.getString("active"));
moduleid = Root.TrimString(oRs.getString("moduleid"));
conceptid = Root.TrimString(oRs.getString("conceptid"));
languagecode = Root.TrimString(oRs.getString("languagecode"));
typeid = Root.TrimString(oRs.getString("typeid"));
term = Root.TrimString(oRs.getString("term"));
caseSignificanceid = Root.TrimString(oRs.getString("caseSignificanceid"));
oPrStmt.setString(1, refid);
oPrStmt.setString(2, id);
oPrStmt.setString(3, effectivetime);
oPrStmt.setString(4, active);
oPrStmt.setString(5, moduleid);
oPrStmt.setString(6, conceptid);
oPrStmt.setString(7, languagecode);
oPrStmt.setString(8, typeid );
oPrStmt.setString(9, term);
oPrStmt.setString(10, caseSignificanceid);
oPrStmt.addBatch();
if (++count % batchSize == 0) {
oPrStmt.executeBatch();
oPrStmt.clearBatch();
}
}
oPrStmt.executeBatch();
System.out.println("done");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
oRs = Root.EcwCloseResultSet(oRs);
oPrStmt = Root.EcwClosePreparedStatement(oPrStmt);
oPrStmt = Root.EcwClosePreparedStatement(oPrStmt2);
oRoot = Root.closeDbConnection(null, oRoot);
}
}
如何在程序中实现用于设置限制/偏移量的逻辑? 我有一条选择sql查询语句,该语句返回900000+行。但是我希望使用limit / offset子句以小块形式检索数据,以便可以轻松地将其插入到另一个表中。
答案 0 :(得分:0)
选择d。*从snomed_conceptdata c,snomed_descriptiondata d中,其中c.active = 1和conceptid = c.id AND d.active = 1 限制偏移量,限制;
偏移量-您要从其开始读取数据的行。
限制-您要读取的行数。
例如- 从snomed_conceptdata c,snomed_descriptiondata d中选择d。*。c.active = 1,conceptid = c.id AND d.active = 1 限制10,20 ;
如果在网页中使用此功能,则需要分页。
否则请使用这样的简单循环-
for(int i = 0; i <=(90000/100); i + = 10){
strSql =“从snomed_conceptdata c,snomed_descriptiondata d中选择d。* c.active = 1,conceptid = c.id AND d.active = 1 限制” + i +“,10” ;
//运行其他代码
}
每次迭代都会使指针偏移10行。