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

Dan*_*ger 3 salesforce soql visualforce apex-code

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

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

<p>Client Account Count: {!ClientAccountCount}</p>
Run Code Online (Sandbox Code Playgroud)

然后在控制器中:

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

我认为在SOQL中使用limit子句我会很好,因为它只会返回最多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];
Run Code Online (Sandbox Code Playgroud)

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


Force.com讨论板上有一篇类似的帖子 - COUNT(*)函数上的查询行太多了.我无法将VF页面设置为只读以增加查询行限制.

Dan*_*ger 6

卫生署!我很确定我需要检查SOQL查询为请求检索的累积记录数.因此,当一个SOQL查询最多可以获得50,000个记录时,两个不能分别执行50,000个记录.

猜猜我可以使用Limits.getQueryRows()和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);
}
Run Code Online (Sandbox Code Playgroud)

这个想法 - 将SOQL count()查询计为单行查询似乎值得推广.