环境: 双核 2G内存
DB: mysql hibernate+spring
hibernate执行sql语句的两种方式:
第一种方式:hibernate createSQLQuery里面可以传入传统的sql语句进行查询,它的返回值为一个数组,
第二种方式:hibernate session.connection得到的是一个java.sql.Connection对象,以下的操作就和jdbc一样的操作 。
我用以上两种方式查询1条记录:
String sql = "select m.id,m.m_recordState,m.m_number,m.m_name,m.m_typeid,m.m_spec,m.m_model," +"m.m_color,m.m_weight,m.m_weightunit,m.m_volume,m.m_volumeunit,m.m_height,m.m_width,m.m_oneortwo," + "m.m_state,m.m_recordMaker,m.m_recordDate from t_Material m join t_PurchaseList l on " +
"m.id=l.p_materiaId join t_PurchaseOrder p on p.id=l.parentid " +
"where p.o_vendor='0011' and m.m_state=1 and m.m_recordState=1";
第一种用时 62毫秒 第二种用时47毫秒(我用同事的机器测试第一种方式竟然用了100多毫秒,两种方式用时相差一半多)
我换了sql语句
String sql = "select m.* from t_Material m join t_PurchaseList l on " +
"m.id=l.p_materiaId join t_PurchaseOrder p on p.id=l.parentid " +
"where p.o_vendor='0011' and m.m_state=1 and m.m_recordState=1";
第一种用时 78毫秒 第二种用时47毫秒
所以用hibernate执行sq1语句的两种方式差别:
1: sqlquery :hibernate对其返回值进行了封装相差 connection: hibernate没有对其进行任何的封装 两种方式用时相差很大,(根据机器配置不同,则相差不同)总之,相差近一半.
2: sql语句: 采用select * 和分别列出每个列 对connection来说没有影响,对sqlquery来说也会有相差