Databases, tables, and indexes
The Jet database engine automatically optimizes a query that joins a Microsoft Access table on your hard drive and an ODBC server table if the Access table is small and the joined fields are indexed. In this case, Access improves performance by requesting only the necessary records from the server. Make sure tables you join from different sources are indexed on the join fields.
Fields, calculations, and criteria
Q1: SELECT IIF([MyColumn]="Yes","Order Confirmed","Order Not Confirmed") AS X FROM MyTable;
Q2: SELECT * FROM Q1 WHERE X="Order Confirmed";
Because the IIf expression in Q1 cannot be optimized, Q2 also cannot be optimized. If an expression that can't be optimized is nested within a subquery, the entire query cannot be optimized.
An alternative way to construct the query is as follows:
Q1: SELECT * FROM MyTable WHERE MyColumn = "Yes";
Construct the query as follows:
PARAMETERS [To see confirmed orders, enter Yes. To see unconfirmed
orders, enter No.] Text;
SELECT *
FROM MyTable
WHERE MyColumn = [To see confirmed orders, enter Yes. To see unconfirmed orders,
enter No.];
In the calculated control on the form or report, enter:
=IIF([MyColumn]="Yes","Order Confirmed","Order Not Confirmed")
For greater speed, use Group By on as few fields as possible. As an alternative, use the First function where appropriate.
If a totals query includes a join, consider grouping the records in one query and adding this query to a separate query that will perform the join. This improves performance in some queries.
Expressions and Rushmore optimization
Overview of Rushmore optimization
Microsoft Access can optimize simple expressions or complex expressions in the Criteria row of the query design grid, or in a WHERE clause in an SQL SELECT statement. For certain types of complex expressions, Access can use Rushmore, a data access technology used by the Microsoft Jet database engine, to achieve a greater level of optimization. In addition, the Count function is highly optimized for queries using Rushmore.
Rushmore queries will work with Access tables, as well as with Microsoft FoxPro and dBASE tables (.dbf files). You can't use Rushmore with ODBC data sources, since Access sends these queries to the ODBC data source instead of processing them locally.
Simple optimizable expressions
A simple optimizable expression can form an entire expression or can appear as part of an expression. A simple optimizable expression takes one of the following forms:
IndexedField ComparisonOperator Expression
[Order Date] = #09/15/96#
or
Expression ComparisonOperator IndexedField
#11/1/96# < [HireDate]
In a simple optimizable expression:
IndexedField can either be a field that has its own index or a field that's the first field in a multiple-field index.
ComparisonOperator must be one of the following: <, >, =, <=, >=, <>, Between…And, Like, In.
Expression can be any valid expression, including constants, functions, and fields from other tables.
If you have created indexes for the LastName, Age, and HireDate fields in the Employees table, the following are simple optimizable expressions:
[LastName] = "Smith"
[Age] >= 21
#12/30/90# < [HireDate]
Employees.[LastName] = Customers.[LastName]
[LastName] In ("Smith", "Johnson", "Jones")
[Age] Between 18 And 65
Complex expressions
A complex expression is created by combining two simple expressions with the And or the Or operator. A complex expression takes one of the following forms:
SimpleExpression And SimpleExpression
or
SimpleExpression Or SimpleExpression
A complex expression is fully or partially optimizable depending on whether one or both simple expressions are optimizable, and which operator you used to combine them. A complex expression is Rushmore-optimizable if all three of the following are true:
The expression uses And or Or to join two criteria.
Both criteria are made up of simple optimizable expressions.
Both expressions contain indexed fields. The fields can be indexed individually or they can be part of a multiple-field index.
Note You can optimize multiple-field indexes if you query the indexed fields in the order they appear in the Indexes window, beginning with the first indexed field and continuing with adjacent fields (up to and including 10 fields). For example, if you have a multiple-field index that includes the LastName and FirstName fields, you can optimize a query on LastName or on LastName and FirstName, but you can't optimize a query on FirstName.
The following different combinations of simple expressions in query criteria indicate the level of query optimization.
Fully optimizable query result (using Rushmore)
(optimizable expression) And (optimizable expression)
(optimizable expression) Or (optimizable expression)
Partially optimizable query result (doesn't use Rushmore)
(optimizable expression) And (not optimizable expression)
Not optimizable query result
(not optimizable expression) And (not optimizable expression)
(not optimizable expression) Or (not optimizable expression)
Not (optimizable expression)
Not (not optimizable expression)
You can also use parentheses to group combinations of simple expressions. The preceding rules also apply to combinations of expressions grouped with parentheses.
Once you've combined simple optimizable expressions into complex expressions, these complex expressions can, in turn, be combined to form even more complex expressions that might be optimizable according to the preceding rules.
These examples assume that you created indexes for the LastName and HireDate fields (simple expressions using them are optimizable) but not the MiddleInitial or FirstName fields (simple expressions using them are not optimizable). The following examples of combined simple expressions show the extent to which the result is optimized.
Fully optimizable query result (using Rushmore)
[LastName] = "Smith" And [Hire Date] < #12/30/90#
[LastName] = "Smith" Or [Hire Date] < #12/30/90#
Partially optimizable query result (doesn't use Rushmore)
[LastName] = "Smith" And [MiddleInitial] = "C"
[FirstName] = "Terry" And [MiddleInitial] = "C"
Not optimizable query result
[LastName] = "Smith" Or [MiddleInitial] = "C"
[FirstName] = "Terry" Or [MiddleInitial]= "C"
General suggestions