0

I am trying to improve performance of an api. I need to know whether the second line that's marked will hit database too ? as I want to minimise that.

     StringBuffer queryBuf = new StringBuffer("some query in SQL");
--------->StringBuffer queryBuf2 = new StringBuffer(" SELECT DISTINCT PP.ID FROM ( " + queryBuf + ") PP ");
        Query query1 = getSession().createSQLQuery(queryBuf2.toString());
        query1.setReadOnly(true);
        ScrollableResults results = query1.scroll();
        if (results.isLast() == false)
            results.last();
        int total = results.getRowNumber() + 1;
        results.close();
        logger.debug(">>>>>>TOTAL COUNT<<<<<< = {}", total);
newProgramer
  • 79
  • 2
  • 5
  • 16

5 Answers5

2

No. Only the line ScrollableResults results = query1.scroll(); executes sql.

Also, you may want to use SQL COUNT

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

Its plain and simple it wont hit , you are just creating StringBuffer objects.

saurav
  • 3,424
  • 1
  • 22
  • 33
2

you are just create a string buffer. its not hitting the db. If i right this code may give compile error.

maXfenda
  • 214
  • 3
  • 10
1

I need to know whether the second line that's marked will hit database too ?

You are constructing StringBuffer objects in first three lines , why should it hit the DB ! You can use StringBuilder if synchronization is not required !

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • You are right but i didn't understand then how "total" gets evaluated and showing correct no of rows, when the "results" is dependent on "query1" and "query1" on "queryBuf2". – newProgramer Jul 30 '13 at 10:16
1

No it will not hit the database. It will query from the buffer created by first query.

hima
  • 610
  • 3
  • 10
  • 24