Improving query selectivity
When we run a SOQL query in Salesforce, the Force.com
query optimizer processes the query and uses a set of statistics calculated in an ongoing fashion to determine the most efficient way to run the query against the underlying database. In order to achieve the best performance possible, we want to make our query as selective as possible to reduce the number of records returned. This has a number of beneficial effects on our performance. Firstly, the query will run faster than it would do if not selective, which will reduce the running time perceived by our end user. Secondly, it will reduce the amount of data returned for us to process or display, thereby improving the performance of the rest of our code. Thirdly, it will mean we have retrieved a smaller number of rows contributing to the query rows governor limit.
So, what makes a query selective? The first thing that indicates selectivity is whether the field is indexed. The following types of field...