138万条记录的List()与iterate()速度对比

程序代码


        Session session=HibernateSessionFactory.getSession();
        List li=null;
        Date d=new Date();
        System.out.print(d.toString());
        li=session.createQuery("from TgovMaterial").list();
        Date d2=new Date();
        System.out.print(d2.toString());



...超出内存...

程序代码

        Session session=HibernateSessionFactory.getSession();
        Iterator it=null;
        Date d=new Date();
        System.out.print(d.toString());
        it=session.createQuery("from TgovMaterial"). iterate();
        Date d2=new Date();
        System.out.print(d2.toString());



Wed Jun 04 09:31:43 GMT 2008Hibernate: select tgovmateri0_.Id as col_0_0_ from [Material-08].dbo.tGovMaterial tgovmateri0_
Wed Jun 04 09:31:43 GMT 2008
不到一秒钟,所以记录较多时,我们推荐使用iterate()
输 出测试:

程序代码

        Session session=HibernateSessionFactory.getSession();
        Iterator it=null;
        Date d=new Date();
        System.out.print(d.toString());
        it=session.createQuery("from TgovMaterial").iterate();
        while(it.hasNext())
        {
            TgovMaterial t=(TgovMaterial)it.next();
            System.out.println(t.getId());
        }

        Date d2=new Date();
        System.out.print(d2.toString());



Wed Jun 04 09:38:17 GMT 2008
.......
Wed Jun 04 09:39:20 GMT 2008

竟然只有一分来钟

程序代码

it=session.createQuery("from TgovMaterial").setFirstResult(1).setMaxResults(50).iterate();



Wed Jun 04 09:41:43 GMT 2008Hibernate: select top 51 tgovmateri0_.Id as col_0_0_ from [Material-08].dbo.tGovMaterial tgovmateri0_
....
Wed Jun 04 09:41:43 GMT 2008

不到1秒

程序代码

it=session.createQuery("from TgovMaterial").setFirstResult(1000000).setMaxResults(60).iterate();



Wed Jun 04 09:53:15 GMT 2008Hibernate: select top 1000060 tgovmateri0_.Id as col_0_0_ from [Material-08].dbo.tGovMaterial tgovmateri0_
....
Wed Jun 04 09:53:18 GMT 2008

程序代码

it=session.createQuery("from TgovMaterial").setFirstResult(1300000).setMaxResults(60).iterate();



Wed Jun 04 09:55:09 GMT 2008Hibernate: select top 1300060 tgovmateri0_.Id as col_0_0_ from [Material-08].dbo.tGovMaterial tgovmateri0_
...
Wed Jun 04 09:55:13 GMT 2008

程序代码

it=session.createQuery("from TgovMaterial").setFirstResult(10000).setMaxResults(60).iterate();


Wed Jun 04 09:55:53 GMT 2008Hibernate: select top 10060 tgovmateri0_.Id as col_0_0_ from [Material-08].dbo.tGovMaterial tgovmateri0_

你可能感兴趣的:(iterate)