-->
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.  [ 10 posts ] 
Author Message
 Post subject: oracle limit and order by queries
PostPosted: Mon Feb 09, 2004 8:03 am 
Newbie

Joined: Thu Oct 23, 2003 6:39 pm
Posts: 16
Hi all
I'm working on a project involving an Oracle 7.3 database, and I'm using Hibernate. I know that using this database is not optimal by any means, but there's nothing i can do about it, and it seams to work very well.

The problem is when i try to use queries that are sorted, like "from Object o order by o.property"), and then put a limit on the results returned by the query. Hibernate generates a SQL query with a subselect, using the rownum function like this:

Code:
select * from (select t.property p from table t order by p asc) where rownum <= ?


Oracle then complains about a missing right parenthesis, though the generated SQL looks fine to me. I've tried to put the "order by" cause after the "where rownum" cause, like this:

Code:
select * from (select t.property p from table t) where rownum <= ? order by p asc


And it works fine. Is there any way I can use the

Code:
hibernate.query.substitutions


to modify the way Hibernate generates the SQL query? Session conf is MBean on Jboss 3.2, Hibernate version 2.1.1.

Help much appriciated!
milx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 8:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
http://forum.hibernate.org/viewtopic.php?t=927563&highlight=rownum


Top
 Profile  
 
 Post subject: customize dialect
PostPosted: Mon Feb 09, 2004 8:57 am 
Newbie

Joined: Thu Oct 23, 2003 6:39 pm
Posts: 16
I've read that one, and it ends by saying that this is not supported by Oracle 8.0, and therefore not on 7.3 either, but it also says that
Quote:
you could easily customize the dialect
.
My question is then how can I put the order by cause after the rownum cause, by customizing the dialect?
milx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:01 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I don't believe this gives you the same results. The second query you presentet is - give me the first 10 results and then order them, while the original one is order the results then give me the first 10. I think oracle would be seriously messed up if this works the same.

You could change the dialect to use a different limiting strategy by overriding getLimitSelectString() (read the source)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:36 am 
Newbie

Joined: Thu Oct 23, 2003 6:39 pm
Posts: 16
Well, running Oracle 7 in 2004 is messed up....
I see the problem with ordering the results aferwards, asking for top 10 ordered by something will return the top 10 rows in the database, and then sort these.....
Currently I let Hibernate order the results, then I only return a specific number of these to the client. This works fine with a small total number of rows in the table, but my guess is that 500.000 rows will be a performance killer.
How does Hibernate perform if you iterate over a large result? A client hardly ever asks for more than the 100 first results of a particular query. If the results returned by the Hibernate query are ordered, but not limited, I really only have to use the first 100 rows, not the other 499.900. Does Hibernate create 500.000 objects, or only those that are needed by the Iterator?
Thank you very much for the help!
milx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You could do it that way:

Code:
Iterator iter = session.createQuery("your query").iterate();
for (int i=0; i<10 && iter.hasNext(); i++) {
    result.add(iter.next());
}

This will result in one query for the ids, and 10 more querys for the first 10 objects. That way you can prevent the huge memory consumption at the cost of more queries.

If there is a way to do the querying you want in direct SQL, you should be able to modify the dialect to have Hibernate behave accordingly. If not, you will have to make some tradeoffs ...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 10:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
By "customize the dialect" we mean disable the dialect-specific-limit stuff *altogether*. Look at some of the other dialects to see what this would look like.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 8:04 am 
Newbie

Joined: Thu Oct 23, 2003 6:39 pm
Posts: 16
Thanks a lot for the feedback! I've been reading some in the hard to find Oracle 7 reference, and it seems like both limiting and ordering the results are impossible. If anyone knows a workaround, please let me know. I'm now going with gloeglms purposed Iterator solution, it works fine but slow.
milx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 8:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just try overriding supportsLimit() of the oracleDialect to return false, and override all the other limit stuff to throw an UnsupportedOperationException. Then try to use setMaxResult, etc. It could be Hibernate simulates the limit efect, I just don't know how effective it is :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 8:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
As I understand it Hibernate then does a normal Select, and gets you the appropriate part of the result set in the returned List. This avoids the Object generation overhead, but still gets you a large result set. You have to decide yourself where to trade off.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.