I am trying to measure the performance of Data Fetching Time by a hibernate query. For this :-
a.) I have opened two different sessions and in both sessions, I have used the same Hibernate query.
b.) While inside Session1 , time taken is 162 milliseconds and while fetching the data in Session2, time taken is 24 milliseconds.
Why is this difference in query execution time ? As per logical understanding, Every time (in different session), a new query is hit... So, Query execution time should also be similar. ???
Note:- There is no 2nd Level Cache enabled in this test program.
Session session = HibernateUtil.getSessionFactory().openSession(); VendorDaoImpl vendorDao = new VendorDaoImpl();
long start = System.currentTimeMillis();
VendorDetail vendorDetailRecord1 = vendorDao.getVendorByCode(session, "92f880");
long end = System.currentTimeMillis();
System.out.println("Time to fetch from DB while in Session 1: " + (end-start));
session.close();
Session session2 = HibernateUtil.getSessionFactory().openSession();
start = System.currentTimeMillis();
VendorDetail vendorDetailRecord2 = vendorDao.getVendorByCode(session2, "92f880");
end = System.currentTimeMillis();
System.out.println("Time to fetch from DB while in session 2: " + (end-start));
session2.close();
Below is the Results :-
Hibernate: select vendordeta0_.id as id0_0_, vendordeta0_.banned_zip_pattern as banned2_0_0_, vendordeta0_.code as code0_0_, vendordeta0_.company_name as company4_0_0_, vendordeta0_.created as created0_0_, vendordeta0_.cst_number as cst6_0_0_, vendordeta0_.default_page as default7_0_0_, vendordeta0_.enabled as enabled0_0_, vendordeta0_.max_invoice_number as max9_0_0_, vendordeta0_.name as name0_0_, vendordeta0_.next_invoice_number_reset_date as next11_0_0_, vendordeta0_.print_mock_invoice as print12_0_0_, vendordeta0_.special_panel_access as special13_0_0_, vendordeta0_.starting_invoice_number as starting14_0_0_, vendordeta0_.tin_number as tin15_0_0_, vendordeta0_.updated as updated0_0_, vendordeta0_.version as version0_0_ from test.vendor_detail vendordeta0_ where vendordeta0_.code=?
Time to fetch from DB while in Session 1: 162
Hibernate: select vendordeta0_.id as id0_0_, vendordeta0_.banned_zip_pattern as banned2_0_0_, vendordeta0_.code as code0_0_, vendordeta0_.company_name as company4_0_0_, vendordeta0_.created as created0_0_, vendordeta0_.cst_number as cst6_0_0_, vendordeta0_.default_page as default7_0_0_, vendordeta0_.enabled as enabled0_0_, vendordeta0_.max_invoice_number as max9_0_0_, vendordeta0_.name as name0_0_, vendordeta0_.next_invoice_number_reset_date as next11_0_0_, vendordeta0_.print_mock_invoice as print12_0_0_, vendordeta0_.special_panel_access as special13_0_0_, vendordeta0_.starting_invoice_number as starting14_0_0_, vendordeta0_.tin_number as tin15_0_0_, vendordeta0_.updated as updated0_0_, vendordeta0_.version as version0_0_ from test.vendor_detail vendordeta0_ where vendordeta0_.code=?
Time to fetch from DB while in session 2: 24
Any help would greatly be appreciated !