Hitachi

Hitachi Advanced Database Application Development Guide


5.11.3 Equivalent exchange for OR conditions (equivalent exchange to derived tables for which the UNION ALL set operation is specified)

A specified SQL statement might be subject to equivalent exchange if the SQL statement satisfies the following two conditions:

The search conditions specified in the form of an OR condition are converted by equivalent exchange to derived tables for which the UNION ALL set operation is specified. The following explains this equivalent exchange in detail by using examples. In the examples, T1 and T2 are table names, and C1 is a column name.

■ SQL statement before equivalent exchange (specified SQL statement)

[Figure]

In the preceding SQL statement, a comma join is specified in the FROM clause and an OR condition is specified in the WHERE clause (see the underscored portions). Therefore, the SQL statement is subject to equivalent exchange.

■ SQL statement after equivalent exchange

[Figure]

As shown in the preceding example, the SQL statement is converted by equivalent exchange to derived tables for which the UNION ALL set operation is specified. In the SQL statement before equivalent exchange, search conditions were specified in the form of an OR condition (see 1 in the SQL statement before equivalent exchange). Now, in the SQL statement after equivalent exchange, these search conditions are specified as the search conditions of the query specifications in the set operation operands (see 2 and 3 in the SQL statement after equivalent exchange).

■ Advantages of equivalent exchange

For the SQL statement before equivalent exchange, because the WHERE clause contains an OR condition, table joining takes place before search conditions are evaluated.

For the SQL statement after equivalent exchange, however, because the WHERE clauses in the set operation operands contain no OR condition, table joining takes place after search conditions are evaluated. This might be able to reduce the number of input rows that are required for table joining (that is, the search performance might be improved).

If indexes are defined for columns "T1"."C1" and "T2"."C1", any of the indexes are used during a search. The indexes that will be used for a search are determined from the search conditions after equivalent exchange.

Note that, in some cases, the search time might become longer because query specifications and search conditions increase due to equivalent exchange.

Organization of this subsection

(1) Example of equivalent exchange

The following shows examples of equivalent exchange. In the examples, T1, T2, and T3 are table names, and C1 is a column name.

Example 1: Case where two OR conditions are specified in the search conditions

[Figure]

Explanation:

  • In the SQL statement before equivalent exchange, a joined table is specified in the FROM clause and OR conditions are specified in the WHERE clause (see the underscored portion in the preceding example). Therefore, the SQL statement is subject to equivalent exchange.

  • The search conditions that were specified with OR conditions in the SQL statement before equivalent exchange are, after equivalent exchange, specified as search conditions of query specifications in separate set operation operands. If two OR conditions are specified as shown in the SQL statement before equivalent exchange, three set operation operands are generated after equivalent exchange.

Example 2: Case where AND and OR conditions are specified in the search conditions

[Figure]

Explanation:

  • In the SQL statement before equivalent exchange, a comma join is specified in the FROM clause and an OR condition is specified in the WHERE clause (see the underscored portions). Therefore, the SQL statement is subject to equivalent exchange.

  • The search conditions that were specified with OR conditions in the SQL statement before equivalent exchange are, after equivalent exchange, specified as search conditions of query specifications in separate set operation operands. The search condition that was specified on the left side of the AND condition in the SQL statement before equivalent exchange are, after equivalent exchange, specified as search conditions of query specifications in all set operation operands.

(2) Conditions for equivalent exchange to take place

For equivalent exchange to take place, all of the following conditions must be met.

■ Conditions for the server definition or client definition

All of the following conditions must be met:

  • 0 is not specified for the adb_sql_exe_hashtbl_area_size operand in the server definition or client definition.

  • 0 is not specified for the adb_sys_uthd_num operand in the server definition.

  • 0 is not specified for the adb_sql_exe_max_rthd_num operand in the server definition or client definition.

■ Conditions for SQL statements

All of the following conditions must be met:

  • The SQL statement is a retrieval SQL statement.

  • An HADB server whose version is 04-03 or later has collected cost information from any of tables specified in the SQL statement.

  • The SQL statement does not have a query specification that includes an external reference column.

■ Conditions for query specifications

All of the following conditions must be met:

  1. Conditions for selection expressions

    • ROW is not specified in the selection expression.

  2. Conditions for a table reference specified in the FROM clause

    • The number of tables specified in the selection expression does not exceed 16.

    • Comma joins are specified in the FROM clause.

      Note that this condition is met if only comma joins are specified or if both comma joins and joined tables are specified.

      The following shows the types of tables that can be specified. This condition is met even if the specifications of the following types of tables co-exist:

      • Base table (Note that equivalent exchange does not take place if an archivable multi-chunk table is specified.)

      • Joined table (Note that equivalent exchange does not take place if FULL OUTER JOIN is specified.)

      • Derived table (A table subquery, query name, or viewed table applies.)

