我正在尝试通过休眠查询来衡量数据获取时间的性能。为了这 :-
a.) 我打开了两个不同的会话,并且在两个会话中,我都使用了相同的 Hibernate 查询。
b.) 在 Session1 中,所用时间为 162 毫秒,在 Session2 中获取数据时,所用时间为 24 毫秒。
为什么查询执行时间会有这种差异?根据逻辑理解,每次(在不同的会话中),都会有一个新的查询被命中......所以,查询执行时间也应该是相似的。???
注意:- 此测试程序中没有启用二级缓存。
会话会话 = 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();
以下是结果:-
Hibernate:选择 vendordeta0_.id 作为 id0_0_,vendordeta0_.banned_zip_pattern 作为banned2_0_0_,vendordeta0_.code 作为code0_0_,vendordeta0_.company_name 作为company4_0_0_,vendordeta0_.created 作为created0_0_,vendordeta0_.cst_number 作为cst6_0_0_,vendordeta0_.default_page 作为default7_0000 启用, 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=?
在会话 1 中从数据库获取的时间:162
Hibernate:选择 vendordeta0_.id 作为 id0_0_,vendordeta0_.banned_zip_pattern 作为banned2_0_0_,vendordeta0_.code 作为code0_0_,vendordeta0_.company_name 作为company4_0_0_,vendordeta0_.created 作为created0_0_,vendordeta0_.cst_number 作为cst6_0_0_,vendordeta0_.default_page 作为default7_0000 启用, 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=?
在会话 2 中从数据库获取的时间:24
任何帮助将不胜感激!