Return-Path: X-Original-To: apmail-db-derby-commits-archive@www.apache.org Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 94791114C1 for ; Fri, 11 Jul 2014 20:43:51 +0000 (UTC) Received: (qmail 75302 invoked by uid 500); 11 Jul 2014 20:43:51 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 75274 invoked by uid 500); 11 Jul 2014 20:43:51 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 75265 invoked by uid 99); 11 Jul 2014 20:43:51 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Jul 2014 20:43:51 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Jul 2014 20:43:45 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 5FF6E23889EC; Fri, 11 Jul 2014 20:43:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1609827 [1/2] - /db/derby/docs/trunk/src/tuning/ Date: Fri, 11 Jul 2014 20:43:24 -0000 To: derby-commits@db.apache.org From: chaase3@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140711204325.5FF6E23889EC@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: chaase3 Date: Fri Jul 11 20:43:23 2014 New Revision: 1609827 URL: http://svn.apache.org/r1609827 Log: DERBY-6379 Manuals are inconsistent in their use of the element Modified 29 Tuning Guide topics and the map file. Patch: DERBY-6379-tun-trans.diff Modified: db/derby/docs/trunk/src/tuning/ctuntransform11313.dita db/derby/docs/trunk/src/tuning/ctuntransform13699.dita db/derby/docs/trunk/src/tuning/ctuntransform13966.dita db/derby/docs/trunk/src/tuning/ctuntransform14044.dita db/derby/docs/trunk/src/tuning/ctuntransform16033.dita db/derby/docs/trunk/src/tuning/ctuntransform16279.dita db/derby/docs/trunk/src/tuning/ctuntransform21780.dita db/derby/docs/trunk/src/tuning/ctuntransform22576.dita db/derby/docs/trunk/src/tuning/ctuntransform25857.dita db/derby/docs/trunk/src/tuning/ctuntransform25868.dita db/derby/docs/trunk/src/tuning/ctuntransform35783.dita db/derby/docs/trunk/src/tuning/ctuntransform36368.dita db/derby/docs/trunk/src/tuning/ctuntransform36623.dita db/derby/docs/trunk/src/tuning/ctuntransform37032.dita db/derby/docs/trunk/src/tuning/ctuntransform47182.dita db/derby/docs/trunk/src/tuning/ctuntransform55045.dita db/derby/docs/trunk/src/tuning/ctuntransform867165.dita db/derby/docs/trunk/src/tuning/ctuntransform867201.dita db/derby/docs/trunk/src/tuning/rtuntransform139.dita db/derby/docs/trunk/src/tuning/rtuntransform208.dita db/derby/docs/trunk/src/tuning/rtuntransform345.dita db/derby/docs/trunk/src/tuning/rtuntransform443.dita db/derby/docs/trunk/src/tuning/rtuntransform472.dita db/derby/docs/trunk/src/tuning/rtuntransform582.dita db/derby/docs/trunk/src/tuning/rtuntransform590.dita db/derby/docs/trunk/src/tuning/rtuntransform866214.dita db/derby/docs/trunk/src/tuning/rtuntransform866547.dita db/derby/docs/trunk/src/tuning/rtuntransform866587.dita db/derby/docs/trunk/src/tuning/rtuntransform867602.dita db/derby/docs/trunk/src/tuning/tuningderby.ditamap Modified: db/derby/docs/trunk/src/tuning/ctuntransform11313.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform11313.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform11313.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform11313.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,10 @@ limitations under the License. View transformations +When +evaluates a statement that references a view, it transforms the reference to a +view into a derived table. It might make additional transformations to improve +performance. View transformations @@ -26,8 +30,4 @@ limitations under the License. -

When evaluates a statement that references a view, it transforms -the reference to a view into a derived table. It might make additional transformations -to improve performance. -

Modified: db/derby/docs/trunk/src/tuning/ctuntransform13699.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform13699.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform13699.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform13699.dita Fri Jul 11 20:43:23 2014 @@ -18,16 +18,15 @@ limitations under the License. Subquery processing and transformations +Subqueries are notoriously expensive to evaluate. This section +describes some of the transformations that + makes internally to +reduce the cost of evaluating them. -Subqueriesprocessing and transformations -of +Subqueriesprocessing and transformations of -

Subqueries are notoriously expensive to evaluate. This section describes -some of the transformations that makes internally to reduce the -cost of evaluating them. -

Modified: db/derby/docs/trunk/src/tuning/ctuntransform13966.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform13966.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform13966.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform13966.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,9 @@ limitations under the License. Internal language transformations +The SQL +parser sometimes transforms SQL statements internally for performance reasons. +This section describes those transformations. Language transformations for performance @@ -33,46 +36,54 @@ limitations under the License. -

The SQL parser sometimes transforms SQL statements -internally for performance reasons. This appendix describes those transformations. -Understanding the internal language transformations can help you analyze and +

Understanding the internal language transformations can help you analyze and tune performance. Understanding the internal language transformations is not necessary for the general user.

This chapter uses some specialized terms. Here are some definitions:

-
+
base table
-
A real table in a FROM list. In queries that involve "virtual" -tables such as views and derived tables, base tables are the underlying tables -to which virtual tables correspond.
+
A real table in a FROM list. In queries that involve "virtual" tables such +as views and derived tables, base tables are the underlying tables to which +virtual tables correspond.
derived table
A virtual table, such as a subquery given a correlation name or a view. -For example: SELECT derivedtable.c1 FROM (VALUES ('a','b')) -AS derivedtable(c1,c2).
+For example: +SELECT derivedtable.c1 FROM (VALUES ('a','b')) +AS derivedtable(c1,c2)
equality predicate
-
A predicate in which one value is -compared to another value using the = operator.
+
A +predicate +in which one value is compared to another value using the = +operator.
equijoin predicate
A predicate in which one column is compared to a column in another table -using the = operator.
+using the = operator.
optimizable
-
A predicate is optimizable if it provides a -starting or stopping point and allows use of an index. Optimizable predicates -use only simple column references and =, <, >, +, >=, -and IS NULL operators. For complete details, see . A synonym for optimizable is indexable.
+
A predicate is optimizable if it provides a starting or stopping +point and allows use of an index. Optimizable predicates use only +simple column references +and =, <, >, ++, >=, and IS NULL operators. For complete +details, see . A synonym for +optimizable is indexable.
predicate
A WHERE clause contains boolean expressions that can be linked together -by AND or OR clauses. Each part is called a predicate. -For example: WHERE c1 =2 AND c2 = 5 contains two predicates.
+by AND or OR clauses. Each part is called a predicate. For example, the +following clause contains two predicates: +WHERE c1 =2 AND c2 = 5
sargable
-
Sargable predicates are a superset of optimizable -predicates; not all sargable predicates are optimizable, because sargable -predicates also include the <> operator. (Sarg stands -for "search argument.") Predicates that are sargable but not optimizable -nevertheless improve performance and allow the optimizer to use more accurate -costing information. -

