persistence@glassfish.java.net

Re: getSingleResult() . It's very slow...

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Fri, 11 Apr 2008 08:24:56 +0200

On Thu, Apr 10, 2008 at 11:20:15PM -0700, MrFishKill wrote:
> I have a table with a 9000 registers and column is indexed. It's very small
> table. My database is a MySQL 5. Connection (entity mananager) is open only
> one time. Program and database are running in my local machine (Intel Code
> 2Duo). Around 400 milliseconds just in a 'gesSingleResult()' instruction.
> Look at this piece of code:
> ......
> long time1 = System.currentTimeMillis();
> o = q.getSingleResult();
> long time2 = System.currentTimeMillis();
> long dif_time = time2 - time1;
> System.out.println("Dif time:"+dif_time);
> .......
>
> Output:
>
> Dif time: 356
> Dif time: 423
> Dif time: 389

What if you include a property in persistence.xml to print out the SQL statement
and run that statement in a command line MySQL client? Does the query take the
same amount of time? Or is it shorter? If it's shorter I'd say building up the
connection (or some other preparation the entity manager needs to do before
being able to execute the query) is taking up the time.

another way of making sure is to not just register the time but also print it
out, both before and after the execution of the query. As far as I know your app
and the actual query are executed in separate threads, so it may not be
completely reliable. But it may give you some more clues as to what's going on.


HTH, Wouter van Reeven

-- 
People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"