Optimizing DEFINE Fields

The data adapter can translate certain DEFINE expressions to SQL as part of aggregation or record selection operations only. DEFINE expressions that are not translated for the RDBMS are listed in SQL Limitations on Optimization of DEFINE Expressions.

In this section:

Optimizing DEFINE Fields Referenced in FOCUS BY Clauses (DB2, Teradata, Oracle)

IF-THEN-ELSE Optimization

Valued Expressions

SQL Limitations on Optimization of DEFINE Expressions


Top of page

Optimizing DEFINE Fields Referenced in FOCUS BY Clauses (DB2, Teradata, Oracle)

The data adapter can optimize aggregation requests in which a DEFINE field is the object of a FOCUS BY clause. The display commands in these requests must be aggregation commands (SUM, COUNT, or WRITE) or prefix operators such as MIN., MAX., and AVE.

Example:

Passing Aggregation on DEFINE Fields to the RDBMS for Processing


Top of page

Example: Passing Aggregation on DEFINE Fields to the RDBMS for Processing

This example shows the SQL passed following aggregation on a DEFINE field that is the object of a BY clause:

DEFINE FILE DEDUCT
TAX = 0.085 * DED_AMT
END

TABLE FILE DEDUCT
SUM DED_AMT TAX
BY TAX NOPRINT
END

Top of page

IF-THEN-ELSE Optimization

The data adapter can optimize TABLE requests that include DEFINE fields created using IF- THEN-ELSE syntax. In certain cases, such DEFINE fields can be passed to the RDBMS as expressions, enhancing performance and minimizing the size of the answer set returned to FOCUS.

How to:

Enable IF-THEN-ELSE Optimization

Example:

Using IF-THEN-ELSE Optimization Without Aggregation

Using IF-THEN-ELSE Optimization With Aggregation

Using IF-THEN-ELSE Optimization With a Condition That Is Always False


Top of page

Syntax: How to Enable IF-THEN-ELSE Optimization

When you issue the data adapter SET OPTIFTHENELSE command, the data adapter attempts to deliver the construct of a FOCUS IF-THEN-ELSE DEFINE field to the RDBMS as an expression. The DEFINE field must be an object of a selection test or an aggregation request. The DEFINE definition may be specified in the TABLE request or in the Master File.

SQL sqlengine SET OPTIFTHENELSE {ON|OFF}

where:

sqlengine
Indicates the target RDBMS. Valid values are DB2, SQLDS, SQLDBC, SQLIDMS, or SQLORA. Omit if you previously issued the SET SQLENGINE command.

ON
Enables IF-THEN-ELSE optimization.

OFF
Disables IF-THEN-ELSE optimization. OFF is the default.

There is no guarantee that the SQL that is generated will improve performance for all requests. If you find that this feature does not improve performance, set OPTIFTHENELSE OFF to disable the feature.

IF-THEN-ELSE optimization applies to SELECT statements created as a result of FOCUS TABLE requests and is subject to the limitations described in SQL Limitations on Optimization of DEFINE Expressions.


Top of page

Example: Using IF-THEN-ELSE Optimization Without Aggregation

Consider the following request:

SQL DB2 SET OPTIFTHENELSE ON
DEFINE FILE EMPINFO
DEF1 = IF (LAST_NAME EQ ' ') AND (FIRST_NAME EQ ' ')
AND (DEPARTMENT EQ 'MIS') THEN 1 ELSE 0;
END

TABLE FILE EMPINFO
PRINT DEPARTMENT LAST_NAME FIRST_NAME
WHERE DEF1 EQ 1
END

The data adapter generates an SQL request that incorporates the IF-THEN-ELSE condition corresponding to the WHERE DEF1 EQ 1 test:

    SELECT T1."LN",T1."FN",T1."DPT" FROM USER1."EMPINFO" T1 WHERE
(((((T1."LN" = ' ') AND (T1."FN" = ' ')) AND (T1."DPT" =
'MIS')))) FOR FETCH ONLY;

Top of page

Example: Using IF-THEN-ELSE Optimization With Aggregation

