DataView 效能DataView Performance

本主題將討論使用 Find 類別 (Class) 之 FindRowsDataView 方法以及在 Web 應用程式中快取 DataView 的效能提升。This topic discusses the performance benefits of using the Find and FindRows methods of the DataView class, and of caching a DataView in a Web application.

Find 和 FindRowsFind and FindRows

DataView 會建構索引。DataView constructs an index. 索引包含根據資料表或檢視中一個或多個資料表所建立的索引鍵。An index contains keys built from one or more columns in the table or view. 這些索引鍵會儲存在某個結構中,讓 DataView 能夠快速且有效率地尋找與索引鍵值相關聯的資料列。These keys are stored in a structure that enables the DataView to find the row or rows associated with the key values quickly and efficiently. 使用索引的作業,例如篩選和排序,會大幅提升效能。Operations that use the index, such as filtering and sorting, see significant performance increases. 在您建立 DataView 以及修改任何排序或篩選資訊時,系統就會建立 DataView 的索引。The index for a DataView is built both when the DataView is created and when any of the sorting or filtering information is modified. 如果您建立 DataView,然後設定排序或篩選資訊,將會導致系統至少建立索引兩次:一次是建立 DataView 時,另一次是修改任何排序或篩選屬性時。Creating a DataView and then setting the sorting or filtering information later causes the index to be built at least twice: once when the DataView is created, and again when any of the sort or filter properties are modified. 如需使用DataView進行篩選和排序的詳細資訊,請參閱使用 dataview 進行篩選使用 dataview 進行排序For more information about filtering and sorting with DataView, see Filtering with DataView and Sorting with DataView.

如果您想要傳回特定資料查詢的結果,但不要提供資料子集的動態檢視,就可以使用 FindFindRowsDataView 方法,而非設定 RowFilter 屬性。If you want to return the results of a particular query on the data, as opposed to providing a dynamic view of a subset of the data, you can use the Find or FindRows methods of the DataView, rather than setting the RowFilter property. RowFilter 屬性最適於資料繫結應用程式,因為這種應用程式會用繫結控制項顯示篩選結果。The RowFilter property is best used in a data-bound application where a bound control displays filtered results. 設定 RowFilter 屬性會重建資料索引,因而增加應用程式的負荷並降低效能。Setting the RowFilter property rebuilds the index for the data, adding overhead to your application and decreasing performance. FindFindRows 方法會使用目前的索引,而不需要重建索引。The Find and FindRows methods use the current index without requiring the index to be rebuilt. 如果您只要呼叫 FindFindRows 一次,就應該使用現有的 DataViewIf you are going to call Find or FindRows only once, then you should use the existing DataView. 如果您要呼叫 FindFindRows 多次,就應該建立新的 DataView 來重建您想要搜尋之資料行的索引,然後呼叫 FindFindRows 方法。If you are going to call Find or FindRows multiple times, you should create a new DataView to rebuild the index on the column you want to search on, and then call the Find or FindRows methods. 如需FindFindRows方法的詳細資訊,請參閱尋找資料For more information about the Find and FindRows methods, see Finding Rows.

下列範例會使用 Find 方法來尋找具有姓氏 "Zhu" 的連絡人。The following example uses the Find method to find a contact with the last name "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.
int 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 方法來尋找所有紅色的產品。The following example uses the FindRows method to find all the red colored products.

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";

object[] 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.NETASP.NET

ASP.NET 具有一套快取機制,可讓您在記憶體中儲存需要大量伺服器資源才能建立的物件。ASP.NET has a caching mechanism that allows you to store objects that require extensive server resources to create in memory. 快取這些資源類型可大幅改善應用程式的效能。Caching these types of resources can significantly improve the performance of your application. 快取是使用每個應用程式私用的快取執行個體 (Instance) 由 Cache 類別所實作的。Caching is implemented by the Cache class, with cache instances that are private to each application. 由於建立新的 DataView 物件可能會耗用大量資源,因此您可能會想要在 Web 應用程式中使用這項快取功能,避免每次重新整理網頁時必須重建 DataViewBecause creating a new DataView object can be resource intensive, you might want to use this caching functionality in Web applications so that the DataView does not have to be rebuilt every time the Web page is refreshed.

在下列範例中,DataView 會經過快取,避免重新整理頁面時必須重新排序資料。In the following example, the DataView is cached so that the data does not have to be re-sorted when the page is refreshed.

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();  

另請參閱See also