DataView.Sort是性能瓶颈
我在 DataView.Sort 上遇到性能瓶颈。代码如下。
/// <summary>
/// Filters the data table and returns a new data table with only the filtered rows.
/// </summary>
/// <param name="dtInput">The dt input.</param>
/// <param name="filterExpression">The filter expression.</param>
/// <returns></returns>
protected virtual DataTable FilterDataTable(DataTable dtInput, string filterExpression)
{
DataTable result = dtInput;
if (!string.IsNullOrEmpty(filterExpression) && filterExpression.Trim().Length > 0)
{
DataView view = new DataView(dtInput);
view.RowFilter = filterExpression;
view.Sort = HierarchyFieldMap.DisplayedValue;
result = view.ToTable();
}
return result;
}
关于如何改进这种方法有什么想法吗?
执行大约需要 1 秒。
编辑
I have a performance bottleneck on a DataView.Sort. The code is below.
/// <summary>
/// Filters the data table and returns a new data table with only the filtered rows.
/// </summary>
/// <param name="dtInput">The dt input.</param>
/// <param name="filterExpression">The filter expression.</param>
/// <returns></returns>
protected virtual DataTable FilterDataTable(DataTable dtInput, string filterExpression)
{
DataTable result = dtInput;
if (!string.IsNullOrEmpty(filterExpression) && filterExpression.Trim().Length > 0)
{
DataView view = new DataView(dtInput);
view.RowFilter = filterExpression;
view.Sort = HierarchyFieldMap.DisplayedValue;
result = view.ToTable();
}
return result;
}
Any idea's on how to improve this method?
It takes ~1 second to execute.
EDIT
I found this link on DataView's Poor Peformance with Large RecordSets
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您返回的不是
DataView
而是DataTable
,因此您应该能够获得性能提升 - 不是数量级,而是 25-30% - 通过使用 DataTable.Sort:大部分时间都用于将数据复制到新表中。如果您可以避免创建新表并使用
DataTable.Select
返回的DataRow
数组,您可以获得相当大的改进。Since you're not returning a
DataView
but aDataTable
, you should be able to get a performance boost - not order-of-magnitude, but 25-30% - by usingDataTable.Sort
:Most of the time that's being taken up there is copying the data into the new table. If you can avoid creating a new table and work with the array of
DataRow
s thatDataTable.Select
returns you can get a considerable improvement.使用所有可用索引和统计信息对数据库进行排序可能会更快,特别是如果您在向用户显示结果之前对结果进行分页。
It may be quicker to sort in database with all the indexes and stats available, especially if you paginate the result before displaying to the user.
我同意 Shen 的观点,当您必须对 50 - 100k 行进行排序时,是时候将一些逻辑移动到专门用于此目的的层(数据库)。创建一个将 rowlimit 和当前页作为参数的存储过程,或者根据这些值构建您的 select 语句,.NET 速度很快,但没有针对此类操作进行优化,而 SQL Server(或任何与此相关的 RDBMS) )。
I agree with Sheng here, when you have to sorting 50 - 100k rows it's time to move some logic to the layer that is meant just for that, the database. Create a stored Procedure that takes the rowlimit and current page as parameters, or jsut build your select statement based on those values, .NET is fast, but not optimized for these kind of operations, where as SQL server (or any RDBMS for that matter).