3

我有一个 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 中的 limit 子句会很好,因为它每次最多只能返回 50,000。但是,在实践中,我仍然在生产组织中遇到此异常:

09:29:12:179 SOQL_EXECUTE_BEGIN [108]|Aggregations:0|select count() from Account where SomeCustomField__c = 'Client' limit 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 页面设置为只读以增加查询行限制。

4

1 回答 1

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);
}

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

于 2012-04-02T22:04:39.587 回答