-->
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.  [ 6 posts ] 
Author Message
 Post subject: Fastest way to retrieve the last (most recent) record?
PostPosted: Wed Nov 30, 2005 1:17 pm 
Newbie

Joined: Mon Aug 15, 2005 3:57 pm
Posts: 13
Most Exalted Ones,

My app has a number of large tables that are constantly updated by a backend C++ process. I need to get only the most recently added record.

How can I do this through hibernate without getting the entire recordset? Hibernate doesn't do the inserts, so I can't use the last_insert or whatever it's called.

thanks in advance!


Top
 Profile  
 
 Post subject: Re: Fastest way to retrieve the last (most recent) record?
PostPosted: Wed Nov 30, 2005 1:21 pm 
Newbie

Joined: Mon Aug 15, 2005 3:57 pm
Posts: 13
spovilaitis wrote:
I need to get only the most recently added record.


I should add that I need the most recently added record for EACH table. Otherwise I could just use the mySQL last_insert_id function.

thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 1:49 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Add timestamp type field, and select record from mytable where mytime = (select max(mytime) from mytable).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 1:51 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
or "select record from mytable order by mytime desc limit 1"


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Wed Nov 30, 2005 1:56 pm 
Newbie

Joined: Mon Aug 15, 2005 3:57 pm
Posts: 13
Thanks from a fellow Lithuanian for both of your helpful suggestions! Any prediction which query might be faster?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 2:04 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
I am not sure about mysql, but "select record from mytable order by mytime desc limit 1" works faster on postgresql (some versions do not use index for "max", but sort by index without problems)


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