SQL DB2 SET OPTIFTHENELSE ON
DEFINE FILE EMPINFO
DEF2 = IF LAST_NAME EQ 'SMITH' THEN 1 ELSE IF LAST_NAME EQ 'JONES' THEN 2
ELSE IF LAST_NAME EQ 'CARTER' THEN 3 ELSE 0;
END

TABLE FILE EMPINFO
SUM MAX.LAST_NAME IF DEF2 EQ 1
END

The data adapter generates an SQL request that incorporates the IF-THEN-ELSE condition corresponding to the WHERE DEF2 EQ 1 test:

    SELECT MAX(T1."LN") FROM USER1."EMPINFO" T1 WHERE (((T1."LN"
= 'SMITH'))) FOR FETCH ONLY;
TABLE FILE EMPINFO
SUM MAX.LAST_NAME IF DEF2 EQ 2
END

The data adapter generates an SQL request that incorporates the IF-THEN-ELSE condition corresponding to the WHERE DEF2 EQ 2 test:

    SELECT MAX(T1."LN") FROM USER1."EMPINFO" T1 WHERE (((NOT
(T1."LN" = 'SMITH')) AND (T1."LN" = 'JONES'))) FOR FETCH ONLY;

Top of page

Example: Using IF-THEN-ELSE Optimization With a Condition That Is Always False

SQL DB2 SET OPTIFTHENELSE ON
DEFINE FILE EMPINFO
DEF3 = IF FIRST_NAME EQ 'RITA' THEN 1 ELSE 0;
END

TABLE FILE EMPINFO
PRINT FIRST_NAME
IF DEF3 EQ 2
END

Because DEF3 EQ 2 will never be true, the data adapter passes the WHERE test 1=0 (which is always false) to the RDBMS, returning zero records from the RDBMS:

    SELECT T1."FN" FROM USER1."EMPINFO" T1 WHERE (1 = 0) FOR
FETCH ONLY;

Top of page

Valued Expressions

There are two types of valued expressions: arithmetic and character string.

How to:

Logical Expressions

Reference:

Arithmetic Expressions

Character String Expressions


Top of page

Reference: Arithmetic Expressions

Arithmetic expressions return a single number. DEFINE fields are classified as arithmetic if the expression on the right includes one or more of the following elements:

For example, the arithmetic expression in the following DEFINE uses a numeric real-field operand (CURR_SAL), a previously-specified DEFINE field (OTIME_SAL), and two numeric constants (1.1 and 100):

NEW_SAL/D12.2= ((CURR_SAL + OTIME_SAL) * 1.1) - 100;

Top of page

Reference: Character String Expressions

Character string expressions return a character string. DEFINE fields are classified as character strings if the expression on the right includes one or more of the following elements:

For example, the character string expression in the following DEFINE uses two alphanumeric field operands (LAST_NAME and FIRST_NAME), a string constant (','), and string concatenation operators (|):

FORMAL_NAME/A36= LAST_NAME | ',' | FIRST_NAME;

Top of page

Syntax: Logical Expressions

Logical expressions return one of two values: True (1) or False (0). DEFINE fields are classified as logical if the expression on the right includes any of the following elements:

For example, the logical expression in the following DEFINE is composed of two expressions connected by the logical operator OR; each part is itself a logical expression:

SALES_FLAG/I1= (DIV_CODE EQ 'SALES') OR (COMMISSION GT 0);

In the next example, the DEFINE field, QUOTA_CLUB, is the value of a logical expression composed of two other expressions connected by the logical operator AND. Note that the first expression is the previously-specified DEFINE field, SALES_FLAG:

QUOTA_CLUB/I1= (SALES_FLAG) AND (UNITS_SOLD GT 100);

Top of page

SQL Limitations on Optimization of DEFINE Expressions

Since the FOCUS reporting language is more extensive than native SQL, the data adapter cannot pass certain DEFINE expressions to the RDBMS for processing. The data adapter does not offload DEFINE-based aggregation and record selection if the DEFINE includes:

Note:

Note:

In addition, IF-THEN-ELSE optimization does not support the following features:


Information Builders