getRowCount() VS getEstimatedRowCount()
六月 13, 2009 in Oracle 融合中间件
很久没有更新日志了,最近一段时间一直在为项目解决Oralce ADF的问题,同时也在学习和总结Oracle ADF的使用和开发技巧,希望逐步能够形成自己的最佳实践总结。
今天要描述的是ViewObjectImpl中针对行集的两个统计记录行数的两个方法:getRowCount()和getEstimatedRowCount()
在Oracle ADF的Web应用中,经常会需要统计行记录的数量,如在表格的底部显示表格查询的记录数这样的需求,而如果不能够切当的使用方法,很有可能导致严重的性能问题。
可能会有人提出疑问“有这么严重吗?”,下面是Oracle ADF Java DOC中对于两个方法的描述:
1,public int getRowCount()
统计行集的行总数
这个方法通过执行视图对象查询并调用next()方法直到最后一行的方法来获得视图对象的所有行,由于需要遍历视图对象的所有记录,所有这个方法比较慢,
如果你的记录集的数量比较大,又或者你的系统需要一个快速的响应需求,请使用getEstimatedRowCount来快速获取记录数量。
下面的例子使用getRowCount()方法来遍历奇数和偶数行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // Default iterator gets even-numbered rows. // Second iterator gets odd-numbered rows. long nRows = vo.getRowCount(); String msg = ""; for (int i = 0; i < nRows; i +=2) { // Get and set row index values relative to a range. // Index of first row = 0. vo.setCurrentRowAtRangeIndex(i); Row currRow = vo.getCurrentRow(); msg = " Default iterator (even): " + vo.getRangeIndexOf(currRow); printRow(currRow, msg); secondIter.setCurrentRowAtRangeIndex(i + 1); currRow = secondIter.getCurrentRow(); msg = " Second iterator (odd): " + vo.getRangeIndexOf(currRow); printRow(secondIter.getCurrentRow(), msg); } |
2,public long getEstimatedRowCount()
对行集的记录数做一个预估
这个方法通过调用getQueryHitCount(执行SELECT COUNT(*) FROM table)来估量。Java业务组件从内部会保持在行插入或者删除时EstimatedRowCount保持最新,只要第一次调用之后,它能够快速的返回评估的数量。
1 2 | // Get the rowcount again because of deleted or inserted row rowCount = (int) iter.getRowSet().getEstimatedRowCount(); |
如果你的记录集的数量比较大,又或者你的系统需要一个快速的响应需求,请使用getEstimatedRowCount来快速获取记录数量。
然而,这个方法可能没有getRowCount()准确,为了测试视图对象是否从游标读到所有的行,你可以联合getEstimatedRowCount()和getFetchedRowCount()方法,
例如,getEstimatedRowCount()返回等同于count(*),getFetchedRowCount 返回已经获取的行数,如果getFetchedRowCount ()返回的数量比getEstimatedRowCount()小,视图对象则还没有从游标中读到所有的行。
综上所述:
- 如果在应用中,我们需要遍历行集中的所有记录,如得到每行上面的一些属性值,则可以选择使用getRowCount()方法;
- 而如果我们仅仅只需要知道行集数量的时候,那就使用getEstimatedRowCount()方法,如在表格的底部显示记录总数,当然这可以通过#{bindings.iteratorName.estimatedRowCount}来实现,具体可见Metalink:806693.1。
无相关文章.
0 responses to getRowCount() VS getEstimatedRowCount()