Querying on approx 25 million contacts using the following query in database.getQueryLocator
. Used Query Resource Feedback (Link) to get some feedback about query and here are the results.
Query - 1:
SELECT id, accountId, AccountRefId__c FROM Contact where accountId = null
Cardinality: 66 sObject Cardinality: 18 million Relative Cost: 0.000
My actual query is to find contacts with either accountrefid__c or accountid = null. Query -2:
SELECT id, accountId, AccountRefId__c FROM Contact where accountId = null or accountrefId__c = null
Cardinality: 18 million sObject Cardinality: 18 million Relative Cost: 5.381
To reduce the relative cost I made accountrefid__c field as externalId and unique. Also added some filters and limit as well as below: (Query -3)
SELECT id, accountId, AccountRefId__c FROM Contact where (accountId = null or accountrefId__c = null) order by lastmodifieddate desc limit 100
but the relative cost and cardinality staying the same (even though I added limit ) and when I am running this query in my batch class getting query_timeout exception.
Thanks.