In addition, sargable predicates can be pushed down (see ).

+
Sargable predicates are a superset of optimizable predicates; not all +sargable predicates are optimizable, because sargable predicates also include +the <> operator. (Sarg stands for "search +argument.") Predicates that are sargable but not optimizable nevertheless +improve performance and allow the optimizer to use more accurate costing +information. +

In addition, sargable predicates can be pushed down (see +).

simple column reference
-
A reference to a column that is not part of an expression. For example, c1 is a simple column reference, but c1+1, max(c1), and lower(c1) are not.
+
A reference to a column that is not part of an expression. For example, +c1 is a simple column reference, but c1+1, +max(c1), and lower(c1) are not.
Modified: db/derby/docs/trunk/src/tuning/ctuntransform14044.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform14044.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform14044.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform14044.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,9 @@ limitations under the License. Combining ORDER BY and UNION +Without a transformation, a statement that contains both ORDER BY and +UNION would require two separate sorting steps-one to satisfy ORDER BY and one +to satisfy UNION. UNIONavoiding ordering during @@ -25,31 +28,31 @@ limitations under the License. -

Without a transformation, a statement that contains both ORDER BY and UNION -would require two separate sorting steps-one to satisfy ORDER BY and -one to satisfy UNION (Currently uses sorting to eliminate duplicates -from a UNION. You can use UNION ALL to avoid sorting, but UNION ALL will return duplicates. So you only use UNION ALL to avoid sorting if you know that there are no duplicate rows in the tables).

-

In some situations, can transform the statement internally -into one that contains only one of these keywords (the ORDER BY is thrown -out). The requirements are: +

Currently uses +sorting to eliminate duplicates from a UNION. You can use UNION ALL to avoid +sorting, but UNION ALL will return duplicates. So you only use UNION ALL to +avoid sorting if you know that there are no duplicate rows in the tables.

+

In some situations, +can transform the statement internally into one that contains only one of these +keywords (the ORDER BY is thrown out). The requirements are as follows:

  • The columns in the ORDER BY list must be a subset of the columns in the -select list of the left side of the union.
  • -
  • All the columns in the ORDER BY list must be sorted in ascending order +select list of the left side of the union.
  • +
  • All the columns in the ORDER BY list must be sorted in ascending order, and they must be an in-order prefix of the columns in the target list of the -left side of the UNION.
  • -

-

will be able to transform the following statements: +left side of the UNION. + +

will be able to +transform the following statements:

SELECT miles, meal FROM Flights UNION VALUES (1000, 'D') -ORDER BY 1 -

-

cannot avoid two sorting nodes in the following statement, -because of the order of the columns in the ORDER BY clause: +ORDER BY 1 +

cannot avoid two +sorting nodes in the following statement, because of the order of the columns in +the ORDER BY clause:

SELECT flight_id, segment_number FROM Flights UNION SELECT flight_id, segment_number FROM FlightAvailability -ORDER BY segment_number , flight_id -

+ORDER BY segment_number , flight_id
Modified: db/derby/docs/trunk/src/tuning/ctuntransform16033.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform16033.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform16033.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform16033.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,9 @@ limitations under the License. Sort avoidance +Sorting is an expensive process. + tries to eliminate +unnecessary sorting steps where possible. Sort avoidance @@ -26,7 +29,4 @@ limitations under the License. -

Sorting is an expensive process. tries to eliminate unnecessary -sorting steps where possible. -

Modified: db/derby/docs/trunk/src/tuning/ctuntransform16279.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform16279.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform16279.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform16279.dita Fri Jul 11 20:43:23 2014 @@ -19,43 +19,47 @@ limitations under the License. --> DISTINCT elimination based on a uniqueness condition +A DISTINCT (and the corresponding sort) can be eliminated from a +query if a uniqueness condition exists that ensures that no duplicate values +will be returned. DISTINCTeliminated for uniqueness condition -

A DISTINCT (and the corresponding sort) can be eliminated from a query -if a uniqueness condition exists that ensures that no duplicate values will -be returned. If no duplicate values are returned, the DISTINCT node is superfluous, -and transforms the +

If no duplicate values are returned, the DISTINCT node is superfluous, and + transforms the statement internally into one without the DISTINCT keyword.

-

