Search code examples
javagoogle-app-enginejdo

Limit query results when counting entities


What I want is to count the number of Employees, but I'm only interested to know if there are 50 or more Employees. If I do a Count-operation (see below) and set a Range between 0->51, will the query just count up to 51 or will it still count all the Employees? I want to know this so that the query doesn't count all of the 1000 employees each time the query is run (would be bad for performance).

Query query = pm.newQuery("SELECT count(p) FROM Employees p");
query.setRange(0, 51);
long count = (long) query.execute();
if(count >= skipThreshold)
{
   acceptedEmployees.add(mode);
}

Thank you!


Solution

  • If you do SELECT count(p) FROM Employees p then that will return one row with one value ... the COUNT, since you asked for the count. Range is pointless in that query. You use range when you have multiple rows (objects) returned by the query.