我最近一直在将基于网页hibernate的SQL连接升级到DB,我首先想到的是:“我应该为'Prepared Statement'更改简单的'Statement'来否定SQL注入,理论上,加快从DB获取数据的过程“。所以我做了,我改变了一些“陈述”,让我们说30%的陈述已经成功地改为'准备陈述'。这就是问题所在,现在它的运行速度非常慢,我说4分钟做基本选择到4个表中,请记住,使用'语句'选择就像200毫秒或更少。
我知道,当您使用具有不同参数的相同SQL查询并且一直反复执行时,'Prepared Statement'处于其最高性能点。但是什么时候它只用了几次呢?
我的问题是:
“如果查询最多执行3或4次,是否值得从'Statement'更改为'Prepared Statement'以及为什么?”
“我如何解决'准备好的声明'运行缓慢的问题,还是应该回到'声明'?”
我不确定如何解决这个问题,所以你能提供的任何帮助都可以派上用场。如果需要,我也可以提供几行代码来解决问题。
DB:SQL Server 2008.
跑步:本地(目前)。
本地PC:Intel(R)Core(TM)i5-4570S CPU @ 2.90GHz 16,0 GM RAM Windows 8.1 Pro。
软件:具有默认IntegratedWebLogicServer的JDeveloper 12c。
提前致谢!
编辑:
在这段代码中有一些混合的'Statement'和'Prepared Statement',因为我试图测试一些东西。这是代码(我可以上传的最大金额):
public boolean checkDate(Connection conn, String code1, String code2, String code3, String date) throws SQLException, ClassNotFoundException {
Statement stmt = null;
ResultSet rset = null;
boolean sampleboolean=true;
try {
stmt = conn.createStatement();
stmt.execute("some sql");
String select = "some sql"
PreparedStatement ps = conn.prepareStatement(select);
ps.setString(1, code1);
ps.setString(2, code2);
ps.setString(3, code3);
rset = ps.executeQuery();
int countint=0;
String codeactive="";
while (rset.next()) {
countint++;
if(!codeactive.equals(""))codeactive+=",";
codeactive+=rset.getString(1);
}
select = "Some sql";
ps = conn.prepareStatement(select);
ps.setString(1, code1); ps.setString(2, code2);
ps.setString(3, date); ps.setString(4, date);
int count=0;
while (rset.next()) {
count++;
}
if(count==countint)sampleboolean=true;
else sampleboolean=false;
stmt.close();
return sampleboolean;
} catch (Exception e) {
return sampleboolean;
} finally {
if (rset != null)
try {
rset.close();
} catch (Exception e) {
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
}
}