function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
new_bienew_bie 

non-selective query?

Hi

 

what is meant by Selective and Non-selective Query.

 

Can you Explain with small scenario where it can be used.

 

 

Regards.

ForceMantis (Amit Jain)ForceMantis (Amit Jain)

Here is a very good article is salesforce documentation about this.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm

 

In a nutshell you need to write efficent queries that process fewer records more specifically when you run queries to fetch data inside a trigger you should not be traversing more than 100,000 records in a object. The key thing to note here is that it apply on number of records on a table even if only a single record is returned due to all where clauses in your query.

 

 

sfdcfoxsfdcfox
A selective query is one that uses indices to restrict the initial table scans to less than 100,000 rows (as Amit mentioned). A non-selective query is one that does not use indices to restrict the initial table scan and therefore requires a full table scan. Salesforce limits slow queries in order to maximize performance for all users. In general, normal users can't run queries that would be non-selective, so that there are enough resources to go around.
new_bienew_bie
Hi

Can you give examples for Selective and non-selective Queries.

Regards.
sfdcfoxsfdcfox
// Non-Selective, if there are too many rows in database
Account[] a = [select id,name,numberofemployees from account where numberofemployees in (10,100,1000,10000)];

// Selective, because account name is indexed
Account[] a = [select id,name,numberofemployees from account where name > 'k' and name < 'p'];

Please note that a non-selective query won't fail unless there are a large number of records in the database, so it can be non-trivial to determine if a query is non-selective. A general rule of thumb is this: If a SOSL search would return a value using only a FIND clause (no WHERE clauses), the field is probably indexed, while if a SOQL query or WHERE clause is required, it is probably non-indexed. You can also review the Help & Training and developer docs to determine if a field is indexed.