DataView 性能

本主题讨论在 Web 应用程序中使用 DataView 类的 FindFindRows 方法以及缓存 DataView 的性能优势。

Find 和 FindRows

DataView 构造索引。 索引包含从表或视图中的一个或多个列生成的键。 这些键存储在一个结构中,该结构可用于使DataView快速且高效地查找与键值关联的行。 使用索引的操作(如筛选和排序)会显著提高性能。 创建 DataView 及修改任何排序或筛选信息时,均会生成 DataView 的索引。 创建一个DataView,然后再设置排序或筛选信息,将导致索引至少被构建两次:第一次是在DataView创建时,第二次是在任何排序或筛选属性被修改时。 有关筛选和排序 DataView的详细信息,请参阅 使用 DataView 进行筛选和 DataView 排序

如果要返回对数据的特定查询的结果,而不是提供数据子集的动态视图,则可以使用 FindFindRows 方法 DataView,而不是设置 RowFilter 属性。 该 RowFilter 属性最适用于数据绑定应用程序中,其中绑定控件显示筛选的结果。 RowFilter设置属性会重新生成数据的索引,从而增加应用程序开销并降低性能。 和FindFindRows方法使用当前索引,而无需重新生成索引。 如果只调用一次FindFindRows,则应使用现有的DataView。 如果要调用 FindFindRows 多次调用,则应创建一个新 DataView 方法来重新生成要搜索的列上的索引,然后调用 FindFindRows 方法。 有关FindFindRows方法的详细信息,请参阅“查找行”

下面的示例使用该方法 Find 查找姓氏为“Zhu”的联系人。

DataTable contacts = _dataSet.Tables["Contact"];

EnumerableRowCollection<DataRow> query = from contact in contacts.AsEnumerable()
                                         orderby contact.Field<string>("LastName")
                                         select contact;

DataView view = query.AsDataView();

// Find a contact with the last name of Zhu.
var found = view.Find("Zhu");
Dim contacts As DataTable = dataSet.Tables("Contact")

Dim query = _
    From contact In contacts.AsEnumerable() _
    Order By contact.Field(Of String)("LastName") _
    Select contact

Dim view As DataView = query.AsDataView()

Dim found As Integer = view.Find("Zhu")

以下示例使用 FindRows 该方法查找所有红色产品。

DataTable products = _dataSet.Tables["Product"];

EnumerableRowCollection<DataRow> query = from product in products.AsEnumerable()
                                         orderby product.Field<decimal>("ListPrice"), product.Field<string>("Color")
                                         select product;

DataView view = query.AsDataView();

view.Sort = "Color";

var criteria = new object[] { "Red" };

DataRowView[] foundRowsView = view.FindRows(criteria);
Dim products As DataTable = dataSet.Tables("Product")

Dim query = _
From product In products.AsEnumerable() _
Order By product.Field(Of Decimal)("ListPrice"), product.Field(Of String)("Color") _
Select product

Dim view As DataView = query.AsDataView()
view.Sort = "Color"

Dim criteria As Object() = New Object() {"Red"}

Dim foundRowsView As DataRowView() = view.FindRows(criteria)

ASP.NET

ASP.NET 有一种缓存机制,可用于存储需要大量服务器资源才能在内存中创建的对象。 缓存这些类型的资源可以显著提高应用程序的性能。 缓存由 Cache 类实现,缓存实例是每个应用程序专用的。 由于创建新 DataView 对象可能占用大量资源,因此可能需要在 Web 应用程序中使用此缓存功能,以便 DataView 每次刷新网页时都不必重新生成该缓存功能。

在以下示例中,缓存 DataView 数据,以便在刷新页面时不必重新排序数据。

If (Cache("ordersView") = Nothing) Then  
  
Dim dataSet As New DataSet()  
  
   FillDataSet(dataSet)  
  
   Dim orders As DataTable = dataSet.Tables("SalesOrderHeader")  
  
   Dim query = _  
                    From order In orders.AsEnumerable() _  
                    Where order.Field(Of Boolean)("OnlineOrderFlag") = True _  
                    Order By order.Field(Of Decimal)("TotalDue") _  
                    Select order  
  
   Dim view As DataView = query.AsDataView()  
  
   Cache.Insert("ordersView", view)  
  
End If  
  
Dim ordersView = CType(Cache("ordersView"), DataView)  
  
GridView1.DataSource = ordersView  
GridView1.DataBind()  
if (Cache["ordersView"] == null)  
{  
   // Fill the DataSet.
   DataSet dataSet = FillDataSet();  
  
   DataTable orders = dataSet.Tables["SalesOrderHeader"];  
  
   EnumerableRowCollection<DataRow> query =  
                        from order in orders.AsEnumerable()  
                        where order.Field<bool>("OnlineOrderFlag") == true  
                        orderby order.Field<decimal>("TotalDue")  
                        select order;  
  
   DataView view = query.AsDataView();  
   Cache.Insert("ordersView", view);  
}  
  
DataView ordersView = (DataView)Cache["ordersView"];  
  
GridView1.DataSource = ordersView;  
GridView1.DataBind();  

另请参阅