My question is in addition to the issue described here: System.LimitException: Too many query rows: 50001

What I don't understand is that my query only returns 1 row so how is it possible that I'm hitting a limit?

SELECT Id, CampaignId, ContactId, LeadId, F2F_Activity_ID__c, F2F_Activity_Subject__c, F2F_Activity_Date__c FROM CampaignMember WHERE ContactId = '003d000001wfzTe'

There are 350,000+ CampaignMember items but my WHERE statement returns only 1 and is on ContactId (lookup field) which I believe is indexed. I have tried putting SOQL in a loop as suggested here as well and I still get System.LimitException: Too many query rows: 50001

All post on this topic mention setting a limit below 50k which I have tried and it works considering I will likely never have a single contact Id that has more than 10k campaign members. But the question is WHY does LIMIT fix this problem? The SOQL is still going to search all 350k+ CampaignMembers.

Note: Developer Console returns 1 row, but execute anonymous returns 50k+ (same SOQL)

  • 2
    This seems like it could be a visibility issue. Perhaps there are 50k+ but only 1 that you have access to.
    – Adrian Larson
    Commented Jan 27, 2015 at 0:57
  • I have full access and this query is looking by 1 Contact Id, (003d000001wfzTe). If I remove the WHERE then yes 350k+ get returned. It would seem more likely that the ContactId field is not indexed but the documentation makes it appear it is so unless anyone else has insight about this I don't think that's the case Commented Jan 27, 2015 at 1:00
  • for grins, did you look at the query explain in Developer Console?
    – cropredy
    Commented Jan 27, 2015 at 2:27
  • 2
    The question you should answer is where this query is? Is it just this query(execute anonymous ) or is it part of transaction? Commented Jan 27, 2015 at 7:11
  • So this SOQL happens in a afterInsert/afterUpdate trigger handler. But my result from the developer console is a anonymous SOQL (in the query editor). I'm not sure I understand what you mean by query explain in DC. Commented Jan 27, 2015 at 17:57

1 Answer 1


question is asked a while back...

System.LimitException: Too many query rows: 50001 , may need not be caused by single SOQL statement. In DC, you have executed a single SOQL Statement, but During the trasaction, the query result from multiple SOQL results can add up to more than 50000 records, resulting in above said error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .