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
KitaSanKitaSan 

Options for slow SOQL

I have an embedded VF page that shows if a lead is suspected to be a potential new contact at an existing customer. There are two formula fields on the lead and contact that contain the email domain. I then have a controller extension that takes the lead email domain and looks for matches in the contact table. 
 
public integer getPotentialCustomers() {
        return [SELECT count() FROM contact WHERE email != null AND Contact_Status__c = 'Active' AND email_domain__c = :lead.email_domain__c  LIMIT 1];
    }

However, that has so search over 120k contacts... so its slow. 

I tried JS remoting it but I run into an error with public classes in an iframe.

I have asked SFDC for custom indexing on the formula fields which should help a bit.. but wondering if there is another method to speed this up. Even just a way to make that SOQL query asynchronous so that the rest of the page loads and then the embedded vf updates. 

-Justin 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post how to resolve this issue
1) https://help.salesforce.com/apex/HTViewSolution?urlname=How-to-make-my-SOQL-query-selective&language=en_US

Force.com SOQL Best Practices: Nulls and Formula Fields
1) https://developer.salesforce.com/blogs/engineering/2013/02/force-com-soql-best-practices-nulls-and-formula-fields.html

1. You may find that the query in question needs to be more selective in the WHERE clause. I.e. as per Salesforce standards & best practices - the where clause needs to subset 10% or less of the data.
2. You may need a custom index on the field.
3. A possible quick fix may be to make the field in question an external ID. Since external IDs are indexed automatically, this will create the index and may solve the problem.

To make the field an external ID simply go to Setup | Customize | Object name | Fields, edit field name. and check the box "External ID".

Let us know if this will help you