我对PreparedStatement和Statement的性能测试了一下:
测试代码如下:
test表很简单,id int,name varchar(50).
对几种数据库和相应驱动程序进行测试,结果如下(ms):
oracle: 1235 1109
MSSQL2000(JTDS):391 453
MSSQL2000(MS): 453 640
Mysql5: 391 891
PostgreSQL8.1: 1078 1047
结论:
1.单并发情况下,oracle--PostgreSQL--MSSQL--MySQL的性能依次增高;
2.MySQL不支持Prepared Statement特性,已在其驱动程序的文档中证实,所以在MySQL里使用 PreparedStatement的性能尤其低,可以比Statement慢一倍以上。
而MSSQL2000下,PreparedStatement比Statement慢;
Oracle,PostgreSQL对它的支持最好,使用PreparedStatement性能比Statement高。
测试代码如下:
java 代码
- Connection con = getOraConnection();
- String sql = "select id,name from test where id=";
- String tempSql;
- int count = 1000;
- long time = System.currentTimeMillis();
- for (int i = 0; i < count; i++) {
- Statement st = con.createStatement();
- tempSql=sql+(int) (Math.random() * 100);
- st.executeQuery(tempSql);
- st.close();
- }
- System.out.println("st cost:" + (System.currentTimeMillis() - time));
- String psql = "select id,name from test where id=?";
- time = System.currentTimeMillis();
- for (int i = 0; i < count; i++) {
- int id=(int) (Math.random() * 100);
- PreparedStatement pst = con.prepareStatement(psql);
- pst.setBigDecimal(1, new BigDecimal(id));
- pst.executeQuery();
- pst.close();
- }
- System.out.println("pst cost:" + (System.currentTimeMillis() - time));
- con.close();
test表很简单,id int,name varchar(50).
对几种数据库和相应驱动程序进行测试,结果如下(ms):
oracle: 1235 1109
MSSQL2000(JTDS):391 453
MSSQL2000(MS): 453 640
Mysql5: 391 891
PostgreSQL8.1: 1078 1047
结论:
1.单并发情况下,oracle--PostgreSQL--MSSQL--MySQL的性能依次增高;
2.MySQL不支持Prepared Statement特性,已在其驱动程序的文档中证实,所以在MySQL里使用 PreparedStatement的性能尤其低,可以比Statement慢一倍以上。
而MSSQL2000下,PreparedStatement比Statement慢;
Oracle,PostgreSQL对它的支持最好,使用PreparedStatement性能比Statement高。