Contents Index Recommended page sizes Predicate analysis

ASA SQL User's Guide
  Query Optimization and Execution

Semantic query transformations


To operate efficiently, Adaptive Server Anywhere usually rewrites your query, possibly in several steps, into a new form. It ensures that the new version computes the same result, even though it expresses the query in a new way. In other words, Adaptive Server Anywhere rewrites your queries into semantically equivalent, but syntactically different, forms.

Adaptive Server Anywhere can perform a number of different rewrite operations. If you read the access plans, you will frequently find that they do not correspond to a literal interpretation of your original statement. For example, the optimizer tries as much as possible to rewrite subqueries with joins. The fact that the optimizer has the freedom to rewrite your SQL statements and some of the ways in which it does so, are of importance to you.

Example 

Unlike the SQL language definition, some languages mandate strict behavior for AND and OR operations. Some guarantee that the left-hand condition will be evaluated first. If the truth of the entire condition can then be determined, the compiler guarantees that the right-hand condition will not be evaluated.

This arrangement lets you combine conditions that would otherwise require two nested IF statements into one. For example, in C you can test whether a pointer is NULL before you use it as follows. You can replace the nested conditions

if ( X != NULL ) {
   if ( X->var != 0 ) {
      ... statements ...
   }
}

with the more compact expression

if ( X != NULL && X->var != 0 ) {
      ... statements ...
}

Unlike C, SQL has no such rules concerning execution order. Adaptive Server Anywhere is free to rearrange the order of such conditions as it sees fit. The reordered form is semantically equivalent because the SQL language specification makes no distinction. In particular, query optimizers are completely free to reorder predicates in a WHERE, HAVING, and ON clause.


Predicate analysis
Types of semantic transformations

Contents Index Recommended page sizes Predicate analysis