Title: Truncate with Hibernate and H2 is slower than expected
Tags: java;sql;hibernate;h2
Question: Using Hibernate 4.3.11 and H2 1.3.172 database
I changed code to use truncate to empty a database table rather than delete because delete was performing too slowly as the number of records in the table increased. However TRUNCATE also seems rather slow (aprox 30 seconds) I assumed it would work rather quickly and would not matter how many records were in the table ?
try
session = HibernateUtil.beginTransaction();
session.createSQLQuery("truncate table SongDiff").executeUpdate();
session.getTransaction().commit();
return true;
catch(Exception ex)
MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
finally
HibernateUtil.closeSession(session);
Comment: Have you tried `drop`? is it also slow?

Comment: No its not slow, but how to you drop and recreate a hibernate table without recreating the whole schema ?

Comment: probably it might be the implementation of H2. but worth to measure the performance using JDBC (without Hibernate).



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:06/05/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><sql><hibernate><h2>', 'Title': 'Truncate with Hibernate and H2 is slower than expected', 'ViewCount': 116, 'id': 56463650, 'user_AboutMe': '<p>Runs <a href="http://albunack.net" rel="nofollow">Albunack</a> providing awesome artist discographies and the <a href="http://www/jthink/net/jaikoz">Jaikoz</a> and <a href="http://www.jthink.net/songkong" rel="nofollow">SongKong Music Tagger</a> applications.</p>\n\n<p>Main skills:Java and Databases.</p>\n\n<p>Also, photographer concentrating on the less obvious in South-West England, photographs can be seen at <a href="http://www.secretdorsetphoto.com" rel="nofollow">Secret Dorset Photo</a></p>\n', 'user_AccountId': 111681.0, 'user_DisplayName': 'Paul Taylor', 'user_Reputation': 12807.0, 'user_Views': 2186.0, 'user_id': 1480018.0}}