为什么我测试出来的h2性能比mysql要差

HeAntai 发布于 2013/01/01 18:34
阅读 5K+
收藏 0
H2

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

从h2官网的数据看,h2的性能要比mysql的高,但是我在本机上测试结果却是mysql的性能是h2的10倍,是不是测试方法或者配置有问题,请问有什么方法可以优化h2的性能?

测试类的代码

public class H2Test {

static JdbcTemplate template;

public static void main(String[] args) {
ApplicationContext ac = new FileSystemXmlApplicationContext("beans_h2.xml");
template=(JdbcTemplate) ac.getBean("jdbcTemplate");

template.update("drop table if exists person");
template.update("create table person (id int,name varchar(10))");
 
int count=100;
   long start=System.currentTimeMillis();
   for (int i = 0; i < count; i++) {
    System.out.println(i);
template.update("insert into  person (id,name) values("+i+",'"+i+"')");
}
long end=System.currentTimeMillis();
System.out.println("平均插入用时:"+(end-start)/count+"ms");

start=end;
for (int i = 0; i < count; i++) {
Map map=template.queryForMap("select * from person where id="+i);
System.out.println(map);
}
end=System.currentTimeMillis();
System.out.println("平均查询用时:"+(end-start)/count+"ms");
}
}

配置文件

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >

<!-- 
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>

-->
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:testtest"/>

</bean>


<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

h2的平均插入及查询用时在100ms左右,mysql在10ms左右

h2版本h2-1.3.169

mysql版本5.5.20

操作系统:win7

cpu:2.4Ghz

内存:4g

加载中
1
鳄鱼先生
鳄鱼先生

更可笑的是用100条数据来测数据库性能!


0
平安北京
数据量太小了,再大些,多测几次,就显出了
0
鳄鱼先生
鳄鱼先生

又见到一个没有主键的表。

id上面建索引再看看吧。


0
孤火369
孤火369

我测试也一样,插入1000条数据要5秒左右。spring boot框架,jpa测试。说快的人能拿出测式样例吗。还是就没测过。

0
晓雷哥

spring boot jpa 

10000条数据, H2  575ms

 

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部