persistence@glassfish.java.net

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

From: MrFishKill <jgmaux_at_telefonica.net>
Date: Thu, 10 Apr 2008 23:20:15 -0700 (PDT)

Thanks Marina,


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
...
...

Thanks in advance.


Marina Vatkina wrote:
>
> There might be several reasons:
> - the query runs on a large table but the field is not mapped to a PK or
> an
> indexed column;
> - it's the 1st connection being created, or you are not using pooled
> connection,
> and each time a new physical connection is created;
> - a network problem between the client and the database;
> - your timing includes more than just a query...
>
> Regards,
> -marina
>
> MrFishKill wrote:
>> Hi,
>>
>> I have a problem with getSingleResult(). It seems very slow , around 400
>> milliseconds.
>>
>> This is a piece of my code: (Do you see anything wrong in it?):
>>
>> public <T> T findUniqueByX(Class<T> source_class, String field, Object
>> value)
>> {
>> Object o = null;
>> T ot = null;
>>
>> StringBuffer sbQuery = new StringBuffer();
>> sbQuery.append("SELECT x FROM ");
>> sbQuery.append(source_class.getSimpleName());
>> sbQuery.append(" x WHERE ");
>> sbQuery.append("x.");
>> sbQuery.append(field);
>> sbQuery.append("=?1");
>>
>> Query q = em.createQuery(sbQuery.toString());
>> q.setParameter(1, value);
>> try
>> {
>> o = q.getSingleResult();
>> ot = source_class.cast(o);
>> }
>> catch (javax.persistence.NoResultException e)
>> {
>> ot = null;
>> }
>>
>> return ot;
>> }
>>
>> Thanks in advance
>
>
>

-- 
View this message in context: http://www.nabble.com/getSingleResult%28%29-.-It%27s-very-slow...-tp16618446p16625621.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.