hibernate的问题

03-09-10 chinahero
各位老大,我测了一下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();

}

}

    

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

因为 System IO 也要花时间。

yehs220
2003-09-10 10:50
1 测试方法太粗糙

2 ResultSet rs = stmt.executeQuery("select * from post limit 10000,100");

这并没有实际取出数据

chinahero
2003-09-10 11:03
stmt.executeQuery("select * from post limit 10000,100");

能取到数据的

我看过了这里另一个关于性能的贴子,有两点

1.mysql的jdbc驱动有问题,所以极快?

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

yehs220
2003-09-10 11:09
stmt.executeQuery()只是返回一个ResultSet,

你需要遍历这个ResultSet并取出每个字段。

jcs看看hibernate的文档

猜你喜欢
7Go 1 2 3 4 ... 7 下一页