- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
描述
我有一个使用 Hibernate ORM 与数据库通信的 Spring 应用程序。我有这个函数,它填充名为 orders、order_line 和 cc_xacts 的数据库表。表之间的关系如下所示:
-------- --------------
|orders|--- 1:m --->| order_line |
-------- --------------
| ------------
-------- 1:m --->| cc_xacts |
------------
所以 orders 与 cc_xacts 和 order_line 实体具有一对多关系。
public void populateOrdersAndCC_XACTSTable()
{
GregorianCalendar cal;
String[] credit_cards = {"VISA", "MASTERCARD", "DISCOVER", "AMEX",
"DINERS" };
int num_card_types = 5;
String[] ship_types = {"AIR", "UPS", "FEDEX", "SHIP", "COURIER", "MAIL" };
int num_ship_types = 6;
String[] status_types = {"PROCESSING", "SHIPPED", "PENDING", "DENIED" };
int num_status_types = 4;
// Order variables
int O_C_ID;
java.sql.Timestamp O_DATE;
double O_SUB_TOTAL;
double O_TAX;
double O_TOTAL;
String O_SHIP_TYPE;
java.sql.Timestamp O_SHIP_DATE;
int O_BILL_ADDR_ID, O_SHIP_ADDR_ID;
String O_STATUS;
String CX_TYPE;
int CX_NUM;
String CX_NAME;
java.sql.Date CX_EXPIRY;
String CX_AUTH_ID;
int CX_CO_ID;
System.out.println( "Populating ORDERS, ORDER_LINES, CC_XACTS with "
+ NUM_ORDERS + " orders" );
System.out.print( "Complete (in 10,000's): " );
for ( int i = 1; i <= NUM_ORDERS; i++ )
{
if ( i % 10000 == 0 )
System.out.print( i / 10000 + " " );
int num_items = getRandomInt( 1, 5 );
O_C_ID = getRandomInt( 1, NUM_CUSTOMERS );
cal = new GregorianCalendar();
cal.add( Calendar.DAY_OF_YEAR, -1 * getRandomInt( 1, 60 ) );
O_DATE = new java.sql.Timestamp( cal.getTime().getTime() );
O_SUB_TOTAL = (double) getRandomInt( 1000, 999999 ) / 100;
O_TAX = O_SUB_TOTAL * 0.0825;
O_TOTAL = O_SUB_TOTAL + O_TAX + 3.00 + num_items;
O_SHIP_TYPE = ship_types[getRandomInt( 0, num_ship_types - 1 )];
cal.add( Calendar.DAY_OF_YEAR, getRandomInt( 0, 7 ) );
O_SHIP_DATE = new java.sql.Timestamp( cal.getTime().getTime() );
O_BILL_ADDR_ID = getRandomInt( 1, 2 * NUM_CUSTOMERS );
O_SHIP_ADDR_ID = getRandomInt( 1, 2 * NUM_CUSTOMERS );
O_STATUS = status_types[getRandomInt( 0, num_status_types - 1 )];
Orders order = new Orders();
// Set parameter
order.setCustomer( customerDao.findById( O_C_ID ) );
order.setODate( new Date( O_DATE.getTime() ) );
order.setOSubTotal( O_SUB_TOTAL );
order.setOTax( O_TAX );
order.setOTotal( O_TOTAL );
order.setOShipType( O_SHIP_TYPE );
order.setOShipDate( O_SHIP_DATE );
order.setAddressByOBillAddrId( addressDao.findById( O_BILL_ADDR_ID ) );
order.setAddressByOShipAddrId( addressDao.findById( O_SHIP_ADDR_ID ) );
order.setOStatus( O_STATUS );
order.setCcXactses( new HashSet<ICcXacts>() );
order.setOrderLines( new HashSet<IOrderLine>() );
ordersDao.shrani( order );
for ( int j = 1; j <= num_items; j++ )
{
int OL_ID = j;
int OL_O_ID = i;
int OL_I_ID = getRandomInt( 1, NUM_ITEMS );
int OL_QTY = getRandomInt( 1, 300 );
double OL_DISCOUNT = (double) getRandomInt( 0, 30 ) / 100;
String OL_COMMENTS = getRandomAString( 20, 100 );
OrderLine orderLine = new OrderLine();
orderLine.setItem( itemDao.findById( OL_I_ID ) );
orderLine.setOlQty( OL_QTY );
orderLine.setOlDiscount( OL_DISCOUNT );
orderLine.setOlComment( OL_COMMENTS );
orderLine.setOrders( order );
orderLineDao.shrani( orderLine );
order.getOrderLines().add( orderLine );
}
CX_TYPE = credit_cards[getRandomInt( 0, num_card_types - 1 )];
CX_NUM = getRandomNString( 16 );
CX_NAME = getRandomAString( 14, 30 );
cal = new GregorianCalendar();
cal.add( Calendar.DAY_OF_YEAR, getRandomInt( 10, 730 ) );
CX_EXPIRY = new java.sql.Date( cal.getTime().getTime() );
CX_AUTH_ID = getRandomAString( 15 );
CX_CO_ID = getRandomInt( 1, 92 );
CcXacts ccXacts = new CcXacts();
ccXacts.setCountry( countryDao.findById( CX_CO_ID ) );
ccXacts.setOrders( order );
ccXacts.setCxType( CX_TYPE );
ccXacts.setCxNum( CX_NUM );
ccXacts.setCxName( CX_NAME );
ccXacts.setCxExpiry( CX_EXPIRY );
ccXacts.setCxAuthId( CX_AUTH_ID );
ccXacts.setCxXactAmt( O_TOTAL );
ccXacts.setCxXactDate( O_SHIP_DATE );
ccXacts.setCountry( countryDao.findById( CX_CO_ID ) );
order.getCcXactses().add( ccXacts );
ccXactsDao.shrani( ccXacts );
}
System.out.println( "" );
}
问题
当我想在 orders 实体中填充大约 250000 个订单时,问题就出现了,因为像我现在那样做它非常慢。用如此多的订单填充数据库需要花费很多时间。有时我也会用完 Java 堆大小并得到 OutOfMemoryException。
您有什么建议可以加快速度并且不至于耗尽 Java 堆大小吗?
最佳答案
您需要activate Hibernate batch support ,因此您需要设置以下 Hibernate 属性:
properties.put("hibernate.jdbc.batch_size", "50");
properties.put("hibernate.order_inserts", "true");
properties.put("hibernate.order_updates", "true");
properties.put("hibernate.jdbc.batch_versioned_data", "true");
为避免 OutOfMemoryError,您需要在批处理准备好刷新时清除当前 Session:
doInTransaction(session -> {
int batchSize = batchSize();
for(int i = 0; i < itemsCount(); i++) {
//batch insert logic
if(i % batchSize == 0 && i > 0) {
session.flush();
session.clear();
}
}
});
尝试维护一个实体映射,这样您就不必在每个批处理步骤中获取所有这些实体:
customerDao.findById( O_C_ID );
countryDao.findById( CX_CO_ID );
您可以在 batch process 的开头获取它们或者至少将结果缓存在本地 map 中。每个选择triggers the current Session to flush (如果您使用自动刷新),因此会降低批处理性能。
关于java - 如何加速 Hibernate 批处理并避免 OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862821/
我想在我的 iPhone 应用程序中加入线性回归。经过一些搜索,我发现 Accelerate Framework 中的 LAPACK 和 BLAS 是正确的库。但是我很难将加速框架添加到我的 XCod
有什么方法可以加速 JS 脚本(我指的是一些复杂的 DOM 操作,比如游戏或动画)? 最佳答案 真的没有办法真正加快速度。您可以压缩它,但不会快很多。 关于Javascript 加速?,我们在Stac
有时,我必须为一个项目重新导入数据,从而将大约 360 万行读入 MySQL 表(目前是 InnoDB,但我实际上并不局限于这个引擎)。 “加载数据文件...”已被证明是最快的解决方案,但它有一个权衡
在尝试计算加速时,我被卡住了。所以给出的问题是: 问题 1 如果程序的 50% 增强了 2 倍,其余 50% 增强了 4 倍,那么由于增强而导致的整体加速是多少? Hints:考虑增强前(未增强)机器
目前我正在处理实时绘图,但可视化非常慢。我想知道你可以做些什么来加速 Matplotlib 中的事情: 后端如何影响性能?是否有后端 实时绘图比其他人更好吗? 我可以降低分辨率以提高 FPS 吗? 如
我有一个小型测试框架。它执行一个循环,执行以下操作: 生成一个小的 Haskell 源文件。 使用 runhaskell 执行此操作.该程序生成各种磁盘文件。 处理刚刚生成的磁盘文件。 这种情况发生了
这是我的网站:Instant-YouTube 如您所见,加载需要很长时间。在 IE8 及以下甚至有时会导致浏览器崩溃。我不确定是什么原因造成的。可能是 Clicksor 广告,但我认为是 swfobj
是否可以加速 SKSpriteNode? 我知道可以使用 node.physicsBody.velocity 轻松设置速度但是设置它的加速度有多难? 最佳答案 从牛顿第二定律倒推运动:F = m.a您
有没有人有加速 FCKEditor 的技术?是否有一些关键的 JavaScript 文件可以缩小或删除? 最佳答案 在最新版本 (3.0.1) 中,FCKEditor 已重命名为 CKEditor .
我有以下 MySQL 查询,需要一天多的时间才能执行: SELECT SN,NUMBER FROM a WHERE SN IN (SELECT LOWER_SN FROM b WHER
我现在正在开发一款使用加速来玩的游戏。我找到了如何让我的元素移动,但不改变它的“原点”,或者更准确地说,改变加速度计算的原点: 事实上,我的图像是移动的,它的中心是这样定义的: imageView.c
我有一个 mysql 表,其中存储有 4 列的成员消息: message_id(主键,自增) sender_id( key ) receiver_id( key ) 消息内容 我做了很多 SELECT
我在 cuda_computation.cu 中有以下代码 #include #include #include #include void checkCUDAError(const char
我正在使用 BeautifulSoup 在 for 循环中解析数千个网站。这是我的代码片段: def parse_decision(link): t1 = time.time() de
我正在使用 OpenCV 2.4 (C++) 在灰度图像上进行寻线。这涉及一些基本的图像处理步骤,如模糊、阈值、Canny 边缘检测器、梯度滤波器或霍夫变换。我必须在数千张图像上应用寻线算法。 考虑到
当我试图连续生成四次相同的报告时,我刚刚分析了我的报告应用程序。第一个用了 1859 毫秒,而后面的只用了 400 到 600 毫秒。对此的解释是什么?我能以某种方式使用它来使我的应用程序更快吗?报告
当我打开 Storyboard文件时,由于其中包含的 VC 数量,打开它需要 1-2 分钟。加快速度的最佳做法是什么?我们应该将一些 VC 移动到不同的 Storyboard文件中吗?我们是否应该使用
我有一个包含多个页面的 UIPageViewController。每个页面都是相同的 View Controller ,但会跟踪页码并显示 PDF 的正确页面。问题是每个 PDF 页面都需要在 cur
这实际上是两个问题,但它们非常相似,为了简单起见,我想将它们放在一起: 首先:给定一个已建立的 Java 项目,除了简单的代码内优化之外,还有哪些不错的方法可以加快它的速度? 其次:在用Java从头写
我有一个包含 1000 个条目的文档,其格式类似于:
我是一名优秀的程序员,十分优秀!