gpt4 book ai didi

java - 如何使用hibernate优化插入查询时间?

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:15 24 4
gpt4 key购买 nike

我有大约 140 万条数据记录,但插入它们需要超过 3 小时。我似乎找不到它的问题。

我阅读并从身份更改为序列。只进步了一点点,但仍然需要相当长的时间才能完成插入。

我正在使用:

  • hibernate 5
  • Spring 4
  • MSSQL 2014
  • 野蝇10

applicationContext-hibernate.xml

<tx:advice id="txAdvice">
<!-- the transactional semantics... -->
<tx:attributes>
<tx:method name="*_TransNew" propagation="REQUIRES_NEW" />
<tx:method name="*_NoTrans" propagation="NEVER" />

<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />

<tx:method name="generate*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" />
<tx:method name="is*" propagation="REQUIRED" />

<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>


<!-- ensure that the above transactional advice runs for any execution of
an operation defined by the following -->
<aop:config>
<aop:pointcut id="demoServiceOperations"
expression="execution(* com.test.*.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="demoServiceOperations" />
</aop:config>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>

<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>


</props>
</property>
</bean>

Umts.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 22, 2016 11:36:21 AM by Hibernate Tools 5.2.0.Beta1 -->
<hibernate-mapping>
<class name="com.test.domain.Umts" table="TBLDM_UMTS" schema="dbo" catalog="DEMO" optimistic-lock="version" dynamic-update="true">
<id name="umtsId" type="java.lang.Integer">
<column name="UMTS_ID" />
<generator class="org.hibernate.id.enhanced.SequenceStyleGenerator">
<param name="optimizer">pooled-lo</param>
<param name="increment_size">1</param>
<param name="sequence_name">UMTS_SEQ</param>
</generator>
</id>
<property name="cid" type="java.lang.Integer">
<column name="CI" not-null="true" />
</property>
<property name="channelNo" type="java.lang.Integer">
<column name="UARFCN" />
</property>
<property name="signalStrength" type="java.lang.Double">
<column name="EC_IO" precision="53" scale="0" />
</property>
<property name="sc" type="java.lang.Integer">
<column name="SC" />
</property>
<property name="latitude" type="java.lang.Double">
<column name="LATITUDE" precision="53" scale="0" />
</property>
<property name="longitude" type="java.lang.Double">
<column name="LONGITUDE" precision="53" scale="0" />
</property>
<property name="mcc" type="java.lang.Integer">
<column name="MCC" not-null="true" />
</property>
<property name="mnc" type="java.lang.Integer">
<column name="MNC" not-null="true" />
</property>
<property name="recvDate" type="date">
<column name="RECV_DATE" length="10" />
</property>
<property name="recvTime" type="time">
<column name="RECV_TIME" length="16" />
</property>
</class>
</hibernate-mapping>

服务等级:

public void process(List<Umts> umtsList)
{
for (int i = 0; i < umtsList.size(); i = i + PropertiesUtil.MAX_COMMIT_COUNT)
{
int min = i;
int max = i + PropertiesUtil.MAX_COMMIT_COUNT;

if (max > umtsList.size())
{
max = umtsList.size();
}

createUmts_TransNew(umtsList.subList(min, max));
}
}
@Override
public void createUmts_TransNew(Collection list)
{
// TODO Auto-generated method stub

umtsDAO.saveAll(list);
}

DAO 类:

@Transactional
public void saveAll(Collection collection)
{
log.debug("** save all");
try
{
if (collection != null && collection.size() > 0)
{
for (Object obj : collection)
{
sessionFactory.getCurrentSession().saveOrUpdate(obj);
}
}
}
catch (RuntimeException re)
{
log.error("** save all failed", re);
throw re;
}
}

** 已编辑连接池在这里起作用吗?这意味着连接池有助于提高性能吗?我需要将 jar 文件添加到 wildfly 10 或应用程序本身吗?

最佳答案

首先,尝试增加pooled-lo值。由于您将其设置为 1,因此不会进行优化/池化 - 因为需要获取的每个 ID 都需要调用数据库来获取实际值。如果您有更大的增量大小,hibernate 将预取/保留 ID block 以用于新实体,而无需每个实体的往返。

不确定您发布的代码是如何执行的,但我假设您将它们按顺序插入到单个线程中。您可以:

  • 使用线程池,其中每个线程从列表/队列中获取多个项目进行插入。
  • 理想情况下,由一个线程插入到单个事务中的项目的大小与配置的 hibernate 批量大小相同,以再次最小化往返次数。
  • 确保池中的线程数与连接池大小一致,这样在等待连接时就不会阻塞工作线程。
  • 确保连接池大小对于服务器可以承受的负载来说是合理的,并使用良好的连接池(例如 HikariCP )。这是一个有趣的writeup on connection pool size.

关于java - 如何使用hibernate优化插入查询时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42107953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com