How does setMaxResults (N) work in sleep mode?

I am using MS SQL Server 2008 with Hibernate. the question i have is how hibernate implementssetMaxResults

Take the following simple scenario.

If I have a query that returns 100 rows, and if I go 1 to setMaxResults, it will affect the returned result from the SQL server itself (as if it were executing a statement select top 1) or Hibernate got all the results first (all 100 rows in this case) and select the top one?

The reason I'm asking for is because she will have a big performance problem when the number of rows starts to grow.

Thank.

+5
source share
2 answers

Hibernate , . SQLServerDialect (. org.hibernate.dialect.SQLServerDialect.supportsLimit() .getLimitString()), select top 1 -query.

, debug-logging showSql -option test.

+3

, . , Bean EmpBean, 5 . ,

public List<EmpBean> getData()
{
    Session session = null;
    try
    {
        session = HibernateUtil.getSession();
        Query qry = session.createQuery("FROM EmpBean");
        qry.setMaxResults(5);
        return qry.list();
    }
    catch(HibernateException e)
    {}
    finally
    {           
        HibernateUtil.closeSession(session);
    }
    return null;
}

getSession closeSession - ,

0

All Articles