-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Urgent: How to optimize this SLOW query?
PostPosted: Sun Apr 25, 2004 12:54 pm 
Newbie

Joined: Sun Apr 25, 2004 12:42 pm
Posts: 2
Hi there, newbie here.

I'm doing a project where we have, among others, 2 tables in a database (DB2 8.1 using legacy CLI JDBC driver): "Paper" and "Quote". As you might've guessed, the paper-table contains information about stock papers, whereas the quote-table contains streaming quotes for the papers.

The two tables are related to each other via paper.id and quote.fk_paper.

I need to be able to get the newest row from the quote table, for all or some papers, in one go (=one HQL). The result should then be a List of Quote objects - one quote for each paper.

What I do now is this:
Code:
   /** Get the Quotes for all papers in the database */
   public List getQuotes(Long paperTypeId) throws DAOFinderException {
      List list = new ArrayList();
      
      log.debug("Getting list of Quotes for Papers of type " + paperTypeId);
      try {
         List ps = new ArrayList();
         if (paperTypeId==null)
            ps = (List) getQuotes();
         else
            ps = (List) ses.createQuery("select distinct p.id from Paper p where p.type.id=:id")
               .setLong("id", paperTypeId.longValue())
               .list();
            
//         TODO: There MUST be a better way to do this, mann!
         Iterator itr = ps.iterator();
         while (itr.hasNext()){
            list.add(getQuote((Long)itr.next()));
         }
      }
      catch (HibernateException e) {
         // TODO: What about the Session!?!?!
         throw new DAOFinderException(""+e);
      }

      return list;
   }

   /** Get the Quotes that belongs to the supplied Paper ID*/
   public Quote getQuote(Long paperId) throws DAOFinderException {
      Quote q = null;
      try{
         q = (Quote) ses.createQuery("from Quote q where q.papir.id=:id order by q.id desc")
            .setLong("id", paperId.longValue())
            .setMaxResults(1)
            .uniqueResult();
      }
      catch(HibernateException e){
         throw new DAOFinderException("Couldn't get single Quote for Paper w/id=" + paperId + ": " + e);
      }
      return q;
   }

The mapping files are as follows:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>

  <class name="dk.xyz.web.model.Quote" table="quote" schema="wasadmin" dynamic-insert="true">

    <!-- INFO -->
    <id column="id" name="id">
       <generator class="identity" />
    </id>

   <!--discriminator column="fk_paper" type="long" /-->

    <property column="tid" name="tid" not-null="true" />

   <many-to-one name="papir" column="fk_paper" not-null="true" />
   
    <property column="bud" name="bud" not-null="true" />
    <property column="udbud" name="udbud" not-null="true" />
    <property column="seneste" name="seneste" not-null="true" />
    <property column="senestegain" name="senesteGain" not-null="true" />
  </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>

  <class name="dk.xyz.web.model.Paper" table="paper" schema="wasadmin">

    <!-- INFO -->
    <id column="id" name="id" unsaved-value="null">
       <generator class="identity" />
    </id>
    <property column="name" name="name" not-null="true" />
    <property column="kode" name="code" />
    <property column="isin" name="isin" />

  </class>
</hibernate-mapping>


Please help anyone, I'm on a very tight schedule here!?

Thanks in advance!
/Henrik


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Look at the generated SQL and check what is actually executed. Then tell us what exactly your problem is.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 2:55 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You can try to create some fake table and update it with trigger on "QUOTE"
(it can start to produce dealocks), but the fasted way is to put the last quote to map in memory after insert and load this map once at startup (Key is a paper). Your paper table is limited, is not it ?


Top
 Profile  
 
 Post subject: Generated SQL is not the issue
PostPosted: Sun Apr 25, 2004 3:16 pm 
Newbie

Joined: Sun Apr 25, 2004 12:42 pm
Posts: 2
The generated SQL is not the issue.
The SQL is fine, and fast enough.

The problem is, that i use 101 queries to get 100 quotes:
1 query for getting the id's for the papers, and
100 subsequent queries for getting each of the 100 quotes.

About storing them in a cache the first time they are queried; how to do that?

Thanks,
/Henrik


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 4:31 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if you call .iterate(), i think one of the goal is to take advantage of the second level cache, if you don't use second level cache, use .list() method or be sure to fully understand what .iterate() do.
Now if you want to use a cache, just read about ehcache, it's easy to use


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.