DataView 效能

本主題將討論使用 Find 類別 (Class) 之 FindRowsDataView 方法以及在 Web 應用程式中快取 DataView 的效能提升。

Find 和 FindRows

DataView 會建構索引。 索引中包含從資料表或檢視中一或多個資料行建出的索引鍵。 這些索引鍵會儲存在某個結構中,讓 DataView 能夠快速且有效率地尋找與索引鍵值相關聯的資料列。 使用索引的作業 (例如篩選與排序) 效能會大幅提升。 在您建立 DataView 以及修改任何排序或篩選資訊時,系統就會建立 DataView 的索引。 如果您建立 DataView,然後設定排序或篩選資訊,將會導致系統至少建立索引兩次:一次是建立 DataView 時,另一次是修改任何排序或篩選屬性時。 深入瞭解透過 DataView 進行篩選與排序,請參閱透過 DataView 進行篩選透過 DataView 進行排序

如果您想要傳回特定資料查詢的結果,但不要提供資料子集的動態檢視,就可以使用 FindFindRowsDataView 方法,而非設定 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 具有一套快取機制,可讓您在記憶體中儲存需要大量伺服器資源才能建立的物件。 快取這些資源類型可大幅改善應用程式的效能。 快取是使用每個應用程式私用的快取執行個體 (Instance) 由 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();  

另請參閱