发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 CQRS 扩展性 并发编程 事件溯源 分布式 SOA
1 2 3 4 ... 7 下一页 Go 7

hibernate的问题

    
2003-09-10 10:42
赞助商链接

各位老大,我测了一下hibernate和直接用jdbc取mysql的数据

目的是:从表的10000条记录处取100条,看时间

1.用jdbc取数据
Class.forName("org.gjt.mm.mysql.Driver");
String url="jdbc:mysql://10.0.18.93/test";
String user = "root";
String password = "hello";
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
long begintime=System.currentTimeMillis();
ResultSet rs = stmt.executeQuery("select * from post limit 10000,100");
long endtime=System.currentTimeMillis();
System.out.println("query cost time="+(endtime-begintime));

结果:query cost time=235

2.用hibernate
sql = "select post from my.test.Post as post ";
long begintime = System.currentTimeMillis();
System.out.println("Begin time is " + begintime);
query = session.createQuery(sql);
query.setFirstResult(10000);
query.setMaxResults(100);
List catList = query.list();
long middletime = System.currentTimeMillis();
System.out.println("Query cost time is " +
(middletime - begintime));

结果:Query cost time is 8391


使用hibernate的全部代码


public class HibernateMain {
private static SessionFactory sessionFactory;
private static Session session;
private static Transaction transaction;

public static void main(String[] args) {
HibernateMain hm = new HibernateMain();

try {
hm.initHibernate();
session = sessionFactory.openSession();
//取出贴子条数
String sql = "select count(*) from com.alibaba.bizclub.dbo.Post as post ";
Query query = session.createQuery(sql);
Integer count = (Integer) query.iterate().next();
//取出贴子内容
sql = "select post from my.test.Post as post ";
long begintime = System.currentTimeMillis();
System.out.println("Begin time is " + begintime);
query = session.createQuery(sql);
query.setFirstResult(10000);
query.setMaxResults(100);
List catList = query.list();
long middletime = System.currentTimeMillis();
System.out.println("Query cost time is " +
(middletime - begintime));

for (Iterator it = catList.iterator(); it.hasNext();) {
Post post = (Post) it.next();
}

long endtime = System.currentTimeMillis();
System.out.println("End cost time is " + (endtime - begintime));
} catch (HibernateException ee) {
ee.printStackTrace();
}

}

private void initHibernate() throws HibernateException {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
}

2003-09-10 10:47

把这一句放到后面去或注释掉会更准确 System.out.println("Begin time is " + begintime);

因为 System IO 也要花时间。

2003-09-10 10:50

1 测试方法太粗糙

2 ResultSet rs = stmt.executeQuery("select * from post limit 10000,100");
这并没有实际取出数据

2003-09-10 11:03

stmt.executeQuery("select * from post limit 10000,100");

能取到数据的

我看过了这里另一个关于性能的贴子,有两点
1.mysql的jdbc驱动有问题,所以极快?

2.hibernate是不是默认不打开jcs的,所以性能一般,如何打开jcs?

2003-09-10 11:09

stmt.executeQuery()只是返回一个ResultSet,
你需要遍历这个ResultSet并取出每个字段。

jcs看看hibernate的文档

7Go 1 2 3 4 ... 7 下一页

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系反馈 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com