The requirements are:

    -
  • No GROUP BY list.
  • -
  • SELECT list contains at least one simple -column reference.
  • -
  • Every simple -column reference is from the same table.
  • -
  • Every table in the FROM list is a base -table.
  • -
  • Primary table

    There is -at least one unique index on one table in the FROM list for which all the -columns appear in one of the following:

      -
    • equality -predicates with expressions that do not include any column references
    • -
    • simple -column references in the SELECT list
    • +

      The requirements are as follows:

      +
        +
      • No GROUP BY list.
      • +
      • SELECT list contains at least one +simple +column reference.
      • +
      • Every simple column reference is from the same table.
      • +
      • Every table in the FROM list is a +base +table.
      • +
      • Primary table +

        There is at least one unique index on one table in the FROM list for which +all the columns appear in one of the following:

        +
          +
        • Equality +predicates with expressions that do not include any column +references
        • +
        • Simple column references in the SELECT list
      • -
      • Secondary table(s)

        All -the other tables in the FROM list also have at least one unique index for -which all the columns appear in one of the following:

          -
        • equality -predicates with expressions that do not include columns from the same -table
        • -
        • simple -column references in the SELECT list
        • +
        • Secondary table(s) +

          All the other tables in the FROM list also have at least one unique index for +which all the columns appear in one of the following:

          +
            +
          • Equality predicates with expressions that do not include columns from the +same table
          • +
          • Simple column references in the SELECT list
        • -

        -

        For example: CREATE TABLE tab1 (c1 INT NOT NULL, +

      +

      For example:

      +CREATE TABLE tab1 (c1 INT NOT NULL, c2 INT NOT NULL, c3 INT NOT NULL, c4 CHAR(2), @@ -72,28 +76,26 @@ INSERT INTO tab2 VALUES (1, 2), (1, 3), (2, 2), (2, 3) --- all the columns in the index on the only table (tab1) appear --- in the way required for the Primary table (simple column references) +-- all the columns in the index on the only table (tab1) appear +-- in the way required for the Primary table (simple column references) SELECT DISTINCT c1, c2, c3, c4 FROM tab1 --- all the columns in the index on the only table (tab1) appear --- in the way required for the Primary table (equality predicates) +-- all the columns in the index on the only table (tab1) appear +-- in the way required for the Primary table (equality predicates) SELECT DISTINCT c3, c4 FROM tab1 WHERE c1 = 1 AND c2 = 2 AND c4 = 'WA' --- all the columns in the index on tab1 appear +-- all the columns in the index on tab1 appear -- in the way required for the Primary table, -- and all the columns in the -- other tables appear in the way required --- for a Secondary table +-- for a Secondary table SELECT DISTINCT tab1.c1, tab1.c3, tab1.c4 FROM tab1, tab2 WHERE tab1.c2 = 2 AND tab2.c2 = tab1.c2 -AND tab2.c1 = tab1.c1

      +AND tab2.c1 = tab1.c1
      Modified: db/derby/docs/trunk/src/tuning/ctuntransform21780.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform21780.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform21780.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform21780.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,12 @@ limitations under the License. Combining ORDER BY and DISTINCT +Without a transformation, a statement that contains both DISTINCT +and ORDER BY would require two separate sorting steps-one to satisfy DISTINCT +and one to satisfy ORDER BY. (Currently, + uses sorting to +evaluate DISTINCT. There are, in theory, other ways to accomplish +this.) DISTINCTcombined with ORDER BY @@ -25,24 +31,22 @@ limitations under the License. -

      Without a transformation, a statement that contains both DISTINCT and ORDER -BY would require two separate sorting steps-one to satisfy DISTINCT -and one to satisfy ORDER BY. (Currently, uses sorting to evaluate -DISTINCT. There are, in theory, other ways to accomplish this.) In some situations, can -transform the statement internally into one that contains only one of these -keywords. The requirements are: +

      In some situations, +can transform the statement internally into one that contains only one of these +keywords. The requirements are as follows:

      • The columns in the ORDER BY list must be a subset of the columns in the SELECT list.
      • All the columns in the ORDER BY list are sorted in ascending order.
      • -

      +

    A unique index is not required.

    -

    For example: +

    For example,

    SELECT DISTINCT miles, meal FROM Flights -ORDER BY meal

    -

    is transformed into +ORDER BY meal +

    is transformed into

    SELECT DISTINCT miles, meal -FROM FlightsNote that these are not equivalent functions; this -is simply an internal transformation.

    +FROM Flights +

    Note that these are not equivalent functions; this is simply an internal + transformation.

    Modified: db/derby/docs/trunk/src/tuning/ctuntransform22576.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform22576.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform22576.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform22576.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,10 @@ limitations under the License. View flattening +When evaluating a statement that references a view, + internally transforms a +view into a derived table. This derived table might also be a candidate for +flattening into the outer query block. View flattening @@ -25,34 +29,32 @@ limitations under the License. -

    When evaluating a statement that references a view, internally -transforms a view into a derived table. This derived table might also be a -candidate for flattening into the outer query block.

    -

    A view or derived table can be flattened into the outer query block if -all of the following conditions are met: +

    A view or derived table can be flattened into the outer query block if all of +the following conditions are met:

      -
    • The select list is composed entirely of simple column references and constants.
    • -
    • There is no GROUP BY clause in the view.
    • -
    • There is no DISTINCT in the view.
    • +
    • The select list is composed entirely of +simple column references +and constants.
    • +
    • There is no GROUP BY clause in the view.
    • +
    • There is no DISTINCT in the view.
    • There is no ORDER BY, result offset, or fetch first clause in the view.
    • -

    -

    For example, given view v1(a,b): +

+

For example, given view v1(a,b):

SELECT Cities.city_name, Countries.country_iso_code FROM Cities, Countries -WHERE Cities.country_iso_code = Countries.country_iso_code

-

and a SELECT that references it: +WHERE Cities.country_iso_code = Countries.country_iso_code +

and a SELECT that references it:

SELECT a, b -FROM v1 WHERE a = 'Melbourne'

-

after the view is transformed into a derived table, the internal query -is +FROM v1 WHERE a = 'Melbourne' +

After the view is transformed into a derived table, the internal query is

SELECT a, b FROM (select Cities.city_name, Countries.country_iso_code FROM Cities, Countries WHERE Cities.country_iso_code = Countries.country_iso_code) v1(a, b) -WHERE a = 'Melbourne'

-

After view flattening it becomes +WHERE a = 'Melbourne' +

After view flattening it becomes

SELECT Cities.city_name, Countries.country_iso_code FROM Cities, Countries WHERE Cities.country_iso_code = Countries.country_iso_code -AND Cities.city_name = 'Melbourne'

+AND Cities.city_name = 'Melbourne'
Modified: db/derby/docs/trunk/src/tuning/ctuntransform25857.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform25857.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform25857.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform25857.dita Fri Jul 11 20:43:23 2014 @@ -20,45 +20,59 @@ limitations under the License. --> Materialization +Materialization means that a subquery is evaluated only +once. Several types of subqueries can be materialized. subqueriesmaterialization materializationsubqueries -

Materialization means that a subquery is evaluated -only once. Several types of subqueries can be materialized.

-
Expression subqueries that are not correlated

A -subquery can be materialized if it is a noncorrelated expression subquery. -A correlated subquery is one that references columns in the outer query, and -so has to be evaluated for each row in the outer query.

For example: - SELECT * FROM Staff WHERE id = (SELECT MAX(manager) FROM Org)

In -this statement, the subquery needs to be evaluated only once.

This type -of subquery must return only one row. If evaluating the subquery causes a -cardinality violation (if it returns more than one row), an exception is thrown -when the subquery is run.

Subquery materialization is detected before -optimization, which allows the optimizer -to see a materialized subquery as an unknown constant value. The comparison -is therefore optimizable.

The original statement is transformed into -the following two statements: constant = SELECT MAX(manager) FROM Org +

Expression subqueries that are not correlated +

A subquery can be materialized if it is a noncorrelated expression subquery. +A correlated subquery is one that references columns in the outer query, and so +has to be evaluated for each row in the outer query.

+

For example:

+SELECT * FROM Staff +WHERE id = (SELECT MAX(manager) FROM Org) +

In this statement, the subquery needs to be evaluated only once.

+

This type of subquery must return only one row. If evaluating the subquery +causes a cardinality violation (if it returns more than one row), an exception +is thrown when the subquery is run.

+

Subquery materialization is detected before optimization, which allows the + optimizer to see a +materialized subquery as an unknown constant value. The comparison is therefore +optimizable.

+

The original statement is transformed into the following two statements:

+constant = SELECT MAX(manager) FROM Org SELECT * FROM Staff -WHERE id = constant

The second statement is -optimizable.

-
Subqueries that cannot be flattened

Materialization -of a subquery can also occur when the subquery is nonflattenable and there -is an equijoin between the subquery and another FROM table in the query.

For -example: SELECT i, a FROM t1, +WHERE id = constant +

The second statement is optimizable.

+
+
Subqueries that cannot be flattened +

Materialization of a subquery can also occur when the subquery is +nonflattenable and there is an equijoin between the subquery and another FROM +table in the query.

+

For example:

+SELECT i, a FROM t1, (SELECT DISTINCT a FROM T2) x1 -WHERE t1.i = x1.a AND t1.i in (1, 3, 5, 7) In this example, the -subquery x1 is noncorrelated because it does not reference any of the columns -from the outer query. The subquery is nonflattenable because of the DISTINCT -keyword. does not -flatten DISTINCT subqueries. This subquery is eligible for materialization. -Since there is an equijoin predicate between the subquery x1 and the table -t1 (namely, t1.i = x1.a), the optimizer -will consider performing a hash join between t1 and x1 (with x1 as the inner -operand). If that approach yields the best cost, materializes -the subquery x1 to perform the hash join. The subquery is evaluated only a -single time and the results are stored in an in-memory hash table. then -executes the join using the in-memory result set for x1.

+WHERE t1.i = x1.a AND t1.i in (1, 3, 5, 7) +

In this example, the subquery x1 is noncorrelated because it +does not reference any of the columns from the outer query. The subquery is +nonflattenable because of the DISTINCT keyword. + does not flatten +DISTINCT subqueries. This subquery is eligible for materialization. Since there +is an +equijoin +predicate between the subquery x1 and the table +t1 (namely, t1.i = x1.a), the + optimizer will consider +performing a hash join between t1 and x1 (with +x1 as the inner operand). If that approach yields the best +cost, materializes the +subquery x1 to perform the hash join. The subquery is evaluated +only a single time, and the results are stored in an in-memory hash table. + then executes the join +using the in-memory result set for x1.

+
Modified: db/derby/docs/trunk/src/tuning/ctuntransform25868.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform25868.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform25868.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform25868.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,8 @@ limitations under the License. Flattening a subquery into an EXISTS join +An EXISTS join is a join in which the right side of the join needs to +be probed only once for each outer row. Subqueriesflattening of to an EXISTS join @@ -26,47 +28,56 @@ limitations under the License. -

An EXISTS join is a join in which the right side of the join needs to be -probed only once for each outer row. Using such a definition, an EXISTS join -does not literally use the EXISTS keyword. treats a statement -as an EXISTS join when there will be at most one matching row from the right -side of the join for a given row in the outer table.

-

A subquery that cannot be flattened into a normal join because of a uniqueness -condition can be flattened into an EXISTS join if it meets all the requirements -(see below). Recall the first example from the previous section ():

-SELECT c1 FROM t1 WHERE c1 IN (SELECT c1 FROM t2) +

Using such a definition, an EXISTS join does not literally use the EXISTS +keyword. treats a +statement as an EXISTS join when there will be at most one matching row from the +right side of the join for a given row in the outer table.

+

A subquery that cannot be flattened into a normal join because of a +uniqueness condition can be flattened into an EXISTS join if it meets all the +requirements (see below). Recall the first example from +:

+SELECT c1 FROM t1 +WHERE c1 IN (SELECT c1 FROM t2)

This query could not be flattened into a normal join because such a join -would return the wrong results. However, this query can be flattened into -a join recognized internally by the system as an EXISTS join. -When processing an EXISTS join, knows to stop processing the right -side of the join after a single row is returned. The transformed statement -would look something like this: +would return the wrong results. However, this query can be flattened into a join +recognized internally by the + system as an EXISTS +join. When processing an EXISTS join, + knows to stop +processing the right side of the join after a single row is returned. The +transformed statement would look something like this:

SELECT c1 FROM t1, t2 WHERE t1.c1 = t2.c1 -EXISTS JOIN INTERNAL SYNTAX

-

Requirements for flattening into an EXISTS join: +EXISTS JOIN INTERNAL SYNTAX +

Requirements for flattening into an EXISTS join are as follows:

    -
  • The subquery is not under an OR.
  • -
  • The subquery type is EXISTS, IN, or ANY.
  • -
  • The subquery is not in the SELECT list of the outer query block.
  • -
  • There are no aggregates in the SELECT list of the subquery.
  • -
  • The subquery does not have a GROUP BY clause.
  • +
  • The subquery is not under an OR.
  • +
  • The subquery type is EXISTS, IN, or ANY.
  • +
  • The subquery is not in the SELECT list of the outer query block.
  • +
  • There are no aggregates in the SELECT list of the subquery.
  • +
  • The subquery does not have a GROUP BY clause.
  • The subquery does not have an ORDER BY, result offset, or fetch first clause.
  • -
  • The subquery has a single entry in its FROM list that is a base table.
  • +
  • The subquery has a single entry in its FROM list that is a +base +table.
  • None of the predicates in the subquery, including the additional one formed between the left side of the subquery operator and the column in the subquery's SELECT list (for IN or ANY subqueries), include any subqueries, method calls, or field accesses.
  • -

+

When a subquery is flattened into an EXISTS join, the table from the subquery -is made join-order-dependent on all the tables with which it is correlated. -This means that a table must appear inner to all the tables on which it is -join-order-dependent. (In subsequent releases this restrictions can be relaxed.) -For example: +is made join-order-dependent on all the tables with which it is correlated. This +means that a table must appear inner to all the tables on which it is +join-order-dependent. For example,

SELECT t1.* FROM t1, t2 -WHERE EXISTS (SELECT * FROM t3 WHERE t1.c1 = t3.c1)

-

gets flattened into -SELECT t1.* FROM t1, t2, t3 WHERE t1.c1 = t3.c1

-

where t3 is join order dependent on t1. This means that the possible join orders are (t1, t2, t3), (t1, t3, t2), and (t2, t1, t3).

+WHERE EXISTS (SELECT * FROM t3 WHERE t1.c1 = t3.c1) +

gets flattened into

+SELECT t1.* FROM t1, t2, t3 +WHERE t1.c1 = t3.c1 +

where t3 is join order dependent on t1. +This means that the possible join orders are (t1, +t2, t3), (t1, +t3, t2), and (t2, +t1, t3).

Modified: db/derby/docs/trunk/src/tuning/ctuntransform35783.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform35783.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform35783.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform35783.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,9 @@ limitations under the License. Predicate transformations +WHERE clauses with predicates joined by OR are usually not +optimizable. WHERE clauses with predicates joined by AND are optimizable if +at least one of the predicates is optimizable. Internal transformation of statementspredicates @@ -25,23 +28,22 @@ limitations under the License. -

WHERE clauses with predicates joined -by OR are usually not optimizable. WHERE clauses with predicates joined by -AND are optimizable if at least one of the predicates -is optimizable. For example: +

For example:

SELECT * FROM Flights WHERE flight_id = 'AA1111' -AND segment_number <> 2

-

In this example, the first predicate is optimizable; the second predicate -is not. Therefore, the statement is optimizable. -In a few -cases, a WHERE clause with predicates joined by OR can be transformed into -an optimizable statement. See .

-

can transform some predicates internally so that at least one -of the predicates is optimizable and thus the statement is optimizable. This -section describes the predicate transformations that performs -to make predicates optimizable.

-

A predicate that uses the following comparison operators can sometimes -be transformed internally into optimizable predicates. -

+AND segment_number <> 2 +

In this example, the first +predicate +is optimizable; the second predicate is not. Therefore, the statement is +optimizable.

+

In a few cases, a WHERE clause with predicates joined by OR can be +transformed into an optimizable statement. See +.

+

can transform some +predicates internally so that at least one of the predicates is optimizable and +thus the statement is optimizable. This section describes the predicate +transformations that +performs to make predicates optimizable.

+

A predicate that uses the following comparison operators can sometimes be +transformed internally into optimizable predicates.

Modified: db/derby/docs/trunk/src/tuning/ctuntransform36368.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform36368.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform36368.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform36368.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,11 @@ limitations under the License. Flattening a subquery into a normal join +Subqueries are allowed to return more than one row when used with IN, +EXISTS, and ANY. However, for each row returned in the outer row, + evaluates the subquery +until it returns one row; it does not evaluate the subquery for all rows +returned. Subqueriesflattening of @@ -25,11 +30,7 @@ limitations under the License. -

Subqueries are allowed to return more than one row when used with IN, EXISTS, -and ANY. However, for each row returned in the outer row, evaluates -the subquery until it returns one row; it does not evaluate the subquery for -all rows returned.

-

For example, given two tables, t1 and t2:

+

For example, given two tables, t1 and t2:

c1 -- 1 @@ -42,85 +43,90 @@ all rows returned.

2 1
-

and the following query: -SELECT c1 FROM t1 WHERE c1 IN (SELECT c1 FROM t2)

-

the results would be +

and the following query:

+SELECT c1 FROM t1 WHERE c1 IN (SELECT c1 FROM t2) +

the results would be

1 -2

-

Simply selecting t1.c1 when simply joining those -tables has different results: +2 +

Simply selecting t1.c1 when simply joining those tables has +different results:

SELECT t1.c1 FROM t1, t2 WHERE t1.c1 = t2.c1 1 2 - 2

-

Statements that include such subqueries can be flattened into joins only -if the subquery does not introduce any duplicates into the result set (in -our example, the subquery introduced a duplicate and so cannot simply be flattened -into a join). If this requirement and other requirements (listed below) are -met, however, the statement is flattened such that the tables in the subquery's -FROM list are treated as if they were inner to the tables in the outer FROM -list.

-

For example, the query could have been flattened into a join if c1 in t2 had a unique index on it. It would not -have introduced any duplicate values into the result set.

-

The requirements for flattening into a normal join are: + 2 +

Statements that include such subqueries can be flattened into joins only if +the subquery does not introduce any duplicates into the result set (in our +example, the subquery introduced a duplicate and so cannot simply be flattened +into a join). If this requirement and other requirements (listed below) are met, +however, the statement is flattened such that the tables in the subquery's FROM +list are treated as if they were inner to the tables in the outer FROM list.

+

For example, the query could have been flattened into a join if +c1 in t2 had a unique index on it. It would +not have introduced any duplicate values into the result set.

+

The requirements for flattening into a normal join are as follows:

    -
  • The subquery is not under an OR.
  • -
  • The subquery type is EXISTS, IN, or ANY, or it is an expression subquery -on the right side of a comparison operator.
  • -
  • The subquery is not in the SELECT list of the outer query block.
  • -
  • There are no aggregates in the SELECT list of the subquery.
  • -
  • The subquery does not have a GROUP BY clause.
  • +
  • The subquery is not under an OR.
  • +
  • The subquery type is EXISTS, IN, or ANY, or it is an expression subquery on +the right side of a comparison operator.
  • +
  • The subquery is not in the SELECT list of the outer query block.
  • +
  • There are no aggregates in the SELECT list of the subquery.
  • +
  • The subquery does not have a GROUP BY clause.
  • The subquery does not have an ORDER BY, result offset, or fetch first clause.
  • There is a uniqueness condition that ensures that the subquery does not -introduce any duplicates if it is flattened into the outer query block.
  • -
  • Each table in the subquery's FROM list (after any view, derived table, -or subquery flattening) must be a base table.
  • -
  • If there is a WHERE clause in the subquery, there is at least one table -in the subquery whose columns are in equality predicates -with expressions that do not include any column references from the subquery -block. These columns must be a superset of the key columns for any unique -index on the table. For all other tables in the subquery, the columns in equality -predicates with expressions that do not include columns from the same table -are a superset of the unique columns for any unique index on the table.
  • -

+introduce any duplicates if it is flattened into the outer query block. +
  • Each table in the subquery's FROM list (after any view, derived table, or +subquery flattening) must be a +base +table.
  • +
  • If there is a WHERE clause in the subquery, there is at least one table in +the subquery whose columns are in +equality +predicates with expressions that do not include any column references +from the subquery block. These columns must be a superset of the key columns for +any unique index on the table. For all other tables in the subquery, the columns +in equality predicates with expressions that do not include columns from the +same table are a superset of the unique columns for any unique index on the +table.
  • +

    Flattening into a normal join gives the optimizer more options for choosing -the best query plan. For example, if the following statement: +the best query plan. For example, if the following statement:

    SELECT huge.* FROM huge -WHERE c1 IN (SELECT c1 FROM tiny)

    -

    can be flattened into -SELECT huge.* FROM huge, tiny WHERE huge.c1 = tiny.c1

    -

    the optimizer can choose a query plan that will scan tiny and do a few probes into the huge table instead of scanning the -huge table and doing a large number of probes into the tiny table.

    -

    Here is an expansion of the example used earlier in this section. Given - +WHERE c1 IN (SELECT c1 FROM tiny) +

    can be flattened into

    +SELECT huge.* FROM huge, tiny +WHERE huge.c1 = tiny.c1 +

    the optimizer can choose a query plan that will scan tiny +and do a few probes into the huge table instead of scanning the huge table and +doing a large number of probes into the tiny table.

    +

    Here is an expansion of the example used earlier in this section. Given

    CREATE TABLE t1 (c1 INT) CREATE TABLE t2 (c1 INT NOT NULL PRIMARY KEY) CREATE TABLE t3 (c1 INT NOT NULL PRIMARY KEY) INSERT INTO t1 VALUES (1), (2), (3) INSERT INTO t2 VALUES (1), (2), (3) -INSERT INTO t3 VALUES (2), (3), (4)

    -

    this query +INSERT INTO t3 VALUES (2), (3), (4) +

    this query

    SELECT t1.* FROM t1 WHERE t1.c1 IN - (SELECT t2.c1 FROM t2, t3 WHERE t2.c1 = t3.c1)

    -

    should return the following results: + (SELECT t2.c1 FROM t2, t3 WHERE t2.c1 = t3.c1) +

    should return the following results:

    2 -3

    -

    The query satisfies all the requirements for flattening into a join, and -the statement can be transformed into the following one: +3 +

    The query satisfies all the requirements for flattening into a join, and the +statement can be transformed into the following one:

    SELECT t1.* FROM t1, t2, t3 WHERE t1.c1 = t2.c1 AND t2.c1 = t3.c1 -AND t1.c1 = t3.c1

    -

    The following query: +AND t1.c1 = t3.c1 +

    The following query:

    SELECT t1.* FROM t1 WHERE EXISTS -(SELECT * FROM t2, t3 WHERE t2.c1 = t3.c1 AND t2.c1 = t1.c1)

    -

    can be transformed into +(SELECT * FROM t2, t3 WHERE t2.c1 = t3.c1 AND t2.c1 = t1.c1) +

    can be transformed into

    SELECT t1.* FROM t1, t2, t3 WHERE t1.c1 = t2.c1 AND t2.c1 = t3.c1 -AND t1.c1 = t3.c1

    +AND t1.c1 = t3.c1
    Modified: db/derby/docs/trunk/src/tuning/ctuntransform36623.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform36623.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform36623.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform36623.dita Fri Jul 11 20:43:23 2014 @@ -20,57 +20,63 @@ limitations under the License. --> Predicates pushed into views or derived tables +An SQL statement that references a view can also include a +predicate. predicatespushed down into views -

    An SQL statement that references a view can also include a predicate. Consider -the view v2 (a,b): CREATE VIEW v2(a,b) AS +

    Consider the view v2(a,b):

    +CREATE VIEW v2(a,b) AS SELECT sales_person, MAX(sales) FROM Sales -GROUP BY sales_person

    -

    The following statement references the view and includes a predicate: - SELECT * +GROUP BY sales_person +

    The following statement references the view and includes a predicate:

    +SELECT * FROM v2 -WHERE a = 'LUCCHESSI'

    -

    When transforms -that statement by first transforming the view into a derived table, it places -the predicate at the top level of the new query, outside the scope of the -derived table: SELECT a, b +WHERE a = 'LUCCHESI' +

    When transforms that +statement by first transforming the view into a derived table, it places the +predicate at the top level of the new query, outside the scope of the derived +table:

    +SELECT a, b FROM (SELECT sales_person, MAX(sales) FROM Sales - WHERE sales_person = 'LUCCHESSI' + WHERE sales_person = 'LUCCHESI' GROUP BY sales_person) - v1(a, b) -

    -

    In the example in the preceding section (see ), was able to flatten the + v1(a, b) +

    In the example in , + was able to flatten the derived table into the main SELECT, so the predicate in the outer SELECT could be evaluated at a useful point in the query. This is not possible in this -example, because the underlying view does not satisfy all the requirements -of view flattening.

    -

    However, if the source of all of the column references in a predicate is -a simple -column reference in the underlying view or table, is -able to push the predicate down to the underlying view. Pushing -down means that the qualification described by the predicate can be evaluated -when the view is being evaluated. In our example, the column reference in -the outer predicate, a, in the underlying view is a simple -column reference to the underlying base +example, because the underlying view does not satisfy all the requirements of +view flattening.

    +

    However, if the source of all of the column references in a predicate is a +simple +column reference in the underlying view or table, + is able to push +the predicate down to the underlying view. Pushing down means that the +qualification described by the predicate can be evaluated when the view is being +evaluated. In our example, the column reference in the outer predicate, +a, in the underlying view is a simple column reference to the +underlying +base table. So the final transformation of this statement after predicate -pushdown is: SELECT a, b +pushdown is:

    +SELECT a, b FROM (SELECT sales_person, MAX(sales) from Sales -WHERE sales_person = 'LUCCHESSI' -GROUP BY sales_person) v1(a, b)

    -

    Without the transformation, would -have to scan the entire table t1 to form all the groups, only to throw -out all but one of the groups. With the transformation, is -able to make that qualification part of the derived table.

    -

    If there were a predicate that referenced column b, it could not -be pushed down, because in the underlying view, column b is not a simple -column reference.

    +WHERE sales_person = 'LUCCHESI' +GROUP BY sales_person) v1(a, b)
    +

    Without the transformation, + would have to scan the +entire table t1 to form all the groups, only to throw out all +but one of the groups. With the transformation, + is able to make that +qualification part of the derived table.

    +

    If there were a predicate that referenced column b, it could +not be pushed down, because in the underlying view, column b is +not a simple column reference.

    Predicate pushdown transformation includes predicates that reference multiple tables from an underlying join.

    Modified: db/derby/docs/trunk/src/tuning/ctuntransform37032.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform37032.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform37032.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform37032.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,8 @@ limitations under the License. Transitive closure +The transitive property of numbers states that if A = B and B = C, +then A = C. Transitive closure @@ -26,15 +28,12 @@ limitations under the License. -

    The transitive property of numbers states that if A = B and B = C, then -A = C.

    -

    applies this property to query predicates to add additional -predicates to the query in order to give the optimizer more information. This -process is called transitive closure. There are two -types of transitive closure: +

    applies this +property to query predicates to add additional predicates to the query in order +to give the optimizer more information. This process is called transitive +closure. There are two types of transitive closure:

      -
    • Transitive closure on join clauses -

      Applied first, if applicable

    • +
    • Transitive closure on join clauses (applied first, if applicable)
    • Transitive closure on search clauses
    • -

    +
    Modified: db/derby/docs/trunk/src/tuning/ctuntransform47182.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform47182.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform47182.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform47182.dita Fri Jul 11 20:43:23 2014 @@ -18,8 +18,9 @@ limitations under the License. Flattening VALUES subqueries + flattens +VALUES subqueries to improve performance. -

    flattens VALUES subqueries to improve performance.

    Modified: db/derby/docs/trunk/src/tuning/ctuntransform55045.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform55045.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform55045.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform55045.dita Fri Jul 11 20:43:23 2014 @@ -18,10 +18,11 @@ limitations under the License. Outer join transformations + transforms +OUTER to INNER joins when the predicate filters out all nulls on the join +column. This transformation can allow more potential query plans and thus better +performance. -

    transforms OUTER to INNER joins when the predicate filters -out all nulls on the join column. This transformation can allow more potential -query plans and thus better performance.

    Modified: db/derby/docs/trunk/src/tuning/ctuntransform867165.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform867165.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform867165.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform867165.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,9 @@ limitations under the License. DISTINCT elimination in IN, ANY, and EXISTS subqueries +An IN, ANY, or EXISTS subquery evaluates to true if there is at least +one row that causes the subquery to evaluate to true. These semantics make a +DISTINCT within an IN, ANY, or EXISTS subquery unnecessary. Subquerieselimination of DISTINCT in IN, @@ -26,13 +29,11 @@ ANY, and EXISTS subqueries -

    An IN, ANY, or EXISTS subquery evaluates to true if there is at least one -row that causes the subquery to evaluate to true. These semantics make a DISTINCT -within an IN, ANY, or EXISTS subquery unnecessary. The following two queries -are equivalent and the first is transformed into the second: +

    The following two queries are equivalent, and the first is transformed into +the second:

    SELECT * FROM t1 WHERE c1 IN (SELECT DISTINCT c2 FROM t2 WHERE t1.c3 = t2.c4) SELECT * FROM t1 WHERE c1 IN - (SELECT c2 FROM t2 WHERE t1.c3 = t2.c4)

    + (SELECT c2 FROM t2 WHERE t1.c3 = t2.c4)
    Modified: db/derby/docs/trunk/src/tuning/ctuntransform867201.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/ctuntransform867201.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/ctuntransform867201.dita (original) +++ db/derby/docs/trunk/src/tuning/ctuntransform867201.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,9 @@ limitations under the License. IN/ANY subquery transformation +An IN or ANY subquery that is guaranteed to return at most one row +can be transformed into an equivalent expression subquery (a scalar subquery +without the IN or ANY). The subquery must not be correlated. IN/ANY subquery transformation @@ -25,26 +28,24 @@ limitations under the License. -

    An IN or ANY subquery that is guaranteed to return at most one row can -be transformed into an equivalent expression subquery (a scalar subquery without -the IN or ANY). The subquery must not be correlated. Subqueries guaranteed -to return at most one row are: +

    Subqueries guaranteed to return at most one row are:

    • Simple VALUES clauses
    • SELECTs returning a non-grouped aggregate
    • -

    -

    For example: -WHERE C1 IN (SELECT MIN(c1) FROM T)

    -

    can be transformed into -WHERE C1 = (SELECT MIN(c1) FROM T)

    + +

    For example,

    +WHERE C1 IN (SELECT MIN(c1) FROM T) +

    can be transformed into

    +WHERE C1 = (SELECT MIN(c1) FROM T)

    This transformation is considered before subquery materialization. If the -transformation is performed, the subquery becomes materializable. In the example, -if the IN subquery were not transformed, it would be evaluated anew for each -row.

    +transformation is performed, the subquery becomes materializable. In the +example, if the IN subquery were not transformed, it would be evaluated anew for +each row.

    The subquery type transformation is shown in the following table.

    -IN or ANY subquery transformations for subqueries -that return a single row +IN or ANY subquery transformations for subqueries that return a single +row +This table shows how IN and ANY subqueries are transformed. Modified: db/derby/docs/trunk/src/tuning/rtuntransform139.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/rtuntransform139.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/rtuntransform139.dita (original) +++ db/derby/docs/trunk/src/tuning/rtuntransform139.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,9 @@ limitations under the License. BETWEEN transformations +A BETWEEN predicate is transformed into equivalent predicates that +use the >= and <= operators, which are +optimizable. BETWEEN transformations @@ -26,10 +29,9 @@ limitations under the License. -

    A BETWEEN predicate is transformed into equivalent predicates that use -the >= and <= operators, which are optimizable. For example: -flight_date BETWEEN DATE('2005-04-01') and DATE('2005-04-10')

    -

    is transformed into +

    For example,

    +flight_date BETWEEN DATE('2005-04-01') and DATE('2005-04-10') +

    is transformed into

    flight_date >= DATE('2005-04-01') -AND flight_date >= '2005-04-10'

    +AND flight_date >= '2005-04-10'
    Modified: db/derby/docs/trunk/src/tuning/rtuntransform208.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/rtuntransform208.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/rtuntransform208.dita (original) +++ db/derby/docs/trunk/src/tuning/rtuntransform208.dita Fri Jul 11 20:43:23 2014 @@ -18,6 +18,8 @@ limitations under the License. LIKE transformations +This section describes using LIKE transformations as a comparison +operator. LIKE transformations @@ -26,8 +28,6 @@ limitations under the License.
    -

    This section describes using LIKE transformations as a comparison operator. -

    LIKE transformations and optimizations are disabled when you use locale-based collation. See "Character-based collation in " in the Modified: db/derby/docs/trunk/src/tuning/rtuntransform345.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/rtuntransform345.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/rtuntransform345.dita (original) +++ db/derby/docs/trunk/src/tuning/rtuntransform345.dita Fri Jul 11 20:43:23 2014 @@ -18,25 +18,27 @@ limitations under the License. Character string beginning with constant +A LIKE predicate in which a column is compared to a character string +that begins with a character constant (not a wildcard) is transformed +into three predicates: one predicate that uses the LIKE operator, one that +uses the >= operator, and one that uses the +< operator. -

    A LIKE predicate in which a column is compared to a character string that begins with a character constant (not a wildcard) is transformed -into three predicates: one predicate that uses the LIKE operator, one that -uses the >= operator, and one that uses the < operator. For example: - -country LIKE 'Ch%i%'

    -

    becomes +

    For example,

    +country LIKE 'Ch%i%' +

    becomes

    country LIKE 'Ch%i%' AND country >= 'Ch' -AND country < 'Ci'

    -

    The first (LIKE) predicate is not optimizable, but the new predicates added -by the transformation are.

    -

    When the character string begins with one more character constants and -ends with a single "%", the first LIKE clause is eliminated. For example: - -country LIKE 'Ch%'

    -

    becomes +AND country < 'Ci' +

    The first (LIKE) predicate is not optimizable, but the new +predicates added by the transformation are.

    +

    When the character string begins with one more character constants +and ends with a single "%", the first LIKE clause is eliminated. For +example,

    +country LIKE 'Ch%' +

    becomes

    country >= 'Ch' -AND country < 'Ci'

    +AND country < 'Ci'
    Modified: db/derby/docs/trunk/src/tuning/rtuntransform443.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/rtuntransform443.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/rtuntransform443.dita (original) +++ db/derby/docs/trunk/src/tuning/rtuntransform443.dita Fri Jul 11 20:43:23 2014 @@ -18,18 +18,22 @@ limitations under the License. Character string without wildcards +A LIKE predicate is transformed into a predicate that uses the += operator (and a NOT LIKE predicate is transformed into one +that uses <>) when the character string does not contain +any wildcards. -

    A LIKE predicate is transformed into a predicate that uses the = operator -(and a NOT LIKE predicate is transformed into one that uses <>) when -the character string does not contain any wildcards. For example: -country LIKE 'Chile'

    -

    becomes -country = 'Chile'

    -

    and -country NOT LIKE 'Chile'

    -

    becomes -country <> 'Chile'

    -

    Predicates that use the = operator are optimizable. Predicates that use the <> operator are sargable.

    +

    For example,

    +country LIKE 'Chile' +

    becomes

    +country = 'Chile' +

    and

    +country NOT LIKE 'Chile' +

    becomes

    +country <> 'Chile' +

    Predicates that use the = operator are +optimizable. Predicates that use the <> operator are sargable.

    +
    Modified: db/derby/docs/trunk/src/tuning/rtuntransform472.dita URL: http://svn.apache.org/viewvc/db/derby/docs/trunk/src/tuning/rtuntransform472.dita?rev=1609827&r1=1609826&r2=1609827&view=diff ============================================================================== --- db/derby/docs/trunk/src/tuning/rtuntransform472.dita (original) +++ db/derby/docs/trunk/src/tuning/rtuntransform472.dita Fri Jul 11 20:43:23 2014 @@ -18,25 +18,26 @@ limitations under the License. Unknown parameter +The situation is similar to those described above when a column is +compared using the LIKE operator to a parameter whose value is unknown in +advance (dynamic parameter, join column, and the like). -

    'The situation is similar to those described above when a column is compared -using the LIKE operator to a parameter whose value is unknown in advance (dynamic -parameter, join column, etc.).

    -

    In this situation, the LIKE predicate is likewise transformed into three -predicates: one LIKE predicate, one predicate using the >= operator, and -one predicate using the < operator. For example: -country LIKE ?

    -

    is transformed into +

    In this situation, the LIKE predicate is likewise transformed into +three predicates: one LIKE predicate, one predicate using the +>= operator, and one predicate using the +< operator. For example,

    +country LIKE ? +

    is transformed into

    country LIKE ? AND country >= InternallyGeneratedParameter -AND country < InternallyGeneratedParameter

    -

    where the InternallyGeneratedParameters are calculated -at the beginning of execution based on the value of the parameter. -This transformation can lead to a bad plan if the user passes in -a string that begins with a wildcard or a nonselective string as the parameter. +AND country < InternallyGeneratedParameter +

    where the InternallyGeneratedParameters are calculated at the +beginning of execution based on the value of the parameter.

    +

    This transformation can lead to a bad plan if the user passes in a +string that begins with a wildcard or a nonselective string as the parameter. Users can work around this possibility by writing the query like this (which -is not optimizable): +is not optimizable): (country || '') LIKE ?