Database Subqueries

A CCL query statement can incorporate data directly from an external database by means of a database subquery, which is nested in the CCL query as part of the FROM clause.

When the CCL query is running, the database subquery is passed to an external database server, such as Oracle. The external database executes the subquery, selects the appropriate data, and passes the data back to the CCL query. Once the Oracle data is returned to Sybase CEP and incorporated into the output data published by the query to its output stream, it can be processed by other CCL queries in the same manner as all other Sybase CEP data.

A database subquery is always joined to exactly one Sybase CEP data stream (not to a window). Database subqueries are quite different from regular subqueries (which are simply CCL queries nested within other CCL statements). Database subqueries use special syntax and follow very restricted rules.

A database subquery is continuous and conceptually executes every time its associated Sybase CEP query executes. However, as database information is often relatively static and may not require repetitious execution, performance can be improved by reducing the frequency of database subquery execution by the use of caching. For a discussion of caching external database data, please see the Sybase CEP Installation Guide.

Let's assume that there is an external SQL database called StockDatabase which has a table of historical stock prices called StockHistory with the following schema:

Column

Datatype

Description

symbol

String

The symbol of the stock.

avgprice

Float

The average historical price of the stock.

Note:

The type of SQL database is not important to this discussion because the SQL query involved will be common to all normal databases such as Oracle, MySQL, and so on. Whatever database is available at your installation can be accessed with a database subquery, as long as it has the necessary interface driver.

Here is an example of a simple database subquery using the StockTrades stream to pick up historical data from the StockHistory table:

INSERT INTO StockWithHistory 
SELECT current.symbol, current.price, history.avgprice 
FROM StockTrades AS current,
(DATABASE 'StockDatabase' SCHEMA (avgprice FLOAT)
[[SELECT avgprice FROM StockHistory
 WHERE symbol = ?current.symbol]]) AS history;

Notice the following things about a CCL query that contains a database subquery:



Created March 8, 2010. Send feedback on this help topic to Sybase Technical Publications: pubs@sybase.com