Does anyone have any background on which and when using the built in getAll() method for custom settings is better than just using traditional soql?
I am aware that getAll retrieves from the cache where-as soql does not, is there some point at which (due to number of rows/size of custom settings) it is quicker to use soql.
Currently I am working with a custom setting that has around 500 rows and these are occasionally refreshed (deleted down and recreated daily). I am having some slow performance, and have noticed that getAll also gets the deleted values, which I then need to filter out.
I’d say it depends on how you are using the
Map<String, CustomSetting__c> that
getAll()returns and how may list records there are.
If you are looping over the map
values() searching for a single record based on a field other than the Name then SOQL would probably serve you better as you could just grab the required records.
One thing to note with the SOQL approach. It will cost you one of your 100 synchronous SOQL queries governor limit. Where as the cached
getAll() is free from that but may not have the absolute latest data.
I assume that the caching is only for the duration of the transaction (TODO: confirm this is actually the case). See the comment from @ca_peterson that the underlying implementation is based on memcached.
If you can access the Map by the Name key then the Map has some good advantages over SOQL if you need to pick out several values. Even better would be pulling out individual CustomSetting__c records by name using
I’m going to go out on a limb here and say that the cache will be
much faster than a SOQL query in getting a raw list of all the possible list values. Of course, it’s hard to say what other caching Salesforce has got going on internally.
I did a quick test with a list custom setting to get a list of all values:
- SOQL query for all fields: 19:22:33.249 to 19:22:33.251 = ~2 ms
- .getAll().values(): 19:22:33.251 to 19:22:33.252 = ~1 ms
There is a slight advantage to the cache here, but that doesn’t take into account how much extra processing you will need to do on the results.
So, in conclusion, it depends on how many of those list custom setting values you want and if you can access them by name. You will need to try both approaches on your data to see which works best while taking into account if you can spare the extra SOQL query.