2007-01-22
PreparedStatement与Statement性能详细对比
关键字: jdbc
我对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高。
评论
gongmingwind
2008-06-24
用事实说话!
meikefu
2008-03-19
没有发挥预编译的威力,,oracle中应该有50%的性能提升不可能只提高那么一点PreparedStatement pst = con.prepareStatement(psql);
for (int i = 0; i < count; i++) {
int id=(int) (Math.random() * 100);
pst.setBigDecimal(1, new BigDecimal(id));
pst.executeQuery();
}
pst.close();
for (int i = 0; i < count; i++) {
int id=(int) (Math.random() * 100);
pst.setBigDecimal(1, new BigDecimal(id));
pst.executeQuery();
}
pst.close();
发表评论
- 浏览: 120530 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
对于单例模式的一点想法
以前有人讨论过。 一种比较好的实现方式是Initialization on De ...
-- by buaawhl -
对于单例模式的一点想法
yxbbing 写道# private static Singleton INS ...
-- by Lucas Lee -
对于单例模式的一点想法
# private static Singleton INSTANCE; # ...
-- by yxbbing -
对于单例模式的一点想法
biubiu 写道除了你已经提出的两种办法,没有其他可以run everywhe ...
-- by Lucas Lee -
对于单例模式的一点想法
slangmgh 写道 JVM 不保证代码INSTANCE=new Single ...
-- by Lucas Lee






评论排行榜