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
chandrasekhar reddychandrasekhar reddy 

Selective threshold question?

Hi,
My Lead table has 1300000 records (<2 million). I am using beloq SOQL query.

select id, company, phone, statecode from Lead where statecode='AB'
Here statecode is custom field and has been indexed.

Above query returns 200000 records. Which is exceeding selective threshold limit. Because for 1300000 records, selective threshold limit is 115000 (10% in first million records+ 5% in rest of the records). But my query returns 200000 records. when the query retutns more than selective threshold, indexes will not be considered and query takes long time to return the result. How can i avoid this issue?

If i use LIMIT as below, still the indexes on statecode will be ignored?

select id, company, phone, statecode from Lead where statecode='AB' LIMIT 100000
Please suggest me. Thanks!


Anoop yadavAnoop yadav
Hi,

I think you should use more than two filter.

Try like this.
select id, company, phone, statecode from Lead where Phone != ''  AND statecode='AB'

chandrasekhar reddychandrasekhar reddy
Hi Anoop, Thanks for your answer.

Even if Phone != '' is applied, query returns same number of records.
Anoop yadavAnoop yadav
Hi reddy,

You can use any other field instead of Phone for selective query.

For more information check the below link.
https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_VLSQ.htm
chandrasekhar reddychandrasekhar reddy
I am not using Phone field. I am using statecode custom field. Becausr we have a custom VF page where user selects statecode and views all the results.
sunny522sunny522
Hi Chandrasekhar,
                          you can use one more filter like  created date < some date      or created by  particular user.
chandrasekhar reddychandrasekhar reddy
Hi Sunny, Thanks for your answer.
If i apply one more filter lke created date < some date, then i get only few records but not all 200000 records. If i apply date filter then i miss records which i dont want. Please suggest me. As am getting 200000 records, will SFDC would ignore indexes while searching ? Thanks! 
sunny522sunny522
Hi Chandrasekhar,
             In this case you need to write batch class.
see the link below.
https://developer.salesforce.com/forums?id=906F000000090EEIAY

A maximum of 50 million records can be returned in the Database.QueryLocator object.so you use Database.QueryLocator  object to query records.

Go through batch apex governor limits once.

2nd solution : you can query records  in multiple steps then do the processing you want.For example first query records till last year, then query records this year.now you can do processing.