【问题标题】:Get total aggregate records count if records is having more than 50000 records如果记录有超过 50000 条记录,则获取总聚合记录数
【发布时间】:2019-03-26 11:22:02
【问题描述】:

我们的 CRM 实体中有大量记录。我正在尝试借助 fetch xml 中的聚合计数来获取记录总数。但它有 50000 条记录的限制。我认为有一种方法可以更改本地 CRM 中的设置。但我不想改变这一点。

以前我们使用分页方法来获取总计数(每次 5000)。但这需要很多时间

public static int GetTotalRowCount(string fetchXml)
{
  try
  {
    using (OrganizationServiceContext svcContext = new OrganizationServiceContext(ServerConnection.CrmService))
    {
      int totalCount = 0;
      int fetchCount = 5000;
      int pageNumber = 1;
      string pagingCookie = null;
      string xml = string.Empty;
      RetrieveMultipleRequest fetchRequest1 = null;
      EntityCollection entityCollection = null;

      xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
      fetchRequest1 = new RetrieveMultipleRequest
      {
        Query = new FetchExpression(xml)
      };

      entityCollection = ((RetrieveMultipleResponse)svcContext.Execute(fetchRequest1)).EntityCollection;

      while (entityCollection.MoreRecords)
      {
        //moving to next page
        pageNumber++;

        xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
        fetchRequest1 = new RetrieveMultipleRequest
        {
          Query = new FetchExpression(xml)
        };

        entityCollection = ((RetrieveMultipleResponse)svcContext.Execute(fetchRequest1)).EntityCollection;
        totalCount = totalCount + entityCollection.Entities.Count;
      }

      return totalCount;
    }
  }
  catch (Exception ex)
  {
  }
}

但这需要很多时间。因此我将其更改为聚合计数方法 - 像这样更改 Fetchxml -

<fetch mapping='logical' output-format='xml-platform' no-lock='true' distinct='false' aggregate='true'>
  <entity name='abc_data'>
    <attribute name='abc_id' aggregate='count' alias='count'/>.....

这样的代码

 int Count = 0;
 FetchExpression fetch = new FetchExpression(fetchXml);
 EntityCollection result = ServerConnection.CrmService.RetrieveMultiple(fetch);
 if (result.Entities.Count > 0)
 {
     Entity entity = result.Entities[0];
     AliasedValue value = (AliasedValue)entity["count"];
     Count = (int)value.Value;
  }
  return Count ;

现在,如果记录超过 50000 条,则会出现异常。

那么有没有办法在聚合计数的帮助下一次获取 50000 条记录并遍历它以获取总计数?

【问题讨论】:

标签: c# dynamics-crm crm dynamics-crm-2015


【解决方案1】:

FetchXML 聚合的限制是我们都面临的挑战。我想一劳永逸地解决这个问题,所以我构建了一个名为 AggX 的在线工具来在任意数量的行上运行聚合。目前可以免费使用。

您可以通过 https://aggx.meta.tools 查看它,请注意它仅适用于 Dynamics 365 Online。另外,请注意,如果您有大量的行需要超过 5 分钟才能运行,您应该监控 AggX 以避免在几分钟的空闲时间后自动将您注销。

如果您的系统是本地系统,或者您想编写自己的代码来进行聚合,您可以设计一种算法将数据拆分为少于 50,000 行的块。然后,您可以在每个块上运行 FetchXML 聚合并对结果求和。这就是 AggX 引擎的工作原理。

【讨论】:

  • 是的,我尝试过这样做.. 更改了我的 fetchxml,例如 - .. 这样我就可以一次获取 50000 块 .. 但它仍然返回超过 50000
  • FetchXML 被硬编码为在 5,000 处停止,无论我们设置的页面大小如何。您必须根据数据本身进行分块,例如“名称以'A'开头”,然后“名称以'B'开头”等等。或者“一月创建”,然后在二月创建”等等。
  • 这些针对专业产品的建议解决方案绝对不合标准。我们到底在为什么付出?
猜你喜欢
  • 2013-04-13
  • 2013-12-19
  • 1970-01-01
  • 2017-01-31
  • 2014-11-08
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
相关资源
最近更新 更多