User's Guide
PART 4. Database Administration and Advanced Use
CHAPTER 28. Accessing Remote Data
This section describes the underlying operations on remote servers performed by Adaptive Server Anywhere on behalf of client applications.
When a statement is received from a client, it is parsed. An error is raised if the statement is not a valid Adaptive Server Anywhere SQL statement.
The next step is called query normalization. During this step, referenced objects are verified and some data type compatibility is checked.
For example, consider the following query:
SELECT * FROM t1 WHERE c1 = 10
The query normalization stage verifies that table t1 with a column c1 exists in the system tables. It also verifies that the data type of column c1 is compatible with the value 10. If the column's data type is datetime, for example, this statement is rejected.
Query preprocessing prepares the query for optimization. It may change the representation of a statement so that the SQL statement Adaptive Server Anywhere generates for passing to a remote server will be syntactically different from the original statement.
Preprocessing performs view expansion so that a query can operate on tables referenced by the view. Expressions may be reordered and subqueries may be transformed to improve processing efficiency. For example, some subqueries may be converted into joins.
The previous steps are performed on all queries, both local and remote.
The following steps depend on the type of SQL statement and the capabilities of the remote servers involved.
Each remote server defined to Adaptive Server Anywhere has a set of capabilities associated with it. These capabilities are stored in the syscapabilities system table. These capabilities are initialized during the first connection to a remote server. The generic server class odbc relies strictly on information returned from the ODBC driver to determine these capabilities. Other server classes such as db2odbc have more detailed knowledge of the capabilities of a remote server type and use that knowledge to supplement what is returned from the driver.
Once syscapabilities is initialized for a server, the capability information is retrieved only from the system table. This allows a user to alter the known capabilities of a server.
Since a remote server may not support all of the features of a given SQL statement, Adaptive Server Anywhere must break the statement into simpler components to the point that the query can be given to the remote server. SQL features not passed off to a remote server must be evaluated by Adaptive Server Anywhere itself.
For example, a query may contain an ORDER BY statement. If a remote server cannot perform ORDER BY, the statement is sent to a the remote server without it and Adaptive Server Anywhere performs the ORDER BY on the result returned, before returning the result to the user. The result is that the user can employ the full range of Adaptive Server Anywhere supported SQL without concern for the features of a particular back end.
The most efficient way to handle a statement is usually to hand as much of the original statement as possible off to the remote server involved. Adaptive Server Anywhere will attempt to pass off as much of the statement as is possible. In many cases this will be the complete statement as originally given to Adaptive Server Anywhere.
Adaptive Server Anywhere will hand off the complete statement when:
Every table in the statement resides in the same remote server.
The remote server is capable of processing all of the syntax in the statement.
In rare conditions, it may actually be more efficient to let Adaptive Server Anywhere do some of the work instead of passing it off. For example, Adaptive Server Anywhere may have a better sorting algorithm. In this case you may consider altering the capabilities of a remote server using the ALTER SERVER statement.
For more information see ALTER SERVER statement.
If a statement contains references to multiple servers, or uses SQL features not supported by a remote server, the query is decomposed into simpler parts.
SELECT statements are broken down by removing portions that cannot be passed on and letting Adaptive Server Anywhere perform the feature. For example, let's say a remote server can not process the atan2() function in the following statement:
select a,b,c where atan2(b,10) > 3 and c = 10
The statement sent to the remote server would be converted to:
select a,b,c where c = 10
Locally, Adaptive Server Anywhere would apply "where atan2(b,10) > 3" to the intermediate result set.
Adaptive Server Anywhere processes joins using a nested loop algorithm. When two tables are joined, one table is selected to be the outer table. The outer table is scanned based on the WHERE conditions that apply to it. For every qualifying row found, the other table, known as the inner table is scanned to find a row that matches the join condition.
This same algorithm is used when remote tables are referenced. Since the cost of searching a remote table is usually much higher than a local table (due to network I/O), every effort is made to make the remote table the outermost table in the join.
If Adaptive Server Anywhere cannot pass off an UPDATE or DELETE statement entirely to a remote server, it must change the statement into a table scan containing as much of the original WHERE clause as possible, followed by positioned UPDATE or DELETE "where current of cursor" when a qualifying row is found.
For example, when the function atan2 is not supported by a remote server:
UPDATE t1 SET a = atan2(b, 10) WHERE b > 5
Would be converted to the following:
SELECT a,b FROM t1 WHERE b > 5
Each time a row is found, Adaptive Server Anywhere would calculate the new value of a and issue:
UPDATE t1 SET a = 'new value' WHERE CURRENT OF CURSOR
If a already has a value that equals the "new value", a positioned UPDATE would not be necessary and would not be sent remotely.
In order to process an UPDATE or DELETE that requires a table scan, the remote data source must support the ability to perform a positioned UPDATE or DELETE ("where current of cursor"). Some data sources do not support this capability.
Temporary tables cannot be updated |