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, determining 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 if it were not selective, which will reduce the running time perceived by our end user. Secondly, it will reduce the amount of data returned to 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 row governor limit.
So, what makes a query selective? The first thing that indicates selectivity is whether a field is indexed. The following types of fields...