■ Conditions for join conditions

All of the following conditions must be met:

  • No OR condition is specified in the join specifications of joined tables.

  • The join conditions of tables to which the columns specified in OR conditions belong satisfy the following condition:

    • At least one join condition in the column-specification=column-specification format exists between the tables to which the columns specified in OR conditions.

  • When HADB performs equivalent exchange to derived tables for which the UNION ALL set operation is specified, a join condition for all join-target tables must exist in the search condition of the query specification in each set operation operand.

■ Conditions for the search conditions specified in the WHERE clause

All of the following conditions must be met:

  1. Two or more conditions are not specified in OR conditions in the conditions specified by using the AND logical operator.

    Examples:

    • Example of a search condition that satisfies the conditions for equivalent exchange to take place

      condition-1 AND condition-2 AND (condition-3 OR condition-4 OR condition-5)

      The conditions for equivalent exchange to take place are satisfied because there is only one condition that includes OR conditions.

    • Example of a search condition that does not satisfy the conditions for equivalent exchange to take place

      (condition-1 OR condition-2) AND condition-3 AND (condition-4 OR condition-5)

      The conditions for equivalent exchange to take place are not satisfied because there are two conditions that include OR conditions.

    • If multiple OR conditions are nested, the conditions for equivalent exchange to take place are satisfied.

    • The number of tables specified in successive search conditions other than OR conditions is not taken into account (the specifications of these tables do not need to be join specifications).

    • An OR condition with NOT specified does not satisfy the conditions for equivalent exchange to take place.

  2. Columns of joined tables are not specified in OR conditions.

    However, the conditions for equivalent exchange to take place are satisfied if a joined table is converted to a comma join.

  3. No subquery is specified with an OR condition.

  4. Scalar function RANDOM or RANDOM_NORMAL is not specified with an OR condition.

  5. The number of OR logical operators does not exceed 15.

  6. An OR condition includes a search condition for two or more tables.

  7. The search condition in each set operation operand after equivalent exchange includes one or more search conditions in any of the following formats. In addition, logical operator OR or NOT is not specified for those conditions.

    • Comparison predicate

      column-specification comparison-operator {literal|dynamic-parameter|datetime-information-acquisition-function|user-information-acquisition-function}
      • {literal|dynamic-parameter|datetime-information-acquisition-function|user-information-acquisition-function} comparison-operator column-specification
    • BETWEEN predicate

      column-specification BETWEEN {literal|dynamic-parameter|datetime-information-acquisition-function|user-information-acquisition-function}
                 AND {literal|dynamic-parameter|datetime-information-acquisition-function|user-information-acquisition-function}
    • IN predicate

      column-specification IN ({literal|dynamic-parameter|datetime-information-acquisition-function|user-information-acquisition-function}
              [,{literal|dynamic-parameter|datetime-information-acquisition-function|user-information-acquisition-function}]...)
    • LIKE predicate

      column-specification LIKE pattern-character-string [ESCAPE escape-character]
        pattern-character-string ::= {literal|dynamic-parameter|datetime-information-acquisition-function|user-information-acquisition-function}
        escape-character ::= {literal|dynamic-parameter}
    • LIKE_REGEX predicate

      column-specification LIKE_REGEX regular-expression-character-string [FLAG {I|IGNORECASE}]
        regular-expression-character-string ::= literal
    • NULL predicate

      column-specification IS NULL
    Note

    If only literals are specified in a scalar operation, that scalar operation might be treated as a literal. For details about scalar operations equivalent to literals, see the table Conditions under which value expressions are equivalent to literals under Rules in Specification format and rules for value expressions in the manual HADB SQL Reference.

■ Conditions for the locations of query specifications subject to equivalent exchange

If a query specification is included in the following locations, the HADB server checks whether to perform equivalent exchange:

  • The query expression body in a query expression

  • Scalar subquery

  • Table subquery

  • Set operation operands of a set operation

    If derived tables with UNION ALL specified are created when equivalent exchange is performed for an SQL statement, the maximum number of set operations might be exceeded. In such a case, however, equivalent exchange of the SQL statement continues.

  • WITH list element

    Equivalent exchange is not performed for query specifications of recursive members.

  • Query expression in the CREATE VIEW statement