LimitException:查询行太多:来自count()聚合函数的50001

时间:2012-04-02 21:50:33

标签: salesforce apex-code visualforce soql

我有一个Visualforce页面,我想显示特定sObject表中记录数的计数。

在Visualforce页面中,我有一些相当简单的东西,例如:

<p>Client Account Count: {!ClientAccountCount}</p>

然后在控制器中:

// Return the number of clients
public integer getClientAccountCount() {
    return [Select count() from Account where SomeCustomField__c = 'Client' limit 50000];
}

我认为SOQL中的限制条款我会很好,因为每次返回最多只有50,000。但是,在实践中,我仍然在生产组织中得到这个例外:

  

09:29:12:179 SOQL_EXECUTE_BEGIN [108] |聚合:0 |从Account中选择count(),其中SomeCustomField__c ='Client'限制50000

     

09:29:12:331 EXCEPTION_THROWN [108] | System.LimitException:查询行太多:50001

是否有一种安全的方法来执行此查询,但不会导致我无法捕获的异常?

奇怪的是,如果我在生产中尝试以下作为匿名顶点,它可以正常运行并返回50,000.

integer count = [select count() from Account where SomeCustomField__c = 'Client' limit 50000];

问题可能是导致问题的所有操作的累积查询行数,我需要在运行查询之前检查代码中的限制吗?


Force.com讨论区上有一篇类似的帖子 - Too many query rows on COUNT(*) function。我无法将VF页面设置为只读以增加查询行限制。

1 个答案:

答案 0 :(得分:6)

卫生署!我很确定我需要检查SOQL查询为请求检索的累积记录数。因此,虽然一个SOQL查询最多可以获得50,000个记录,但每个查询不能达到50,000个。

猜猜如果需要,我可以使用Limits.getQueryRows() and Limits.getLimitQueryRows()来禁用SOQL查询。


我改变了getClientAccountCount()方法的工作方式。我认为只有每一个都能够指示当聚合函数受到限制时有多少行。

// Return the number of ad book clients
public string getClientAccountCount() {
    System.debug(LoggingLevel.Debug, 'getClientAccountCount() - Current Query Rows: ' + Limits.getQueryRows() + '/' + Limits.getLimitQueryRows());
    integer recordCount = [Select count() from Account where SomeCustomField__c = 'Client' limit 1001];
    if(recordCount == 1001) { return '1000+'; }
    return string.valueOf(recordCount);
}

这个想法 - Count the SOQL count() query as a single row query似乎值得推广。