2

查询简单,在 VF 页面控制器中耗时 1 分钟,在 Developer Console 中执行时不到 1 秒。查询正在搜索超过 50,000 条记录。为什么查询运行时间有如此巨大的差异?

String s = '123456';
List<Registration__c> regs = 
    [select id, name 
     from Registration__c 
     where name =: s or speical_number__c =: s limit 1];

以下是调试日志的摘录:

开发者控制台:

12:22:39.063 (63557000)|SOQL_EXECUTE_BEGIN|[2]|Aggregations:0|select id, name from Registration__c where (name = :tmpVar1 or speical_number__c = :tmpVar2) limit 1

12:22:39.263 (263582000)|SOQL_EXECUTE_END|[2]|行数:0

VF 页面控制器:

12:17:08.148 (3148592000)|SOQL_EXECUTE_BEGIN|[633]|Aggregations:0|select id, name from Registration__c where (name = :tmpVar1 or speical_number__c = :tmpVar2) limit 1

12:18:07.350 (62350264000)|SOQL_EXECUTE_END|[633]|行数:0

4

1 回答 1

0

我怀疑这与 VF/Apex 批量回复的方式有关。虽然,限制 1 似乎表明情况并非如此。但是,只是为了好玩......试试这个......

String s = '123456';
List<Registration__c> regs = new List<Registration__c>();
for ( Registration__c reg : 
    [select id, name 
     from Registration__c 
     where name =: s or speical_number__c =: s 
     limit 1
    ] ) {
   regs.add(reg);
}

speical_number__c 是 external id 的好候选人吗?我问是因为将该字段设置为外部 id 将保证 Salesforce.com 将为您的字段创建索引。如果没有索引,查询将执行全表扫描。

于 2013-02-13T00:54:51.250 回答