我有一个用于持久化某些Java对象的MySQL表,并使用Hibernate来映射数据/列。对象/表包含大约50列/字段。在表面和最小负载下,这工作正常,但我在程序启动时遇到问题,我从表中加载所有可用对象。表中目前有大约8500行。加载这些行需要花费很长时间,超过5分钟!显然这是不对的,我必须在我的Hibernate配置(或其他地方)中做了一些可怕的错误。使用java.sql。*包对所有行进行常规查询需要大约300毫秒来检索8000+行,这是可以接受的,但理想情况下我希望保留Hibernate的映射功能。
我已经在下面附加了Hibernate的主要配置。我很乐意提供进一步的配置/实施细节,但我不想在这个阶段用可能不必要的信息来讨论这个问题。
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://127.0.0.1:3306/abc</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="org.hibernate.SQL">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.c3p0.idle_test_period">60</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">250</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.acquireRetryAttempts">1</property>
<property name="hibernate.c3p0.acquireRetryDelay">250</property>
<property name= "hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="hibernate.current_session_context_class">thread</property>
我最初使用默认连接池,但尝试切换到C3P0,希望它有所帮助,但无论池如何,行的加载时间基本相同。
显然我不是Hibernate专家,所以我真的很感激我对错误的看法。感谢。
UPDATE1 从查询中添加日志输出。
20:16:40,633 DEBUG QueryTranslatorImpl:283 - --- HQL AST ---
\-[QUERY] Node: 'query'
+-[SELECT_FROM] Node: 'SELECT_FROM'
| \-[FROM] Node: 'from'
| \-[RANGE] Node: 'RANGE'
| \-[DOT] Node: '.'
| +-[DOT] Node: '.'
| | +-[DOT] Node: '.'
| | | +-[DOT] Node: '.'
| | | | +-[IDENT] Node: 'com'
| | | | \-[IDENT] Node: 'xyz'
| | | \-[IDENT] Node: 'objects'
| | \-[IDENT] Node: 'abc'
| \-[IDENT] Node: 'ObjectID'
\-[WHERE] Node: 'where'
\-[EQ] Node: '='
+-[IDENT] Node: 'event_id'
\-[NUM_INT] Node: '1331570489282'
20:16:40,633 DEBUG QueryTranslatorImpl:252 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (order_log)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'objectid0_.event_id as abc1_0_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=order_log,tableAlias=objectid0_,origin=null,columns={,className=com.xyz.objects.abc.objectid}}}
| \-[SQL_TOKEN] SqlFragment: 'objectid0_.order_id as abc2_0_, objectid0_.order_id as order3_0_, objectid0_.px_cond as px4_0_, objectid0_.px_curr as px5_0_, objectid0_.px_value as px6_0_, objectid0_.vol_cond as vol7_0_, objectid0_.vol as vol0_, objectid0_.side as side0_, objectid0_.trd_vol as trd10_0_, objectid0_.open_vol as open11_0_, objectid0_.accno as accno0_, objectid0_.symbol as symbol0_, objectid0_.market as market0_, objectid0_.validity_type as validity15_0_, objectid0_.validity_date as validity16_0_, objectid0_.mod_date as mod17_0_, objectid0_.result_code as result18_0_, objectid0_.order_state as order19_0_, objectid0_.action_state as action20_0_, objectid0_.action_type as action21_0_, objectid0_.action_status as action22_0_, objectid0_.status_text as status23_0_, objectid0_.strategy_id as strategy24_0_, objectid0_.passive as passive0_, objectid0_.timestamp as timestamp0_, objectid0_.confirmedorder as confirm27_0_, objectid0_.updatesource as updates28_0_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[objectid0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'order_log objectid0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=order_log,tableAlias=objectid0_,origin=null,columns={,className=com.abc.objects.xyz.objectid}}
\-[WHERE] SqlNode: 'where'
\-[EQ] BinaryLogicOperatorNode: '='
+-[IDENT] IdentNode: 'event_id' {originalText=event_id}
\-[NUM_INT] LiteralNode: '1331570489282'
日志中有更多输出,但我认为上面的两项是最相关的。如果我错过了任何重要的内容,请告诉我。
UPDATE2 添加实际的sql-output。
Hibernate: /* from objectid where abc_event_id=1331570505614 */ select objectid0
_.abc_event_id as abc1_0_, objectid0_.abc_order_id as abc2_0_, objectid0_.
order_id as order3_0_, objectid0_.px_cond as px4_0_, objectid0_.px_curr as px5_0_,
objectid0_.px_value as px6_0_, objectid0_.vol_cond as vol7_0_, objectid0_.vol as v
ol0_, objectid0_.side as side0_, objectid0_.trd_vol as trd10_0_, objectid0_.open_vo
l as open11_0_, objectid0_.accno as accno0_, objectid0_.symbol as symbol0_, ucorde
r0_.market as market0_, objectid0_.validity_type as validity15_0_, objectid0_.vali
dity_date as validity16_0_, objectid0_.mod_date as mod17_0_, objectid0_.result_cod
e as result18_0_, objectid0_.order_state as order19_0_, objectid0_.action_state as
action20_0_, objectid0_.action_type as action21_0_, objectid0_.action_status as a
ction22_0_, objectid0_.status_text as status23_0_, objectid0_.strategy_id as strat
egy24_0_, objectid0_.passive as passive0_, objectid0_.timestamp as timestamp0_, uc
order0_.confirmedorder as confirm27_0_, objectid0_.updatesource as updates28_0_ f
rom order_log objectid0_ where abc_event_id=1331570505614
更新3 对象映射+事务代码
<hibernate-mapping>
<class name="com.abc.objects.xyz.objectid" table="order_log">
<id name="EventId" type="long" column="event_id" >
<generator class="assigned"/>
</id>
<property name="ordId">
<column name="order_id"/>
</property>
<property name="pxCond">
<column name="px_cond"/>
</property>
...
</hibernate-mapping>
映射是摘录,但它与上面的行相同。查询表的事务如下所示:
Session session = HibernateUtil.getSessionFactory().openSession();
org.hibernate.Transaction transaction = null;
try {
long t0 = System.currentTimeMillis();
transaction = session.beginTransaction();
List<UCOrder> result = session.createQuery("from UCOrder").list();
transaction.commit();
System.out.println (result.size() + " rows were retrieved");
System.out.println("Time elapsed: " + (System.currentTimeMillis()-t0));
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
我还想直截了当地思考?!?但是,与下面说明的vanilla java.sql.*
方法相比,检索数据所需的时间是千倍:
Connection conn = DriverManager.getConnection (url, userName, password);
Statement s = conn.createStatement ();
s.executeQuery ("SELECT * FROM order_log");
ResultSet rs = s.getResultSet ();
答案 0 :(得分:1)
我建议您在Hibernate中启用详细日志记录。您可以使用它来记录所有SQL查询,并查看它们中是否存在异常。
一种方法是将hibernate.show_sql
设置为true
和/或(?)配置Hibernate使用的公共日志记录,将所有org.hibernate
类别输出到您喜欢的输出文件。< / p>
答案 1 :(得分:0)
您不需要在“选择”中进行交易。 如果您使用log4j,请禁用hibernate的调试。 申请关联表“模式懒惰”。这非常重要。