限制结果集。
如果查询返回的项目多于在 Microsoft SharePoint Foundation 2010 中配置的查询阈值,则将阻止查询并且不显示结果。默认查询阈值为 5,000 项,但您可以将其设置为更小的值。对于 Microsoft SharePoint Server 2010 查询,如果您需要所有项目,请使用 ContentIterator.MaxItemsPerQuery 并分页显示结果。
使用索引字段。
如果所查询的字段未编制索引,并且生成的扫描在列表中遇到的项目多于查询阈值,则将阻止查询。请将 SPQuery.RowLimit 设置为小于查询阈值的值。确保 ContentIterator.MaxItemsPerQuery 的值小于或等于该阈值,这是为 SharePoint Server 2010 推荐的值。
SPQuery query = new SPQuery();
query.Query =
"<Where><Eq><FieldRef Name=\"MyIndexedField\"/><Value Type=\"Text\">FieldValue</Value></Eq></Where>"
+ ContentIterator.ItemEnumerationOrderByNVPField;
ContentIterator ci = new ContentIterator();
ci.ProcessItemsInList(query,
delegate(SPListItem item)
{
// Work on each item.
},
delegate(SPListItem item, Exception e)
{
// Handle an exception that was thrown while iterating.
// Return true so that ContentIterator rethrows the exception.
return true;
}
);
使用 PortalSiteMapProvider
使用 PortalSiteMapProvider(仅限 Microsoft Office SharePoint Server 2007 和 Microsoft SharePoint Server 2010)。
Steve Peschka 的白皮书在 Office SharePoint Server 2007 中处理大型列表(英文)介绍了使用 PortalSiteMapProvider 类检索列表数据的有效方法。PortalSiteMapProvider 提供了用于检索列表数据的自动缓存基础结构。PortalSiteMapProvider 的 GetCachedListItemsByQuery 方法采用 SPQuery 对象作为参数,然后检查其缓存以确定项目是否已经存在。如果已经存在,则该方法返回缓存的结果。如果不存在,则该方法查询列表并将结果存储在缓存中。当检索的列表数据在一段时间内不会频繁更改时,该方法尤其有效。如果数据集频繁更改,则除了从数据库读取数据所产生的开销外,该类还会因为不断写入缓存而产生性能开销。请注意,PortalSiteMapProvider 类使用网站集对象缓存来存储数据。此缓存默认大小为 100 MB。您可以在每个网站集的对象缓存设置页上增加网站集的此缓存大小。但是,由于此内存来自供应用程序池使用的共享内存,因此可能会影响其他应用程序的性能。另一个重要限制是您不能在基于 Windows 窗体的应用程序中使用 PortalSiteMapProvider 类。以下代码示例显示如何使用此方法。 良好的编码实践 使用 PortalSiteMapProvider
// Get the current SPWeb object.
SPWeb curWeb = SPControl.GetContextWeb(HttpContext.Current);
// Create the query.
SPQuery curQry = new SPQuery();
curQry.Query = "<Where><Eq><FieldRef Name='Expense_x0020_Category'/>
<Value Type='Text'>Hotel</Value></Eq></Where>";
// Create an instance of PortalSiteMapProvider.
PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider;
PortalWebSiteMapNode pNode = ps.FindSiteMapNode(curWeb.ServerRelativeUrl) as PortalWebSiteMapNode;
// Retrieve the items.
SiteMapNodeCollection pItems = ps.GetCachedListItemsByQuery(pNode, "myListName_NotID", curQry, curWeb);
// Enumerate through all of the matches.
foreach (PortalListItemSiteMapNode pItem in pItems)
{
// Do something with each match.
}