-->
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.  [ 3 posts ] 
Author Message
 Post subject: Maximum records in a table :: Batch delete query
PostPosted: Fri Sep 23, 2005 7:28 am 
Newbie

Joined: Fri Sep 23, 2005 7:11 am
Posts: 1
Hi,
I've got an Events table that stores information about events. The following info about events are stored in that table:
source, severity, dateRecieved and description

After a few days, the size of the table goes over 10,000 records! So i need to purge the database at regular intervals and keep the 1000 most recent records.

In order to do that, I have created a backgound process that runs at regular intervals, and using HQL, I get ALL the records from the table and check the size of the returned collection. If its more than 1000, i delete the older records.

But this is very inefficient and i have also got OutOfMemory Errors. Is there a better HQL query that runs a batch delete and keeps only the 1000 recent records?

Please can somebody help me out with tha query?

Thanks,
Joe.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 8:40 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
You can do a batch delete using an sql query like this

Code:
Connection jdbcCon = session.connection();
PreparedStatement st = jdbcCon.prepareStatement();
//...
try {
    st.setParameter(1, ...)
    st.executeUpdate();
}  finally {
   if (null != st) st.close();
session.close();
}


If you want to do something else in the session, don't forget do call session.clear() because if you don't do so your session will be desync.
And if your event class is in the second level cache you'll have to call sessionFactory.evict()

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 8:51 am 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
My idea would be:
1) to make a select on some sort of timestamp column your event table quite certainly contains - sorting the results.
2) if you get more than 1000 results, find the timestamp of the last entry to keep
3) If you have Hibernate 3, use a bulk delete (see http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#batch-direct), otherwise use directly JDBC to remove all events older than the timestamp found at step 2.

This is certainly a few orders of magnitude more efficient tan what you're doing.

By the way, I would also tend to do the following: wait until the table contains more than say 2000 events, then delete down to 1000 - a matter of taste, not knowing anything else about your application.

Erik


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