请教关于java对SQL Server 2005插入速度的问题,这程序还要修改吗?
要测试出SQL Server 2005插入速度、更新速度,测出平均每秒的插入和更新速度,我这边测试出的数据是插入才1600多次每秒,更新只有200多次每秒。请问一下要怎么才有提高速度?我这程序还要再优化吗?
程序代码:
package sqltest;
import java.sql.*;
public class SqlSpeed {
private final String DBDRIVE = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private final String URL = "jdbc:sqlserver://192.168.168.104:1433;DatabaseName=ATest";
private static Connection con = null;
private static PreparedStatement stmt = null;
private static ResultSet rs = null;
public void connect(){
try{
Class.forName(DBDRIVE);
con = DriverManager.getConnection(URL,"sa","ba123");
System.out.println("Connect success...");
}catch(Exception e){
e.printStackTrace();
System.out.println("Connect failed...");
}
}
public void operate(String sql){
//int exec = 0;
try{
stmt = con.prepareStatement(sql);
//exec = stmt.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
}
public void resultSql(String sql){
try{
stmt = con.prepareStatement(sql);
//rs = stmt.executeQuery(sql);
ResultSetMetaData md = rs.getMetaData();
System.out.println(md.getColumnName(1)+'\t'+md.getColumnName(2)+'\t'+" "+md.getColumnName(3));
while(rs.next()){
System.out.println(rs.getString(1)+'\t'+" "+rs.getString(2)+rs.getString(3));
}
}catch(Exception e){
e.getMessage();
}
}
public static void close(){
if (rs != null)
try {
rs.close();
} catch(Exception e) {
e.printStackTrace();
}
if (stmt != null)
try {
stmt.close();
} catch(Exception e) {
e.printStackTrace();
}
if (con != null)
try {
con.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws SQLException{
String SQL = "SELECT * FROM tableb";
String deleteSQL = "DELETE FROM tableb where name = 'eee'";
String insertSQL = "INSERT INTO tableb(num,name,age,score) VALUES(?,'eee',20,82)";
SqlSpeed ss = new SqlSpeed();
long begin1 = System.currentTimeMillis();
ss.connect(); //连接
long time1 = System.currentTimeMillis()-begin1;
int loop = 10000;
long begin2 =System.currentTimeMillis();
for(int i=0; i<loop; i++){
ss.operate(insertSQL);
stmt.setInt(1, i);
stmt.executeUpdate();
//stmt.addBatch(); //批量处理
//String updateSQL = "UPDATE tableb SET age =+"+i+" where num =1";
//插入
// ss.operate(updateSQL); //更新
//if(i/8000 == 0){
// int[] st = stmt.executeBatch();
//}
}
long time2 = System.currentTimeMillis() - begin2;
System.out.println("平均每秒插入:"+(loop*1000)/time2);
long begin3 = System.currentTimeMillis();
ss.operate(deleteSQL); //删除
stmt.executeUpdate();
long time3 = System.currentTimeMillis() - begin3;
/*
long begin4 = System.currentTimeMillis();
for(int i=1; ; i++){
String updateSQL = "UPDATE message SET age =+"+i+" where num =1";
ss.operate(updateSQL); //更新
if(System.currentTimeMillis() - begin4 == 1000){
System.out.println("更新速率="+i+"次/秒");
break;
}
}
// */
long begin5 = System.currentTimeMillis();
//ss.resultSql(SQL); //处理结果
long time5 = System.currentTimeMillis()-begin5;
System.out.println("连接time1="+time1 +"ms. 插入time2="+time2+"ms. 删除time3="+
time3+"ms. 处理结果time5="+time5+"ms.");
close(); //关闭所有连接
}
}