Newer Version Available

This content describes an older version of this product. View Latest

Improve Performance by Avoiding Null Values

In your SOQL and SOSL queries, explicitly filtering out null values in the WHERE clause allows Salesforce to improve query performance. In the following example, any records where the Thread__c value is null are eliminated from the search.
1Public class TagWS {
2
3/* getThreadTags
4*
5* a quick method to pull tags not in the existing list
6*
7*/
8   public static webservice List<String> 
9   getThreadTags(String threadId, List<String> tags) {
10
11      system.debug(LoggingLevel.Debug,tags);
12
13      List<String> retVals = new List<String>();
14      Set<String> tagSet = new Set<String>();
15      Set<String> origTagSet = new Set<String>();
16      origTagSet.addAll(tags);
17
18// Note WHERE clause optimizes search where Thread__c is not null
19
20      for(CSO_CaseThread_Tag__c t : 
21         [SELECT Name FROM CSO_CaseThread_Tag__c 
22         WHERE Thread__c = :threadId AND
23         Thread__c != null]) 
24
25      {
26         tagSet.add(t.Name);
27      }
28      for(String x : origTagSet) { 
29   // return a minus version of it so the UI knows to clear it
30         if(!tagSet.contains(x)) retVals.add('-' + x);
31      }
32      for(String x : tagSet) { 
33   // return a plus version so the UI knows it's new
34         if(!origTagSet.contains(x)) retvals.add('+' + x);
35      }
36
37      return retVals;
38   }
39}