对相同对象的数据检索分组

上次修改时间: 2011年5月27日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

为了改进性能,总是会尝试将针对同一对象的数据检索请求组合在一起。

在下面的示例中,Method1 和 Method2 都将检索网站的标题和说明以及"通知"列表的说明,只不过 Method1 的性能高于 Method2 的性能。

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class RetrieveData
    {
        // The following example increases performance by grouping the Title and Description 
        // property calls together in the same Load method call.
        static void Method1()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            List oList = oWebsite.Lists.GetByTitle("Announcements");

            clientContext.Load(oWebsite, 
                website => website.Title, 
                website => website.Description);

            clientContext.Load(oList, 
                list => list.Description);

            clientContext.ExecuteQuery();
        }

        // The following example decreases performance because the Title and Description
        // property calls are not grouped together in the same Load method call.
        static void Method2()
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            List oList = oWebsite.Lists.GetByTitle("Announcements");

            clientContext.Load(oWebsite, 
                website => website.Title);

            clientContext.Load(oList, 
                list => list.Description);

            clientContext.Load(oWebsite, 
                website => website.Description);

            clientContext.ExecuteQuery();
        }
    }
}
Imports System
Imports Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class RetrieveData

        // The following example increases performance by grouping the Title and Description 
        // property calls together in the same Load method call.
        Shared Sub Method1()
            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
            Dim oWebsite As Web = clientContext.Web
            Dim oList As List = oWebsite.Lists.GetByTitle("Announcements")
         
            clientContext.Load(oWebsite, Function(website) website.Title, _
                Function(website) website.Description)

            clientContext.Load(oList, Function(list) list.Description)
         
            clientContext.ExecuteQuery()
        End Sub 'Method1
      
        ' The following example decreases performance because the Title and Description
        ' property calls are not grouped together in the same Load method call.      
        Shared Sub Method2()
            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
            Dim oWebsite As Web = clientContext.Web
            Dim oList As List = oWebsite.Lists.GetByTitle("Announcements")
         
            clientContext.Load(oWebsite, Function(website) website.Title)

            clientContext.Load(oList, Function(list) list.Description)

            clientContext.Load(oWebsite, Function(website) website.Description)
         
            clientContext.ExecuteQuery()

        End Sub
    End Class
End Namespace
// The following example increases performance by grouping the Title and Description 
// property calls together in the same Load method call.
function Method1 () {

        var clientContext = new SP.ClientContext('/sites/TestWebs/TestWeb1');
        this.oWebsite = clientContext.get_web();
        this.oList = oWebsite.get_lists().getByTitle('Announcements');

        clientContext.load(oWebsite, 'Title', 'Description');
        clientContext.load(oList, 'Description');
        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

// The following example decreases performance because the Title and Description
// property calls are not grouped together in the same Load method call.
function Method2 () {

        var clientContext = new SP.ClientContext('/sites/TestWebs/TestWeb1');
        this.oWebsite = clientContext.get_web();
        this.oList = oWebsite.get_lists().getByTitle('Announcements');

        clientContext.load(oWebsite, 'Title');
        clientContext.load(oList, 'Description');
        clientContext.load(oWebsite, 'Description');
        
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

在 Method1 中,用于检索网站的标题和说明的代码将组合在一起;而在 Method2 中,用于检索这些属性的代码将由其他操作分离。Method2 将触发对同一网站对象的两个单独查询,从而导致出现针对同一对象的两个结果集。由于客户端库旨在返回一致的数据,因此第二个结果集将包含网站的标题和说明,而第一个结果集也将包含网站的标题。

下面的 SQL 命令之间的差异说明了上述两种方法之间的不同之处。

Method1:
SELECT Title, Description FROM Webs WHERE ...
SELECT Description FROM Lists WHERE ...

Method2:
SELECT Title FROM Webs WHERE ...
SELECT Description FROM Lists WHERE ...
SELECT Title, Description FROM Webs WHERE ...

请参阅

概念

数据检索概述

在访问值属性之前调用 Load 和 ExecuteQuery

值对象不能在查询中跨方法使用

可以在查询的各个方法之间使用客户端对象

检索客户端对象并不会检索所有属性