Sunday, April 29, 2018

"System.QueryException: Non-selective query against large object type" is thrown.

If you're running a trigger on objects that have more than 200,000 records, this error may occur, "System.QueryException: Non-selective query against large object type."


How to resolve:

  •    Make sure the SOQL is selective by using good filters like 'IN' with a set/list of records like where Id in :optyIdSet, that ensures that query will not return all the records exists on  object.
  •    Right operand of where clause should not be null or blank, for example: where Name in :setOfNames; and setOfNames must be initialized or should not be blank.
  •     Before putting the query in apex, make sure SOQL is not breaking governer limit. One may check governer limit of SOQL by using a simple debug in apex System.debug('LimitOfSOQLRows:'+Limits.getLimitQueryRows());
  •    Avoid using OR conditions in SOQL on objects having more than 200,000 records. Prefer using AND condition, but if need to use it you may use that in loop using If conditions.
  •    Put your query in Query plan tool of Salesforce Developer console to ensure that it is  selective and ready to be used in apex.
  •    Avoid using negative operator such as NOT EQUAL TO ( !=), NOT CONTAINS, and NOT STARTS WITH in SOQL to get records.
  •    Do not use a formula field for filtering in SOQL.
  •    Making a field indexed also helps resolving this error, but first try all above steps to see if it  resolves, else you may make a field indexed. By default, Lookup, Master Detail and external     id fields are indexed, For custom indexing, kindly contact Salesforce.com support.


No comments:

Post a Comment

‘With Sharing ‘/ ‘Without Sharing’ In Salesforce.

Salesforce keywords just make wonders and ‘With Sharing ‘/ ‘Without Sharing’ keyword is one of them and sometimes we are oblivious abo...