DataView.RowFilter Vs DataTable.Select()vs DataTable.Rows.Find() [英] DataView.RowFilter Vs DataTable.Select() vs DataTable.Rows.Find()

查看:536
本文介绍了DataView.RowFilter Vs DataTable.Select()vs DataTable.Rows.Find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

  Dataview someView = new DataView(someable)
someView.RowFilter = someFilter;

if(someView.count> 0){...。 #$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $在VS2008之前。



解决:DataView的糟糕性能与大型记录集的谜团

DataRecord与DataView的数组:性能的戏剧性差异



Googling on this topic我发现一些文章/论坛主题它提到Datatable.Select()本身是非常错误的(不确定在这个)和在各种情况下表现不佳。



在此(最佳实践ADO.NET )主题关于msdn,建议如果在datatable上定义了主键,则findrows ()或find()方法应该使用Datatabl e.Select()。



这篇文章这里(.NET 1.1)基准测试所有三种方法加上几个。但是这是1.1版,所以不知道这些是否仍然有效。加入此DataRowCollection.Find()优于所有方法,Datatable.Select()优于DataView.RowFilter。



所以我很困惑,可能是最好的方法在数据表中查找行。或者没有一个很好的方法来做到这一点,根据场景有多种解决方案?

解决方案

Aseem,



您正在寻找在数据表中查找行的最佳方法,所以我首先要问:最好是什么?我想,任何技术都有可能比其他方式更好的场景。



首先,看看 DataView.RowFilter :DataView在数据绑定中有一些优势。它非常面向视图,因此它具有强大的排序,筛选或搜索功能,但会产生一些开销,并未针对性能进行优化。我会选择 DataView.RowFilter 用于较小的记录集和/或您利用其他功能(如直接数据绑定到视图)。



关于DataView的大多数事实,您可以在旧帖子中阅读,仍然适用。



其次,您应该更喜欢 DataTable.Rows.Find 超过 DataTable.Select 如果您只想单击一次。为什么? DataTable.Rows.Find只返回一行。基本上,当您指定主键时,将创建一个二叉树。这有一些与之相关的开销,但是极大地加快了检索速度。



DataTable.Select 更慢,但如果您有多个条件,并且不关心索引或未索引的行,则可以非常方便:它可以基本找到所有内容,但未针对性能进行优化。基本上,DataTable.Select必须遍历整个表格,并将每个记录与您传入的条件进行比较。



希望您能够发现这个小概述有用。 >

我建议看看 这篇文章 ,这对我来说对于性能问题有帮助。这个帖子包含一些引号。



一点点更新:
顺便说一下,这可能看起来有点超出范围的问题,但它几乎总是最好的解决方案来做后台的过滤和搜索。如果您想要简单,并且在客户端上具有 SQL Server 作为后端和.NET3 +,请转到LINQ to SQL。搜索Linq对象非常舒适,并创建在服务器端执行的查询。虽然LINQ对对象也是一个非常舒适但也较慢的技术。如果你不知道....



最好的问候,
托马斯


Considering the code below:

Dataview someView = new DataView(sometable)
someView.RowFilter = someFilter;

if(someView.count > 0) {  …. }

Quite a number of articles which say Datatable.Select() is better than using DataViews, but these are prior to VS2008.

Solved: The Mystery of DataView's Poor Performance with Large Recordsets
Array of DataRecord vs. DataView: A Dramatic Difference in Performance

Googling on this topic I found some articles/forum topics which mention Datatable.Select() itself is quite buggy(not sure on this) and underperforms in various scenarios.

On this(Best Practices ADO.NET) topic on msdn it is suggested that if there is primary key defined on a datatable the findrows() or find() methods should be used insted of Datatable.Select().

This article here (.NET 1.1) benchmarks all the three approaches plus a couple more. But this is for version 1.1 so not sure if these are valid still now. Accroding to this DataRowCollection.Find() outperforms all approaches and Datatable.Select() outperforms DataView.RowFilter.

So I am quite confused on what might be the best approach on finding rows in a datatable. Or there is no single good way to do this, multiple solutions exist depending upon the scenario?

解决方案

Aseem,

You are looking for the "best approach on finding rows in a datatable", so I first have to ask: "best" for what? I think, any technique has scenarios where it might fit better then the others.

First, let's look at DataView.RowFilter: A DataView has some advantages in Data Binding. Its very view-oriented so it has powerful sorting, filtering or searching features, but creates some overhead and is not optimized for performance. I would choose the DataView.RowFilter for smaller recordsets and/or where you take advantage of the other features (like, a direct data binding to the view).

Most facts about the DataView, which you can read in older posts, still apply.

Second, you should prefer DataTable.Rows.Find over DataTable.Select if you want just a single hit. Why? DataTable.Rows.Find returns only a single row. Essentially, when you specify the primary key, a binary tree is created. This has some overhead associated with it, but tremendously speeds up the retrieval.

DataTable.Select is slower, but can come very handy if you have multiple criteria and don't care about indexed or unindexed rows: It can find basically everything but is not optimized for performance. Essentially, DataTable.Select has to walk the entire table and compare every record to the criteria that you passed in.

I hope you find this little overview helpful.

I'd suggest to take a look at this article, it was helpful for me regarding performance questions. This post contains some quotes from it.

A little UPDATE: By the way, this might seem a little out of scope of your question, but its nearly always the fastest solution to do the filtering and searching on the backend. If you want the simplicity and have an SQL Server as backend and .NET3+ on client, go for LINQ-to-SQL. Searching Linq objects is very comfortable and creates queries which are performed on server side. While LINQ-to-Objects is also a very comfortable but also slower technique. In case you didn't know already....

best regards, thomas

这篇关于DataView.RowFilter Vs DataTable.Select()vs DataTable.Rows.Find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