Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6970413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:42:24+00:00 2026-05-27T16:42:24+00:00

#Issue# We are trying to optimize our dataserver application. It stores stocks and quotes

  • 0

#Issue#
We are trying to optimize our dataserver application.
It stores stocks and quotes over a mysql database.
And we are not satisfied with the fetching performances.

#Context#
– database
– table stock : around 500 lines
– table quote : 3 000 000 to 10 000 000 lines
– one-to-many association : one stock owns n quotes
– fetching around 1000 quotes per request
– there is an index on (stockId,date) in the quote table
– no cache, because in production, queries are always different
– Hibernate 3
– mysql 5.5
– Java 6
– JDBC mysql Connector 5.1.13
– c3p0 pooling

#Tests and results#
##Protocol##

  • Execution times on mysql server are obtained with running the generated sql queries in mysql command line bin.
  • The server is in a test context : no other DB readings, no DB writings
  • We fetch 857 quotes for the AAPL stock

##Case 1 : Hibernate with association##
This fills up our stock object with 857 quotes object (everything correctly mapped in hibernate.xml)

session.enableFilter("after").setParameter("after", 1322910573000L);
Stock stock = (Stock) session.createCriteria(Stock.class).
add(Restrictions.eq("stockId", stockId)).
setFetchMode("quotes", FetchMode.JOIN).uniqueResult();

SQL generated :

SELECT this_.stockId AS stockId1_1_,
       this_.symbol AS symbol1_1_,
       this_.name AS name1_1_,
       quotes2_.stockId AS stockId1_3_,
       quotes2_.quoteId AS quoteId3_,
       quotes2_.quoteId AS quoteId0_0_,
       quotes2_.value AS value0_0_,
       quotes2_.stockId AS stockId0_0_,
       quotes2_.volume AS volume0_0_,
       quotes2_.quality AS quality0_0_,
       quotes2_.date AS date0_0_,
       quotes2_.createdDate AS createdD7_0_0_,
       quotes2_.fetcher AS fetcher0_0_
FROM stock this_
LEFT OUTER JOIN quote quotes2_ ON this_.stockId=quotes2_.stockId
AND quotes2_.date > 1322910573000
WHERE this_.stockId='AAPL'
ORDER BY quotes2_.date ASC

Results :

  • Execution time on mysql server : ~10 ms
  • Execution time in Java : ~400ms

##Case 2 : Hibernate without association without HQL##
Thinking to increase performance, we’ve used that code that fetch only the quotes objects and we manually add them to a stock (so we don’t fetch repeated info about the stock for every line). We used createSQLQuery to minimize effects of aliases and HQL mess.

String filter = " AND q.date>1322910573000";
filter += " ORDER BY q.date DESC";
Stock stock = new Stock(stockId);
stock.addQuotes((ArrayList<Quote>) session.createSQLQuery("select * from quote q where stockId='" + stockId + "' " + filter).addEntity(Quote.class).list());

SQL generated :

SELECT *
FROM quote q
WHERE stockId='AAPL'
  AND q.date>1322910573000
ORDER BY q.date ASC

Results :

  • Execution time on mysql server : ~10 ms
  • Execution time in Java : ~370ms

##Case 3 : JDBC without Hibernate##

String filter = " AND q.date>1322910573000";
filter += " ORDER BY q.date DESC";
Stock stock = new Stock(stockId);
Connection conn = SimpleJDBC.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from quote q where stockId='" + stockId + "' " + filter);
while(rs.next())
{
    stock.addQuote(new Quote(rs.getInt("volume"), rs.getLong("date"), rs.getFloat("value"), rs.getByte("fetcher")));
}
stmt.close();
conn.close();

Results :

  • Execution time on mysql server : ~10 ms
  • Execution time in Java : ~100ms

#Our understandings#

  • The JDBC driver is common to all the cases.
  • There is a fundamental time cost in JDBC driving.
  • With similar sql queries, Hibernate spends more time than pure JDBC code in converting result sets in objects.
  • Hibernate createCriteria, createSQLQuery or createQuery are similar in time cost.
  • In production, where we have lots of writing concurrently, pure JDBC solution seemed to be slower than the hibernate one (maybe because our JDBC solutions was not pooled)
  • Mysql wise, the server seems to behave very well, and the time cost is very acceptable.

#Our questions#

  • Is there a way to optimize the performance of JDBC driver ?
  • And will Hibernate benefit this optimization ?
  • Is there a way to optimize Hibernate performance when converting result sets ?
  • Are we facing something not tunable because of Java fundamental object and memory management ?
  • Are we missing a point, are we stupid and all of this is vain ?
  • Are we french ? Yes.

Your help is very welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:42:25+00:00Added an answer on May 27, 2026 at 4:42 pm

    Can you do a smoke test with the simples query possible like:

    SELECT current_timestamp()
    

    or

    SELECT 1 + 1
    

    This will tell you what is the actual JDBC driver overhead. Also it is not clear whether both tests are performed from the same machine.

    Is there a way to optimize the performance of JDBC driver ?

    Run the same query several thousand times in Java. JVM needs some time to warm-up (class-loading, JIT). Also I assume SimpleJDBC.getConnection() uses C3P0 connection pooling – the cost of establishing a connection is pretty high so first few execution could be slow.

    Also prefer named queries to ad-hoc querying or criteria query.

    And will Hibernate benefit this optimization ?

    Hibernate is a very complex framework. As you can see it consumes 75% of the overall execution time compared to raw JDBC. If you need raw ORM (no lazy-loading, dirty checking, advanced caching), consider mybatis. Or maybe even JdbcTemplate with RowMapper abstraction.

    Is there a way to optimize Hibernate performance when converting result sets ?

    Not really. Check out the Chapter 19. Improving performance in Hibernate documentation. There is a lot of reflection happening out there + class generation. Once again, Hibernate might not be a best solution when you want to squeeze every millisecond from your database.

    However it is a good choice when you want to increase the overall user experience due to extensive caching support. Check out the performance doc again. It mostly talks about caching. There is a first level cache, second level cache, query cache… This is the place where Hibernate might actually outperform simple JDBC – it can cache a lot in a ways you could not even imagine. On the other hand – poor cache configuration would lead to even slower setup.

    Check out: Caching with Hibernate + Spring – some Questions!

    Are we facing something not tunable because of Java fundamental object and memory management ?

    JVM (especially in server configuration) is quite fast. Object creation on the heap is as fast as on the stack in e.g. C, garbage collection has been greatly optimized. I don’t think the Java version running plain JDBC would be much slower compared to more native connection. That’s why I suggested few improvements in your benchmark.

    Are we missing a point, are we stupid and all of this is vain ?

    I believe that JDBC is a good choice if performance is your biggest issue. Java has been used successfully in a lot of database-heavy applications.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to optimize SEO readability on our websites and one issue I've come
Hey guys, trying to optimize this query to solve a duplicate user issue: SELECT
I am currently trying to optimize some bobj reports where our backend is Teradata.
I have been trying to optimize an ASP.NET C# application for a few days
I'm having an issue trying to load a series of sql files through our
Trying to make a MySQL-based application support MS SQL, I ran into the following
I have an issue trying to maximize the performance of our listview that has
Hi I seem to be having an issue trying to align an H1 tag
I'm having an issue when trying to do something which should be as easy
I am experiencing a very frustrating issue when trying to insert a new record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.