微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CSOM ListCollectionItems 计数基于文件夹路径性能问题

如何解决CSOM ListCollectionItems 计数基于文件夹路径性能问题

我有一个设计要求,可以根据文件夹路径指定的文件获取 ListItemCollection 的总数。我目前有以下代码

using (ClientContext spClientContext = AuthenticationManager().GetACSAppOnlyContext(siteUrl,_clientId,_clientSecret))
{
    if (spClientContext != null)
    {
        int totalCount = 0;
        
        CamlQuery camlQuery = new CamlQuery();

        camlQuery.FolderServerRelativeUrl = getSiteUrlAbsolutePath(siteUrl) +
            "/Shared Documents/" + folderPath;

        camlQuery.ViewXml = "<View Scope="RecursiveAll">" +
                                "<Query>" +
                                    "<Where>" +
                                        "<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>" +
                                    "</Where>" +
                                "</Query>" +
                            "</View>";

        List list = spClientContext.Web.Lists.GetByTitle("Documents");

        ListItemCollection listItems = list.GetItems(camlQuery);

        spClientContext.Load(listItems);

        spClientContext.ExecuteQuery();

        if (listItems != null && listItems.Count > 0)
        {
            foreach (ListItem item in listItems)
            {
                if (item.FileSystemObjectType.Equals(FileSystemObjectType.File))
                {
                    totalCount++;
                }
            }
        }
    }
}

这对我来说似乎是一个非常简单的查询。我没有加载一堆参数等。但是,当前需要以下时间来执行:

Total Count = 403
Execution Time =  ~ 1276 ms

对于一个简单的查询来说,这似乎特别长?有什么明显的方法可以优化代码吗?

编辑[回复@Darshani Jayasekara]:

更新以显示隔离时间记录:

using (ClientContext spClientContext = AuthenticationManager().GetACSAppOnlyContext(siteUrl,_clientSecret))
{
    if (spClientContext != null)
    {
        int totalCount = 0;
        
        CamlQuery camlQuery = new CamlQuery();

        camlQuery.FolderServerRelativeUrl = getSiteUrlAbsolutePath(siteUrl) +
            "/Shared Documents/" + folderPath;

        camlQuery.ViewXml = "<View Scope="RecursiveAll">" +
                                "<Query>" +
                                    "<Where>" +
                                        "<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>" +
                                    "</Where>" +
                                "</Query>" +
                            "</View>";
                            
        // ************ Start Execution Time Logging ***************************************************************                            
        Stopwatch stopwatch = Stopwatch.StartNew();             

        List list = spClientContext.Web.Lists.GetByTitle("Documents");

        ListItemCollection listItems = list.GetItems(camlQuery);

        spClientContext.Load(listItems);

        spClientContext.ExecuteQuery();
    
        // ************ Stop Execution Time Logging ***************************************************************     
        stopwatch.Stop();
        
        Debug.WriteLine("Execution time: " + stopwatch.ElapsedMilliseconds + " ms.");       

        if (listItems != null && listItems.Count > 0)
        {
            foreach (ListItem item in listItems)
            {
                if (item.FileSystemObjectType.Equals(FileSystemObjectType.File))
                {
                    totalCount++;
                }
            }
        }
    }
}

5 次运行的执行时间:

Execution time: 1348 ms. Total Count = 403
Execution time: 1125 ms. Total Count = 403
Execution time: 1447 ms. Total Count = 403
Execution time: 1102 ms. Total Count = 403
Execution time: 1402 ms. Total Count = 403

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。