Search Images Maps Play YouTube News Gmail Drive More »
Sign in
Screen reader users: click this link for accessible mode. Accessible mode has the same essential features but works better with your reader.

Patents

  1. Advanced Patent Search
Publication numberUS5590324 A
Publication typeGrant
Application numberUS 08/384,925
Publication dateDec 31, 1996
Filing dateFeb 7, 1995
Priority dateFeb 7, 1995
Fee statusLapsed
Publication number08384925, 384925, US 5590324 A, US 5590324A, US-A-5590324, US5590324 A, US5590324A
InventorsTing Y. Leung, Mir H. Pirahesh, Michelle M. Jou, David E. Simmen
Original AssigneeInternational Business Machines Corporation
Export CitationBiBTeX, EndNote, RefMan
External Links: USPTO, USPTO Assignment, Espacenet
Optimization of SQL queries using universal quantifiers, set intersection, and max/min aggregation in the presence of nullable columns
US 5590324 A
Abstract
A method and apparatus for optimizing SQL queries by propagating and exploiting column nullability. Column nullability is identified and propagated using a three-valued logic, wherein a column of a table can be identified nullability information is exploited to optimize query operations through transformations. In one aspect of the present invention, quantified predicates (such as ">ALL") are transformed into simple predicates involving singleton subqueries so that indexing can be exploited. In another aspect of the present invention, "is not null" predicates are generated and pushed for certain aggregate queries. In still another aspect of the present invention, intersect operations are transformed into joins. The end result is that the present invention can significantly enhance the performance of the queries.
Images(16)
Previous page
Next page
Claims(38)
What is claimed is:
1. A method of optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) propagating column nullability values in the memory of the computer through various SQL operations of the query, wherein the column nullability values are selected from a group comprising null, non-null, and nullable; and
(b) optimizing the SQL operations of the query in the memory of the computer based on the propagated column nullability values.
2. The method of claim 1 above, further comprising the step of translating the query into a query graph model.
3. The method of claim 1 above, further comprising the step of executing the SQL query against the relational database and outputting a result table to the user.
4. The method of claim 1 above, wherein the optimizing step comprises the steps of:
(1) examining the query in the memory of the computer to determine whether it contains a universal quantified predicate between an outer column and an inner column returned by a subquery;
(2) examining the query in the memory of the computer to determine nullability values for the outer and inner columns;
(3) creating a derived table in the memory of the computer representing one or more boundary values for the inner column and the nullability value for the inner column, wherein the boundary values are selected from a group comprising a maximum value and a minimum value returned from the subquery, and wherein the derived table contains only one tuple; and
(4) transforming the query in the memory of the computer using the derived table to replace the universal quantified predicate with a simple predicate containing the outer column and columns of the derived table.
5. The method of claim 4 above, further comprising the step of accessing the outer column using an index when there is an index on the outer column.
6. The method of claim 4 above, wherein the universal quantified predicate comprises a relational operator combined with an "ALL" keyword.
7. The method of claim 4 above, wherein the relational operator is selected from a group comprising =, >, <, <=, and >=.
8. The method of claim 4 above, wherein the transforming step further comprises the step of replacing the universal quantified predicate with a first simple predicate involving a singleton subquery when both the inner and outer columns are nullable.
9. The method of claim 4 above, wherein the transforming step further comprises the step of replacing the universal quantified predicate with a second simple predicate involving a singleton subquery when the inner column is nullable and the outer column is non-null.
10. The method of claim 4 above, wherein the simple predicate comprises one or more predicates comparing the outer column to the boundary values for the inner column and the nullability value for the inner column.
11. A method of optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) examining the query in the memory of the computer to determine whether it contains a universal quantified predicate between an outer column and an inner column returned by a subquery;
(b) examining the query in the memory of the computer to determine nullability values for the outer and inner columns, wherein the nullability values are selected from a group comprising null, non-null, and nullable;
(c) creating a derived table in the memory of the computer representing one or more boundary values for the inner column and the nullability value for the inner column, wherein the boundary values are selected from a group comprising a maximum value and a minimum value returned from the subquery, and wherein the derived table contains only one tuple; and
(d) transforming the query in the memory of the computer using the derived table to replace the universal quantified predicate with a simple predicate involving a singleton subquery containing the outer column and columns of the derived table.
12. The method of claim 11 above, further comprising the step of accessing the outer column using an index when there is an index on the outer column.
13. The method of claim 11 above, wherein the universal quantified predicate comprises a relational operator combined with an "ALL" keyword.
14. The method of claim 11 above, wherein the relational operator is selected from a group comprising =, >, <, <=, and >=.
15. The method of claim 11 above, wherein the transforming step further comprises the step of replacing the universal quantified predicate with a first simple predicate involving a singleton subquery when both the inner and outer columns are nullable.
16. The method of claim 11 above, wherein the transforming step further comprises the step of replacing the universal quantified predicate with a second simple predicate involving a singleton subquery when the inner column is nullable and the outer column is non-null.
17. The method of claim 11 above, wherein the simple predicate comprises one or more predicates comparing the outer column to the boundary values for the inner column and the nullability value for the inner column.
18. The method of claim 11 above, further comprising the step of propagating column nullability values through various SQL operations of the query.
19. A method of optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) examining the query in the memory of the computer to determine: (1) whether the query contains a GROUP BY operation on a table without any GROUP BY columns, (2) whether the query contains only a MAX aggregation function in a SELECT clause of the query, (3) whether a column referenced in the MAX aggregation function has a descending index with a null value as a highest collating value, and (4) whether the column referenced in the MAX aggregation function is nullable; and
(b) transforming the query in the memory of the computer to include a new predicate for the column as a Boolean factor in a WHERE clause when the query contains the GROUP BY operation on the table without any GROUP BY columns, the query contains only the MAX aggregation function in the SELECT clause, the column referenced in the MAX aggregation function has a descending index with a null value as a highest collating value, and the column referenced in the MAX aggregation function is nullable, wherein the new predicate is of the form "COLUMN IS NOT NULL."
20. A method of optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) examining the query in the memory of the computer to determine: (1) whether the query contains a GROUP BY operation on a table without any GROUP BY columns, (2) whether the query contains only a MIN aggregation function in a SELECT clause of the query, (3) whether a column referenced in the MIN aggregation function has an ascending index with a null value as a lowest collating value, and (4) whether the column referenced in the MIN aggregation function is nullable; and
(b) transforming the query in the memory of the computer to include a new predicate for the column as a Boolean factor in a WHERE clause when the query contains the GROUP BY operation on the table without any GROUP BY columns, the query contains only the MIN aggregation function in the SELECT clause, the column referenced in the MIN aggregation function has an ascending index with a null value as a lowest collating value, and the column referenced in the MIN aggregation function is nullable, wherein the new predicate is of the form "COLUMN IS NOT NULL."
21. A method of optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) examining the query in the memory of the computer to determine whether the query contains an INTERSECT operation and whether an output of the query is distinct;
(b) examining the query in the memory of the computer to determine whether each output column of the query has at least one of its corresponding input columns being non-null; and
(c) transforming the query in the memory of the computer by replacing the INTERSECT operation with a join query when the query contains an INTERSECT operation, the output of the query is distinct, and each output column of the query has at least one of its corresponding input columns being non-null.
22. The method of claim 21, wherein the examining step (a) further comprises the step of:
(1) examining the query in the memory of the computer to determine whether any operands of the INTERSECT operation has a key; and
(2) identifying an output of the query as distinct when one of the operands of the INTERSECT operation has a key.
23. An apparatus for optimizing an SQL query, comprising:
(a) a computer having a memory and an electronic storage device coupled thereto, the data storage device storing a relational database;
(b) means, performed by the computer, for accepting the SQL query into the memory of the computer, the SQL query being performed by the computer to retrieve data from a relational database stored in the computer;
(c) means, performed by the computer, for propagating column nullability values in the memory of the computer through various SQL operations of the query, wherein the column nullability values are selected from a group comprising null, non-null, and nullable; and
(d) means, performed by the computer, for optimizing the SQL operations of the query in the memory of the computer based on the propagated column nullability values.
24. An apparatus for optimizing an SQL query, comprising:
(a) a computer having a memory and an electronic storage device coupled thereto, the data storage device storing a relational database;
(b) means, performed by the computer, for accepting the SQL query into the memory of the computer, the SQL query being performed by the computer to retrieve data from a relational database stored in the computer;
(c) means, performed by the computer, for examining the query in the memory of the computer to determine whether it contains a universal quantified predicate between an outer column and an inner column returned by a subquery;
(d) means, performed by the computer, for examining the query in the memory of the computer to determine nullability values for the outer and inner columns, wherein the nullability values are selected from a group comprising null, non-null, and nullable;
(e) means, performed by the computer, for creating a derived table in the memory of the computer representing one or more boundary values for the inner column and the nullability value for the inner column, wherein the boundary values are selected from a group comprising a maximum value and a minimum value returned from the subquery, and wherein the derived table contains only one tuple; and
(f) means, performed by the computer, for transforming the query in the memory of the computer using the derived table to replace the universal quantified predicate with a simple predicate involving a singleton subquery containing the outer column and columns of the derived table.
25. The apparatus of claim 24 above, further comprising means for accessing the outer column using an index when there is an index on the outer column.
26. The apparatus of claim 24 above, wherein the means for transforming further comprises means for replacing the universal quantified predicate with a first simple predicate involving a singleton subquery when both the inner and outer columns are nullable.
27. The apparatus of claim 24 above, wherein the means for transforming further comprises means for replacing the universal quantified predicate with a second simple predicate involving a singleton subquery when the inner column is nullable and the outer column is non-null.
28. The apparatus of claim 22 above, wherein the simple predicate comprises one or more predicates comparing the outer column to the boundary values for the inner column and the nullability value for the inner column.
29. The apparatus of claim 24 above, further comprising means for propagating column nullability values through various SQL operations of the query.
30. An apparatus for optimizing an SQL query, comprising:
(a) a computer having a memory and an electronic storage device coupled thereto, the data storage device storing a relational database;
(b) means, performed by the computer, for accepting the SQL query into the memory of the computer, the SQL query being performed by the computer to retrieve data from a relational database stored in the computer;
(c) means, performed by the computer, for examining the query in the memory of the computer to determine: (1) whether the query contains a GROUP BY operation on a table without any GROUP BY columns, (2) whether the query contains only a MAX aggregation function in a SELECT clause of the query, (3) whether a column referenced in the MAX aggregation function has a descending index with a null value as a highest collating value, and (4) whether the column referenced in the MAX aggregation function is nullable; and
(d) means, performed by the computer, for transforming the query in the memory of the computer to include a new predicate for the column as a Boolean factor in a WHERE clause when the query contains the GROUP BY operation on the table without any GROUP BY columns, the query contains only the MAX aggregation function in the SELECT clause, the column referenced in the MAX aggregation function has a descending index with a null value as a highest collating value, and the column referenced in the MAX aggregation function is nullable, wherein the new predicate is of the form "COLUMN IS NOT NULL."
31. An apparatus for optimizing an SQL query, comprising:
(a) a computer having a memory and an electronic storage device coupled thereto, the data storage device storing a relational database;
(b) means, performed by the computer, for accepting the SQL query into the memory of the computer, the SQL query being performed by the computer to retrieve data from a relational database stored in the computer;
(c) means, performed by the computer, for examining the query in the memory of the computer to determine: (1) whether the query contains a GROUP BY operation on a table without any GROUP BY columns, (2) whether the query contains only a MIN aggregation function in a SELECT clause of the query, (3) whether a column referenced in the MIN aggregation function has an ascending index with a null value as a lowest collating value, and (4) whether the column referenced in the MIN aggregation function is nullable; and
(d) means, performed by the computer, for transforming the query in the memory of the computer to include a new predicate for the column as a Boolean factor in a WHERE clause when the query contains the GROUP BY operation on the table without any GROUP BY columns, the query contains only the MIN aggregation function in the SELECT clause, the column referenced in the MIN aggregation function has an ascending index with a null value as a lowest collating value, and the column referenced in the MIN aggregation function is nullable, wherein the new predicate is of the form "COLUMN IS NOT NULL."
32. An apparatus for optimizing an SQL query, comprising:
(a) a computer having a memory and an electronic storage device coupled thereto, the data storage device storing a relational database;
(b) means, performed by the computer, for accepting the SQL query into the memory of the computer, the SQL query being performed by the computer to retrieve data from a relational database stored in the computer;
(c) means, performed by the computer, for examining the query in the memory of the computer to determine whether the query contains an INTERSECT operation and whether an output of the query is distinct;
(d) means, performed by the computer, for examining the query in the memory of the computer to determine whether each output column of the query has at least one of its corresponding input columns being non-null; and
(e) means, performed by the computer, for transforming the query in the memory of the computer by replacing the INTERSECT operation with a join query when the query contains an INTERSECT operation, the output of the query is distinct, and each output column of the query has at least one of its corresponding input columns being non-null.
33. The apparatus of claim 32, wherein the means for examining (c) further comprises:
(1) means for examining the query in the memory of the computer to determine whether any operands of the INTERSECT operation has a key; and
(2) means for identifying an output of the query as distinct when one of the operands of the INTERSECT operation has a key.
34. A program storage device readable by a computer, tangibly embodying a program of instructions executable by the computer to perform method steps for optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) propagating column nullability values in the memory of the computer through various SQL operations of the query, wherein the column nullability values are selected from a group comprising null, non-null, and nullable; and
(b) optimizing the SQL operations of the query in the memory of the computer based on the propagated column nullability values.
35. A program storage device readable by a computer, tangibly embodying a program of instructions executable by the computer to perform method steps for optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) examining the query in the memory of the computer to determine whether it contains a universal quantified predicate between an outer column and an inner column returned by a subquery;
(b) examining the query in the memory of the computer to determine nullability values for the outer and inner columns, wherein the nullability values are selected from a group comprising null, non-null, and nullable;
(c) creating a derived table in the memory of the computer representing one or more boundary values for the inner column and the nullability value for the inner column, wherein the boundary values are selected from a group comprising a maximum value and a minimum value returned from the subquery, and wherein the derived table contains only one tuple; and
(d) transforming the query in the memory of the computer using the derived table to replace the universal quantified predicate with a simple predicate involving a singleton subquery containing the outer column and columns of the derived table.
36. A program storage device readable by a computer, tangibly embodying a program of instructions executable by the computer to perform method steps for optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) examining the query in the memory of the computer to determine: (1) whether the query contains a GROUP BY operation on a table without any GROUP BY columns, (2) whether the query contains only a MAX aggregation function in a SELECT clause of the query, (3) whether a column referenced in the MAX aggregation function has a descending index with a null value as a highest collating value, and (4) whether the column referenced in the MAX aggregation function is nullable; and
(b) transforming the query in the memory of the computer to include a new predicate involving the column as a Boolean factor in a WHERE clause when the query contains the GROUP BY operation on the table without any GROUP BY columns, the query contains only the MAX aggregation function in the SELECT clause, the column referenced in the MAX aggregation function has a descending index with a null value as a highest collating value, and the column referenced in the MAX aggregation function is nullable, wherein the new predicate is of the form "COLUMN IS NOT NULL."
37. A program storage device readable by a computer, tangibly embodying a program of instructions executable by the computer to perform method steps for optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) examining the query in the memory of the computer to determine: (1) whether the query contains a GROUP BY operation on a table without any GROUP BY columns, (2) whether the query contains only a MIN aggregation function in a SELECT clause of the query, (3) whether a column referenced in the MIN aggregation function has an ascending index with a null value as a lowest collating value, and (4) whether the column referenced in the MIN aggregation function is nullable; and
(b) transforming the query in the memory of the computer to include a new predicate for the column as a Boolean factor in a WHERE clause when the query contains the GROUP BY operation on the table without any GROUP BY columns, the query contains only the MIN aggregation function in the SELECT clause, the column referenced in the MIN aggregation function has an ascending index with a null value as a lowest collating value, and the column referenced in the MIN aggregation function is nullable, wherein the new predicate is of the form "COLUMN IS NOT NULL."
38. A program storage device readable by a computer, tangibly embodying a program of instructions executable by the computer to perform method steps for optimizing an SQL query in a computer having a memory, the SQL query being performed by the computer to retrieve data from a relational database stored in an electronic storage device coupled to the computer, the method comprising the steps of:
(a) examining the query in the memory of the computer to determine whether the query contains an INTERSECT operation and whether an output of the query is distinct;
(b) examining the query in the memory of the computer to determine whether each output column of the query has at least one of its corresponding input columns being non-null; and
(c) transforming the query in the memory of the computer by replacing the INTERSECT operation with a join query when the query contains an INTERSECT operation, the output of the query is distinct, and each output column of the query has at least one of its corresponding input columns being non-null.
Description
BACKGROUND OF THE INVENTION

1. Field of the Invention

This invention relates in general to database management systems performed by computers, and in particular, to the optimization of SQL queries in a relational database management system by propagating and exploiting column nullability.

2. Description of Related Art

Relational DataBase Management System (RDBMS) software using a Structured Query Language (SQL) interface is well known in the art. The SQL interface has evolved into a standard language for RDBMS software and has been adopted as such by both the American Nationals Standard Organization (ANSI) and the International Standards Organization (ISO).

In RDBMS software, all data is externally structured into tables. The SQL interface allows users to formulate relational operations on the tables either interactively, in batch files, or embedded in host languages such as C, COBOL, etc. Operators are provided in SQL that allow the user to manipulate the data, wherein each operator operates on either one or two tables and produces a new table as a result. The power of SQL lies on its ability to link information from multiple tables or views together to perform complex sets of procedures with a single statement.

According to the SQL standard (i.e., ISO-ANSI Working Draft: Database Language SQL2 and SQL3; X3H2; ISO/IEC JTC1/SC21/WG3, 1993), column nullability for various set operations is based on the two-valued logic, i.e., a column is either nullable or non-nullable. In the SQL standard, null values can be stored only in nullable columns. For example, the following CREATE statement describes an employee table in the SQL standard:

CREATE TABLE EMPLOYEE (EMPNO INTEGER NOT NULL, LASTNAME CHAR (20), SALARY FLOAT)

In the above example, the column "EMPNO" is specified as "NOT NULL", i.e., the "EMPNO" column cannot contain any null values, whereas other columns may or may not contain null values. Consider a simple SELECT query performed against the employee table:

SELECT LASTNAME

FROM EMPLOYEE

WHERE EMPNO IS NULL

Since the EMPNO column cannot contain null values by definition, it can be verified at query compile time that the above query will return an empty result table. Without this simple and yet important optimization, the RDBMS software would have to fetch all tuples from the employee table and then apply the "IS NULL" predicate to the tuples. However, the "IS NULL" predicate is always false for the above query, since the "EMPNO" column was defined as "NOT NULL."

The above example illustrates the importance of exploiting column nullability information in query optimization. The exploitation of column nullability can mean a potentially huge saving in query execution time. However, determining if a column is nullable becomes less straightforward in presence of complex views or derived tables.

Some researchers have performed important work in this area, as reflected in the following publication: G. N. Paulley and Per-Ake Larson, "Exploiting Uniqueness in Query Optimization," Proceedings of IEEE '94 International Conference on Data Engineering, Houston, Tex., 1994, pp. 68-79. For example, this publication discusses the complexity of exploiting uniqueness in query optimization in the presence of null values. However, the current state-of-the-art in SQL query optimization has few solutions for optimizing query expressions by propagating and exploiting column nullability. Thus, there is a need in the art for improved methods of optimizing SQL queries by propagating and exploiting column nullability.

SUMMARY OF THE INVENTION

To overcome the limitations in the prior art described above, and to overcome other limitations that will become apparent upon reading and understanding the present specification, the present invention discloses a method and apparatus for optimizing SQL queries by propagating and exploiting column nullability.

One object of the present invention is to identify and propagate column nullability using a three-valued logic, wherein a column of a derived or base table can be null (when all data values in the column are null), non-null (when the column does not contain any null values), or nullable (when the column may or may not contain null values). The column nullability information is propagated through intermediate evaluations of SQL queries (e.g., selection, projection, join, intersection, except, union and group by operations).

Another object of the present invention is to exploit the column nullability information to optimize query operations through transformations. One aspect of the present invention transforms universal quantified predicates (such as ">ALL") into simple predicates involving singleton subqueries, so that indexing can be exploited. Another aspect of the present invention generates and pushes "is not null" predicates for certain aggregate queries. Still another aspect of the present invention transforms INTERSECT operations into joins.

BRIEF DESCRIPTION OF THE DRAWINGS

Referring now to the drawings in which like reference numbers represent corresponding parts throughout:

FIG. 1 illustrates the computer hardware environment of the present invention;

FIG. 2 is a flowchart illustrating the steps necessary for the interpretation and execution of SQL statements in an interactive environment according to the present invention;

FIG. 3 is a flowchart illustrating the steps necessary for the interpretation and execution of SQL statements embedded in source code according to the present invention;

FIG. 4 is a flowchart illustrating the method of optimizing SQL queries according to the present invention;

FIG. 5 is a flowchart that illustrates the steps required for propagating column nullability in an N-ary UNION operation according to the present invention;

FIG. 6 is a flowchart that illustrates the steps required for propagating column nullability in an N-ary INTERSECT operation according to the present invention;

FIG. 7 is a flowchart that illustrates the steps required for propagating column nullability in a binary EXCEPT operation according to the present invention;

FIG. 8 is a flowchart that illustrates the steps required for propagating column nullability in a binary EXCEPT operation according to the present invention;

FIGS. 9A, 9B, and 9C together are a flowchart that illustrates the steps required for propagating column nullability in a SELECT operation according to the present invention;

FIG. 10 is a flowchart that illustrates the steps required for transforming quantified predicate queries according to the present invention;

FIG. 11 is a flowchart that illustrates the steps required for transforming one-fetch queries according to the present invention when the null value collates high; and

FIG. 12 is a flowchart that illustrates the steps required for transforming one-fetch queries according to the present invention when the null value collates low.

FIG. 13 is a flowchart that illustrates the steps required for transforming INTERSECT-ALL queries according to the present invention that determines if the output of an INTERSECT-ALL query is distinct, i.e., if the output is duplicate-free; and

FIG. 14 is a flowchart that illustrates the steps required for performing intersect-to-join query transformations (which does not rely on a "null=null" operator in the WHERE clause).

DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENT

In the following description of the preferred embodiment, reference is made to the accompanying drawings which form a part hereof, and in which is shown by way of illustration a specific embodiment in which the invention may be practiced. It is to be understood that other embodiments may be utilized and structural and functional changes may be made without departing from the scope of the present invention.

ENVIRONMENT

FIG. 1 illustrates an exemplary computer hardware environment that could be used with the present invention. In the exemplary environment, a computer system 102 is comprised of one or more processors connected to one or more electronic storage devices 104 and 106, such as disk drives, that store one or more relational databases.

Operators of the computer system 102 use a standard operator interface 108, such as IMS/DB/DC, CICS, TSO, OS/2 or other similar interface, to transmit electrical signals to and from the computer system 102 that represent commands for performing various search and retrieval functions, termed queries, against the databases. In the present invention, these queries conform to the Structured Query Language (SQL) standard, and invoke functions performed by Relational DataBase Management System (RDBMS) software. In the preferred embodiment of the present invention, the RDBMS software comprises the DB2 product offered by IBM for the MVS or OS/2 operating systems. Those skilled in the art will recognize, however, that the present invention has application to any RDBMS software that uses SQL.

As illustrated in FIG. 1, the DB2 architecture for the MVS operating system includes three major components: the IMS Resource Lock Manager (IRLM) 110, the Systems Services module 112, and the Database Services module 114. The IRLM 110 handles locking services, because DB2 treats data as a shared resource, thereby allowing any number of users to access the same data simultaneously, and thus concurrency control is required to isolate users and to maintain data integrity. The Systems Services module 112 controls the overall DB2 execution environment, including managing log data sets 106, gathering statistics, handling startup and shutdown, and providing management support.

At the center of the DB2 architecture is the Database Services module 114. The Database Services module 114 contains several submodules, including the Relational Database System (RDS) 116, the Data Manager 118, the Buffer Manager 120 and other components 122 such as an SQL compiler/interpreter. These submodules support the functions of the SQL language, i.e., definition, access control, retrieval, and update of user and system data.

FIG. 2 is a flowchart illustrating the steps necessary for the interpretation and execution of SQL statements in an interactive environment according to the present invention. Block 202 represents the input of SQL statements into the computer system 102 from the user. Block 204 represents the step of compiling or interpreting the SQL statements. An optimization function within block 204 may transform the SQL query in a manner described in more detail later in this specification. Block 206 represents the step of generating a compiled set of runtime structures called an application plan from the compiled SQL statements. Generally, the SQL statements received as input from the user specify only the data that the user wants, but not how to get to it. This step considers both the available access paths (indexes, sequential reads, etc.) and system held statistics on the data to be accessed (the size of the table, the number of distinct values in a particular column, etc.), to choose what it considers to be the most efficient access path for the query. Block 208 represents the execution of the application plan, and block 210 represents the output of the results of the application plan to the user.

FIG. 3 is a flowchart illustrating the steps necessary for the interpretation and execution of SQL statements embedded in source code according to the present invention. Block 302 represents program source code containing a host language (such as COBOL or C) and embedded SQL statements. The program source code is then input to a pre-compile step 304. There are two outputs from the pre-compile step 304: a modified source module 306 and a Database Request Module (DBRM) 308. The modified source module 306 contains host language calls to DB2, which the pre-compile step 304 inserts in place of SQL statements. The DBRM 308 consists of the SQL statements from the program source code 302. A compile and link-edit step 310 uses the modified source module 306 to produce a load module 312, while an optimize and bind step 314 uses the DBRM 308 to produce a compiled set of runtime structures for the application plan 316. As indicated above in conjunction with FIG. 2, the SQL statements from the program source code 302 specify only the data that the user wants, but not how to get to it. The optimize and bind step 314 may reorder the SQL query in a manner described in more detail later in this specification. Thereafter, the optimize and bind step 314 considers both the available access paths (indexes, sequential reads, etc.) and system held statistics on the data to be accessed (the size of the table, the number of distinct values in a particular column, etc.), to choose what it considers to be the most efficient access path for the query. The load module 312 and application plan 316 are then executed together at step 318.

OPTIMIZATION

The present invention provides a technique for extending the identification of column nullability using a three-valued logic. Thus, a column of either a derived table or base table can be:

null, in that all data values in the column are null;

non-null, in that the column does not contain any null values or the column is not nullable; or

nullable, in that the column may or may not contain null values.

The present invention also provides techniques for propagating column nullability information through various SQL operations, such as the SELECT, GROUP BY, UNION, INTERSECT and EXCEPT operations, and the restriction, projection and join operations provided by the SELECT operation. In addition, the present invention provides techniques for query transformation that exploit the propagated column nullability information.

FIG. 4 is a flowchart illustrating the method of optimizing SQL queries according to the present invention. Block 402 represents the acceptance of the SQL query. Block 404 represents the translation of the query into a Query Graph Model (QGM).

The QGM is well known in the art, and is more fully described in the following publications, both of which are incorporated by reference herein:

1. L. Haas, W. Chang, G. M. Lohman, J. McPherson, P. F. Wilms, G. Lapis, B. Lindsay, H. Pirahesh, M. Carey, and E. Shekita, "STARBURST Mid-flight: As the Dust Clears," IEEE Transactions on Knowledge and Data Engineering, March 1990, (hereinafter referred to as "[HAAS90]"); and

2. Hamid Pirahesh, Joseph Hellerstein, and Waqar Hasan, "Extensible/Rule Based Query Rewrite Optimization in STARBURST," Proceedings of ACM SIGMOD '92 International Conference on Management of Data, San Diego, Calif. 1992, (hereinafter referred to as "[PIRA92]") .

The QGM supports arbitrary table operations where the inputs are tables and outputs are tables, such as the SELECT, GROUP BY, UNION, INTERSECT and EXCEPT operations defined in the SQL standard, and the restriction, projection and join operations performed by the SELECT operation.

In QGM, each operation is represented in a data structure having a head and a body. The head describes the output table produced by the operation, and the body specifies the operation required to compute the output table. The head has a number of output columns such as those specified in the select list of a query. The specification of these columns includes column names, types, and output ordering information.

The head includes a Boolean attribute called distinct that indicates whether the associated table contains only distinct tuples ("head.distinct=IS"), or whether it may contain duplicates ("head.distinct=NONE"). For example, by definition, an intersect-distinct query produces no duplicates and thus the head.distinct is always equal to "IS". Each output column, which either returns results to the user or forms a derived column, may have an associated expression corresponding to expressions allowed in the select list of the query. These expressions are called head expressions.

For example, consider the following query:

SELECT C1, C2+C3

FROM T

The first output column is produced by a simple head expression which contains a column C1 from table T. The second output column is produced by a more complex head expression that comprises an addition of values from two columns.

Referring again to FIG. 4, block 406 represents the RDBMS software propagating column nullability for the QGM, as described in more detail in conjunctions with FIGS. 5, 6, 7, 8, 9A, 9B, 9C, and 10. Block 408 represents the RDBMS software transforming the query in the QGM, as described in more detail in conjunction with FIGS. 11, 12, 13, and 14. After query transformation, returns to block 204 in FIG. 2 or block 314 in FIG. 3 for subsequent processing steps, including the execution of the SQL query against the relational database and the output of the result table to the user.

PROPAGATION OF COLUMN NULLABILITY

The three-valued logic for identifying column nullability of the present invention provides more opportunities in terms of query transformation and query optimization. In the following sections, rules are presented for determining the nullability of an output column of an operation.

N-ARY UNION OPERATION

FIG. 5 is a flowchart that illustrates the steps required for propagating column nullability in an N-ary UNION operation according to the present invention. The Nary UNION operation is the result of merging several binary UNION operations. For example, UNION(T1,(UNION(T2,T3))) is equivalent to UNION(T1,T2,T3), where T1, T2 and T3 are base or derived tables.

Block 502 is a decision block that determines if the corresponding columns from all of the operands of a UNION operation are null. If so, then control is transferred to block 504, which identifies the output column as being null. Otherwise, control is transferred to block 506.

Block 506 is a decision block that determines if the corresponding columns from all the operands of the UNION operation are non-null. If so, then control is transferred to block 508, which identifies the output column as being non-null. Otherwise, control is transferred to block 510, which identifies the output column as being nullable, because at least one operand column is nullable. Block 512 ends the routine and returns to FIG. 4.

N-ARY INTERSECT OPERATION

FIG. 6 is a flowchart that illustrates the steps required for propagating column nullability in an N-ary INTERSECT operation according to the present invention. The N-ary INTERSECT operation is the result of merging several binary INTERSECT operations. For example, INTERSECT(T1,(INTERSECT(T2,T3))) is equivalent to INTERSECT(T1,T2,T3), where T1, T2 and T3 are base or derived tables.

Block 602 is a decision block that determines if a column from one operand of an INTERSECT operation is null. If so, then control is transferred to block 604. Otherwise, control is transferred to block 606, which identifies the case where no operand columns are null.

Block 604 is a decision block that determines if the corresponding column from any other operand of the INTERSECT operation is non-null. If so, then control is transferred to block 608, which identifies the case where there is no output tuple. Otherwise, control is transferred to block 610, which identifies the output column as being null.

Block 606 is a decision block that determines if the corresponding columns of all of the operands of the INTERSECT operation are non-null. If so, then control is transferred to block 612, which identifies the output column as being non-null. Otherwise, control is transferred to block 614, which identifies the output column as being nullable.

Block 616 ends the routine and returns to FIG. 4.

BINARY EXCEPT OPERATION

FIG. 7 is a flowchart that illustrates the steps required for propagating column nullability in a binary EXCEPT operation according to the present invention. The nullability of an output column of an EXCEPT operation is the same as the nullability of its first operand column.

Block 702 is a decision block that determines if the column of the first operand of the EXCEPT operation is null. If so, then control is transferred to block 704, which identifies the output column as being null. Otherwise, control is transferred to block 706.

Block 706 is a decision block that determines if the column of the first operand of the EXCEPT operation is non-null. If so, then control is transferred to block 708, which identifies the output column as being non-null. Otherwise, control is transferred to block 710, which identifies the output column as being nullable.

Block 712 ends the routine and returns to FIG. 4.

GROUP BY OPERATION

FIG. 8 is a flowchart that illustrates the steps required for propagating column nullability in a GROUP BY operation according to the present invention. Block 802 is a decision block that determines if the output column is a GROUP BY column. If so, then control is transferred to block 804, which identifies the output column as having the same nullability as the GROUP BY column. Otherwise, control is transferred to block 806.

Block 806 is a decision block that determines if the output column is one of the aggregation functions MAX, MIN, AVG, or SUM. If so, then control is transferred to block 808, which identifies the output column as being nullable. Otherwise, control is transferred to block 810.

Block 810 is a decision block that determines if the output column is the aggregation function COUNT. If so, then control is transferred to block 812, which identifies the output column as being non-null. Otherwise, control is transferred to block 814, which ends the routine and returns to FIG. 4.

SELECT OPERATION

FIGS. 9A, 9B, and 9C together are a flowchart that illustrates the steps required for propagating column nullability in a SELECT operation according to the present invention. For a SELECT operation, the nullability of an output column depends heavily on its head expression.

In the simplest situation, if a head expression simply consists of a column C, then the output column retains the nullability of column C from its input derived table or base table. However, it is not always possible to determine the output column nullability by analyzing the head expression. For example, a CASE statement may produce a null value. Similarly, a user-defined function in the SELECT clause can produce null values. In such cases, nullable is taken as the default value.

FIG. 9A considers the situation where a column is referenced in a predicate in a WHERE clause. Block 902 is a decision block that determines if a column is referenced in a predicate such that the predicate is always true when the column has a null value. If so, control is transferred to block 904, which identifies the predicate as a "C-null" predicate. Otherwise control is transferred to block 906.

Block 906 is a decision block that determines if a column is referenced in a predicate such that the predicate is always false when the column has a null value. If so, control is transferred to block 908, which identifies the predicate as a "C-non-null" predicate. Otherwise, control is transferred to block 910.

FIG. 9B then considers the case when the head expression is only a simple column C. Block 910 is a decision block that determines if column C is an output column of the SELECT operation. If so, control is transferred to block 912, which identifies the output column as retaining the nullability of the column from its lower operation. Otherwise, control is transferred to block 914.

Block 914 is a decision block that determines if the column appears in a Boolean factor predicate that is C-null with respect to the column. If so, control is transferred to block 916, which identifies the output column as being null. Otherwise control is transferred to block 918.

Block 918 is a decision block that determines if the column appears in a Boolean factor predicate that is C-non-null with respect to the column. If so, control is transferred to block 920, which identifies the output column as being non-null. Otherwise control is transferred to block 922.

Note that applying these rules may lead to conflicting results. For example, if "COLUMN IS NULL AND COLUMN IS NOT NULL" appears in the WHERE clause, then the output column can be either null or non-null according to the above rules.

FIG. 9C describes the rules of precedence. Block 922 is a decision block that determines if the output column has been identified as null by one rule and non-null by another rule. If so, control is transferred to block 924, which identifies the query as returning an empty answer set. Otherwise control is transferred to block 926.

Block 926 is a decision block that determines if the output column has been identified as null by one rule and nullable by another rule. If so, control is transferred to block 928, which identifies the output column as being null. Otherwise control is transferred to block 930.

Block 930 is a decision block that determines if the output column has been identified as non-null by one rule and nullable by another rule. If so, control is transferred to block 932, which identifies the output column as being non-null. Otherwise control is transferred to block 934, which ends the routine and returns to FIG. 4.

QUERY OPTIMIZATION USING COLUMN NULLABILITY INFORMATION

In this section, a number of query optimization techniques are provided that exploit the propagated column nullability information of the intermediate query evaluation.

PREDICATE SIMPLIFICATION

Predicates like "IS NULL" and "IS NOT NULL" are very common. The following rules are provided for simplifying predicates in the WHERE clause:

1. If a column C is null, then "C IS NOT NULL" is equivalent to FALSE and "C IS NULL" is equivalent to TRUE.

2. If a column C is non-null, then "C IS NOT NULL" is equivalent to TRUE and "C IS NULL" is equivalent to FALSE.

Note that the first rule is not possible without using the three-valued logic of column nullability according to the present invention.

UNIVERSAL QUANTIFIED PREDICATE TRANSFORMATION

FIG. 10 is a flowchart that illustrates the steps required for transforming universal quantified predicate queries according to the present invention. Universal quantified predicates can be found in queries that take the form "C1 RELOP ALL (SELECT C2 FROM . . .)", wherein RELOP is a relational operator selected from the group comprising (=,>,<,<=,>=), and C1 and C2 represent columns of derived or base tables.

Block 1002 is a decision block that determines if the query matches a particular pattern, i.e., a pattern that contains a particular quantified predicate in a Boolean factor. If so, then control is transferred to block 1004, which is a decision block that determines if the columns referenced in the quantified predicate are nullable. If so, then control is transferred to block 1006, which replaces the quantified predicate with a first simple predicate involving a singleton subquery. Otherwise, control is transferred to block 1008, which is a decision block that determines if the column returned from the subquery portion of the quantified predicate is nullable, but the other column referenced in the quantified predicate is not. If so, then control is transferred to block 1010, which replaces the quantified predicate with a second simple predicate involving a singleton subquery. Otherwise, block 1012 ends the routine and returns to FIG. 4.

The above steps are generalized for several different types of quantified predicates. The different forms of the quantified predicates, and their associated first and second simple predicates are described in more detail below.

CASE 1: >ALL

For the case of transforming the quantified predicate ">ALL", the optimizer looks for a query of the general form:

SELECT S.*

FROM S

WHERE S.C1>ALL (SELECT T.C2 FROM T)

When both columns S.C1 and T.C2 are nullable, the quantified predicate can be transformed into a first simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMAX(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE ( (S.C1 IS NULL AND Z IS NULL)ORS.Cl >= (CASE WHEN Z IS NULL THEN -INFINITY      WHEN Z=0              THEN Y              ELSE NULL   END))AND(S.C1 > Y OR Z IS NULL)__________________________________________________________________________

In the above expression, the "-infinity" denotes the minimal value in the domain of T.C2. The objective of this transformation is that the derived table returns at most one tuple and if there is an index on S.C1, "index or-ing" can be exploited by the optimizer to access the table S. The following discussion describes why such a query transformation can be performed. First, the semantics of the quantified predicate ">all" in presence of null values and empty subquery is explained. If the subquery returns an empty set, then the quantified predicate is true (even if the value of S.C1 is null). If the subquery returns any null value, then the quantified predicate is false. Finally, if the subquery returns one or more values (without any null), then the quantified predicate is true if S.C1 is greater than all values returned from the subquery; otherwise, it is false. Note, that if S.C1 is a null value, then the predicate is false.

Intuitively, the predicate could have been simplified to just comparing S.C1 and the maximal value of the subquery result. However, the presence of null values and empty subquery result leads to a more complicated query transformation. To detect this information, a derived table (denoted as DT) is created that is the result of evaluation of the subquery.

In the above example, the derived table has two columns. The first column (the Y column) computes the maximal value of the subquery result, which is the primary value for comparison purposes. The second column (the Z column) determines if there is any null value and empty result. The second column has three possible values:

null, if the subquery returns an empty set;

1, if there exists a null value in the subquery result; and

0, if there is no null value in the subquery result and there is one or more non-null values.

Furthermore, this derived table returns only a single tuple. The present invention uses these three possible values to transform the quantified predicate into a better form of predicate, so that indexing can be exploited by the optimizer to access the table S via column S.C1. In this case, then index OR-ing technique may be used.

Now, suppose that column T.C2 is nullable but S.C1 is non-null. Then, the above quantified predicate can be transformed into a second simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMAX(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE (S.C1 > = (CASE WHEN Z IS NULL THEN -INFINITY      WHEN Z=0              THEN Y              ELSE NULL   END))AND(S.C1 > Y OR Z IS NULL)__________________________________________________________________________

The difference of this transformed query and the previous one is that "index or-ing" is not needed, because the ">=" predicate is a good range predicate for an index scan on S.C1.

CASE 2: >=ALL

For the case of transforming the quantified predicate ">=ALL", the optimizer looks for a query of the general form:

SELECT S.*

FROM S

WHERE S.C1>=ALL (SELECT T.C2 FROM T)

When both columns S.C1 and T.C2 are nullable, the quantified predicate can be transformed into a first simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMAX(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE ( (S.C1 IS NULL AND Z IS NULL)ORS.Cl >= (CASE WHEN Z IS NULL THEN -INFINITY      WHEN Z=0              THEN Y      ELSE NULL   END))__________________________________________________________________________

In the above expression, the "-infinity" denotes the minimal value in the domain of T.C2. As in case 1 above, the objective of this transformation is that the derived table returns one tuple and if there is an index on S.C1, index or-ing can be exploited by the optimizer to access the table S.

Now, suppose that column T.C2 is nullable but S.C1 is non-null. The above quantified predicate can be transformed into a second simple predicate involving a singleton subquery:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMAX(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE (S.C1 >= (CASE WHEN Z IS NULL THEN -INFINITY      WHEN Z=0              THEN Y              ELSE NULL   END))__________________________________________________________________________
CASE 3: <ALL

For the case of transforming the quantified predicate "<ALL", the optimizer looks for a query of the general form:

SELECT S.*

FROM S

WHERE S.C1<ALL (SELECT T.C2 FROM T)

When both columns S.C1 and T.C2 are nullable, the quantified predicate can be transformed into a first simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMIN(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE ( (S.C1 IS NULL AND Z IS NULL)ORS.C1 <= (CASE WHEN Z IS NULL THEN +INFINITY      WHEN Z=0              THEN Y              ELSE NULL   END))AND(S.C1 < Y OR Z IS NULL)__________________________________________________________________________

In the above expression, the "+infinity" denotes the maximal value in the domain of T.C2. As in the case 1, the objective of this transformation is that the derived table returns one tuple and if there is an index on S.C1, index or-ing can be exploited by the optimizer to access the table S.

Now, suppose that column T.C2 is nullable but S.C1 is non-null. The above quantified predicate can be transformed into a second simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMIN(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE (S.C1 <= (CASE WHEN Z IS NULL THEN +INFINITY      WHEN Z=0              THEN Y              ELSE NULL   END))AND(S.C1 < Y OR Z IS NULL)__________________________________________________________________________
CASE 4: <=ALL

For the case of transforming the quantified predicate "<=ALL", the optimizer looks for a query of the general form:

SELECT S.*

FROM S

WHERE S.C1<=ALL (SELECT T.C2 FROM T)

When both columns S.C1 and T.C2 are nullable, the quantified predicate can be transformed into a first simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMIN(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE ( (S.C1 IS NULL AND Z IS NULL)ORS.C1 <= (CASE WHEN Z IS NULL THEN +INFINITY      WHEN Z=0              THEN Y              ELSE NULL   END))__________________________________________________________________________

In the above expression, the "+infinity" denotes the maximal value in the domain of T.C2. As in case 1, the objective of this transformation is that the derived table returns one tuple and if there is an index on S.C1, index or-ing can be exploited by the optimizer to access the table S.

Now, suppose that column T.C2 is nullable, but S.C1 is non-null. The above quantified predicate can be transformed into a second simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMIN(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE (S.C1 <= (CASE WHEN Z IS NULL THEN +INFINITY      WHEN Z=0              THEN Y              ELSE NULL   END))__________________________________________________________________________
CASE 5: =ALL

For the case of transforming the quantified predicate "=ALL", the optimizer looks for a query of the general form:

SELECT S.*

FROM S

WHERE S.C1=ALL (SELECT T.C2 FROM T)

When both columns S.C1 and T.C2 are nullable, the quantified predicate can be transformed into a first simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMIN(C2) AS X,MAX(C2) AS Y,MAX(CASE WHEN C2 IS NULL THEN 1 ELSE 0 END) AS ZFROM T) AS DTWHERE ( (S.C1 IS NULL AND Z IS NULL)ORS.C1 <= (CASE WHEN Z IS NULL THEN +INFINITY      WHEN X=Y AND Z=0 THEN X              ELSE NULL   END))  AND((S.C1 IS NULL AND Z IS NULL)  OR  S.C1 >= (CASE WHEN Z IS NULL   THEN -INFINITY      WHEN X=Y AND Z=0 THEN Y              ELSE NULL   END))__________________________________________________________________________

In the above expression, "-infinity" and "+infinity" denote the minimal and maximal value in the domain of T.C2 respectively.

Now, suppose that column T.C2 is nullable but S.C1 is non-null. The above quantified predicate can be transformed into a second simple predicate involving a singleton subquery of the form:

__________________________________________________________________________SELECT S.*FROM S, (SELECTMIN(C2) AS X,MAX(C2) AS Y,FROM T) AS DTWHERE ((S.C1 IS NULL AND Y IS NULL)ORS.C1 <= (CASE WHEN X=Y AND Y IS NULL      THEN +INFINITY     WHEN X=Y      THEN X      ELSE NULL   END))  AND((S.C1 IS NULL AND Z IS NULL)  OR  S.C1 >= (CASE WHEN X=Z AND Y IS NULL      THEN -INFINITY     WHEN X=Y      THEN Y      ELSE NULL   END))__________________________________________________________________________

From these transformations, it can be seen that determining column nullability becomes increasing more important as it could simplify query transformations.

"IS NOT NULL" PREDICATE PUSHDOWN

Consider a table T and a descending index on one of its column C:

CREATE TABLE T (. . . , C INTEGER, . . .)

CREATE INDEX I on T(C DESC)

Suppose the following query, which returns the maximal value of column C, is executed against the table T:

SELECT MAX (C)

FROM T

Note that the MAX(C) function in the query returns the maximal non-null value. Suppose that null has the highest collating sequence value and the optimizer selects a plan that goes through the index I, wherein the execution sequence is that the index I is scanned in descending order, each data value is fetched, and each fetched data value is tested to determine whether it is a null value, and then the fetch-and-test procedure is repeated until the first non-null value is found. The above repeated procedure could be avoided if the information that null values would not contribute to the query answer set was available.

Now, consider the same query in an equivalent form:

SELECT MAX (C)

FROM T

WHERE C IS NOT NULL

The difference is that an additional predicate "C IS NOT NULL" is introduced. With this predicate and the column nullability propagation methods, it can be determined that the input to the group by operation is non-null. Thus, the optimizer can generate a plan that accesses the index I and applies this predicate while accessing the index, as described in Patricia G. Selinger, D. Chamberlin, M. Astrahan, Raymond Lorie, and T. Price, "Access Path Selection in a Relational Database Management System," Proceedings of ACM SIGMOD '79 International Conference on Management of Data, 1979, pp. 23-34, and incorporated by reference herein. The effect is that only the first qualified non-null record is returned, and hence the fetch-and-test procedure is avoided.

This is called the one-fetch property. In general, the more null values that are stored in the column C, the faster the query is executed, because all null values are skipped during the index scan. Moreover, the above observation leads to some valuable query transformations, as described in more detail below.

FIG. 11 is a flowchart that illustrates the steps required for transforming one-fetch queries according to the present invention when the null value collates high. Block 1102 is a decision block that determines if the query involves a GROUP BY operation on a derived table or a base table without any GROUP BY columns. If so, then control is transferred to block 1104. Otherwise, control is transferred to block 1106, which ends the routine and returns to FIG. 4.

Block 1104 is a decision block that determines if there is only one aggregation function MAX in the SELECT clause of the query. If so, then control is transferred to block 1108. Otherwise, control is transferred to block 1106.

Block 1108 is a decision block that determines if the column in the aggregate function MAX has a descending index with a null value as the highest collating value. If so, then control is transferred to block 1110. Otherwise, control is transferred to block 1106.

Block 1110 is a decision block that determines if the column in the aggregate function MAX is nullable. If so, then control is transferred to block 1112. Otherwise, control is transferred to block 1106.

Block 1112 generates a new predicate (as a Boolean factor) for the column of the form "COLUMN IS NOT NULL" in the WHERE clause of the query.

FIG. 12 is a flowchart that illustrates the steps required for transforming one-fetch queries according to the present invention when the null value collates low. Block 1202 is a decision block that determines if the query involves a GROUP BY operation on a derived table or a base table without any GROUP BY columns. If so, then control is transferred to block 1204. Otherwise, control is transferred to block 1206, which ends the routine and returns to FIG. 4.

Block 1204 is a decision block that determines if there is only one aggregation function MIN in the SELECT clause of the query. If so, then control is transferred to block 1208. Otherwise, control is transferred to block 1206.

Block 1208 is a decision block that determines if the column in the aggregate function MIN has an ascending index with a null value as the lowest collating value. If so, then control is transferred to block 1210. Otherwise, control is transferred to block 1206.

Block 1210 is a decision block that determines if the column in the aggregate function MIN is nullable. If so, then control is transferred to block 1212. Otherwise, control is transferred to block 1206.

Block 1212 generates a new predicate (as a Boolean factor) for the column of the form "COLUMN IS NOT NULL" in the WHERE clause of the query.

INTERSECT-TO-JOIN TRANSFORMATION

It has been pointed out in [PIRA92], incorporated by reference herein, that an intersect-distinct query can be transformed into a join via a sequence of query rewrite transformations. For example, suppose there are two tables, T and S, and an intersect-distinct query is applied against the tables as follows:

SELECT C1 FROM T

INTERSECT DISTINCT

SELECT C2 FROM S

The techniques discussed in [PIRA92], incorporated by reference herein, transform the above intersect-distinct query into:

SELECT DISTINCT C1

FROM T, S

WHERE T.C1=S.C2

Unfortunately, these transformations are invalid in presence of null values, because the equality predicate in the WHERE clause is false in the presence of null values, whereas the INTERSECT query provides that a null value equals a null value. That is, without the presence of a variant of the equality predicate "=" where null equals null, the INTERSECT query cannot be transformed into a join. Because of this reason, determining column nullability becomes more important.

FIG. 13 is a flowchart that illustrates the steps required for transforming INTERSECT-ALL queries according to the present invention by determining if the output of an INTERSECT-ALL query is distinct, i.e., if the output is duplicate-free. Block 1302 is a decision block that determines if any column of an operand forms a key, wherein the key is a minimal set of columns that can uniquely determine a tuple in the base or derived table. If so, then control is transferred to block 1304, which identifies the output column as being duplicate-free, i.e., "head.distinct=IS". Otherwise, control is transferred to block 1306, which ends the routine and returns to FIG. 4.

For example, suppose the following tables, index and query are provided:

CREATE TABLE T (C1 INTEGER)

CREATE TABLE S (C2 INTEGER)

CREATE UNIQUE INDEX I ON T(C1)

SELECT C1 FROM T

INTERSECT ALL

SELECT C2 FROM S

The output of the above INTERSECT-ALL query is guaranteed to be duplicate-free, because of the unique index on T.C1. Note that the transformed query cannot be written using a valid SQL syntax, although its QGM representation is valid.

FIG. 14 is a flowchart that illustrates the steps required for performing intersect-to-join query transformations (which do not rely on a "null=null" operator in the WHERE clause). Block 1402 is a decision block that determines if the output of an INTERSECT query is duplicate-free. If so, the control is transferred to block 1404. Otherwise, control is transferred to block 1406, which ends the routine and returns to FIG. 4. Block 1404 is a decision block that determines, for each output column, if at least one of its corresponding input columns is non-null. If so, then control is transferred to block 1408, which transforms the INTERSECT query into a join query (i.e., SELECT-FROM-WHERE). Otherwise, control is transferred to block 1406.

An example of this transformation is provided below. First, the tables T and S are defined:

CREATE TABLE T (C1 INTEGER NOT NULL, C2 INTEGER)

CREATE TABLE S (C1 INTEGER, C2 INTEGER)

A first example query against T and S follows:

SELECT C1 FROM T

INTERSECT DISTINCT

SELECT C1 FROM S

A second example query against T and S follows:

SELECT C2 FROM T

INTERSECT DISTINCT

SELECT C2 FROM S

The first example query can be transformed into a join, because column C1 from table T is non-null. However, the second example query cannot be transformed into a join, because all input columns are nullable.

CONCLUSION

This concludes the description of the preferred embodiment of the invention. The following describes some alternative embodiments for accomplishing the present invention. For example, any type of computer, such as a mainframe, minicomputer, or personal computer, could be used with the present invention. In addition, any software program adhering (either partially or entirely) to the SQL language could benefit from the present invention.

In summary, the present invention discloses a method and apparatus for optimizing SQL queries by propagating and exploiting column nullability. The present invention identifies and propagates column nullability using a three-valued logic, wherein a column of a derived or base table can be null (when all data values in the column are null), non-null (when the column does not contain any null values), or nullable (when the column may or may not contain null values). The column nullability information is propagated through intermediate evaluations of SQL queries (e.g., selection, projection, join, intersection, except, union and group by operations). The present invention exploits the column nullability information to optimize query operations through transformations, and thus significantly enhance query evaluation performance. In one aspect of the present invention, quantified predicates (such as ">ALL") are transformed into simple predicates involving singleton subqueries so that indexing can be exploited during plan optimization. In another aspect of the present invention, "is not null" predicates are generated and pushed for certain aggregate queries. In still another aspect of the present invention, INTERSECT operations are transformed into joins, wherein the transformation does not rely on the use of any variant of the equality "=" operator that deals with null values. The end result is that query execution plans produced using the present invention can significantly enhance the performance of the queries.

The foregoing description of the preferred embodiment of the invention has been presented for the purposes of illustration and description. It is not intended to be exhaustive or to limit the invention to the precise form disclosed. Many modifications and variations are possible in light of the above teaching. It is intended that the scope of the invention be limited not by this detailed description, but rather by the claims appended hereto.

Patent Citations
Cited PatentFiling datePublication dateApplicantTitle
US4769772 *Feb 28, 1985Sep 6, 1988Honeywell Bull, Inc.Automated query optimization method using both global and parallel local optimizations for materialization access planning for distributed databases
US4829427 *May 25, 1984May 9, 1989Data General CorporationDatabase query code generation and optimization based on the cost of alternate access methods
US5001477 *Nov 3, 1988Mar 19, 1991International Business MachinesEncoding variable length and null data while preserving sort sequence
US5091852 *Jan 25, 1989Feb 25, 1992Hitachi, Ltd.System for optimizing query processing in a relational database
US5367675 *Dec 13, 1991Nov 22, 1994International Business Machines CorporationComputer automated system and method for optimizing the processing of a query in a relational database system by merging subqueries with the query
US5495605 *Sep 29, 1993Feb 27, 1996Bull S.A.Method to help in optimizing a query from a relational data base management system, and resultant method of syntactical analysis
US5511186 *Nov 18, 1992Apr 23, 1996Mdl Information Systems, Inc.System and methods for performing multi-source searches over heterogeneous databases
US5519859 *Nov 15, 1993May 21, 1996Grace; John A.Method and apparatus for automatic table selection and generation of structured query language instructions
Non-Patent Citations
Reference
1Date, C. J. & Darwen, Hugh., "Relational Database Management" Relational Database Writings 1989-1991, Part II, pp. 133-154.
2 *Date, C. J. & Darwen, Hugh., Relational Database Management Relational Database Writings 1989 1991 , Part II, pp. 133 154.
3Dayal et al., "An Extended Relational Algebra with Control Over Duplicate Elimination", Computer Corporation of America, Cambridge, MA, (US), ACM 1982, pp. 117-123.
4 *Dayal et al., An Extended Relational Algebra with Control Over Duplicate Elimination , Computer Corporation of America, Cambridge, MA, (US), ACM 1982, pp. 117 123.
5Dayal, Umeshwar, "Of Nests and Trees: A Unified Approach to Processing Queries that Contain Nested Subqueries, Aggregates, and Quantifiers", Computer Corporation of America, Cambridge, MA (US), Proceedings of the 13th VLDB Conference, 1987, pp. 197-208.
6 *Dayal, Umeshwar, Of Nests and Trees: A Unified Approach to Processing Queries that Contain Nested Subqueries, Aggregates, and Quantifiers , Computer Corporation of America, Cambridge, MA (US), Proceedings of the 13th VLDB Conference , 1987, pp. 197 208.
7Ganski et al., "Optimization of Nested SQL Queries Revisited", ACM, 1987, pp. 23-33.
8 *Ganski et al., Optimization of Nested SQL Queries Revisited , ACM , 1987, pp. 23 33.
9Haas et al., "Extensible Query Processing in Starburst", IBM Almaden Research Center, San Jose, CA (US), ACM 1989, pp. 377-388.
10 *Haas et al., Extensible Query Processing in Starburst , IBM Almaden Research Center, San Jose, CA (US), ACM 1989, pp. 377 388.
11Kim, Won, IBM Research "On Optimizing an SQL-Like Nested Query", ACM Transactions on Database Systems, vol. 7, No. 3, Sep. 1982, pp. 443-469.
12 *Kim, Won, IBM Research On Optimizing an SQL Like Nested Query , ACM Transactions on Database Systems , vol. 7, No. 3, Sep. 1982, pp. 443 469.
13Levy et al., "Query Optimization by Predicate Move-Around", Proceedings of the 20th VLDB Conference, Santiago, Chile, 1994, pp. 96-107.
14 *Levy et al., Query Optimization by Predicate Move Around , Proceedings of the 20th VLDB Conference, Santiago, Chile, 1994, pp. 96 107.
15Lohman et al., "Research Report--Query Processing in R*", IBM Research Laboratory, San Jose, CA (US) IBM Research Division, Apr. 1984, pp. 1-33.
16 *Lohman et al., Research Report Query Processing in R* , IBM Research Laboratory, San Jose, CA (US) IBM Research Division, Apr. 1984, pp. 1 33.
17Paulley et al., "Exploiting Uniqueness in Query Optimization", Department of computer Science, University of Waterloo, Ontario, Canada, IEEE 1994, pp. 68-79.
18 *Paulley et al., Exploiting Uniqueness in Query Optimization , Department of computer Science, University of Waterloo, Ontario, Canada, IEEE 1994, pp. 68 79.
19Pirahesh et al., "Extensible/Rule Based Query Rewrite Optimization in Starburst", IBM Almaden Research Center, San Jose, CA (US), ACM Sigmond, Jun. 1992, pp. 39-48.
20 *Pirahesh et al., Extensible/Rule Based Query Rewrite Optimization in Starburst , IBM Almaden Research Center, San Jose, CA (US), ACM Sigmond , Jun. 1992, pp. 39 48.
21Selinger et al, "Access Path Selection in a Relational Database Management System", IBM Research Division, San Jose, CA (US), ACM, 1979, pp. 23-34.
22 *Selinger et al, Access Path Selection in a Relational Database Management System , IBM Research Division, San Jose, CA (US), ACM , 1979, pp. 23 34.
23Yan et al, "Performing Broup-By before Join", Proc. The 10th International Conference on Data Engineering, 14-18 Feb. 94, pp. 89-100.
24 *Yan et al, Performing Broup By before Join , Proc. The 10th International Conference on Data Engineering, 14 18 Feb. 94, pp. 89 100.
Referenced by
Citing PatentFiling datePublication dateApplicantTitle
US5774692 *Oct 5, 1995Jun 30, 1998International Business Machines CorporationOuter quantifiers in object-oriented queries and views of database systems
US5802512 *Mar 12, 1996Sep 1, 1998Oracle CorporationStorage, replay and error detection of user-defined queries
US5819256 *Nov 20, 1996Oct 6, 1998Oracle CorporationMethod and apparatus for processing count statements in a database system
US5822747 *Aug 23, 1996Oct 13, 1998Tandem Computers, Inc.System and method for optimizing database queries
US5864840 *Jun 30, 1997Jan 26, 1999International Business Machines CorporationEvaluation of existential and universal subquery in a relational database management system for increased efficiency
US5905982 *Apr 4, 1997May 18, 1999International Business Machines CorporationHandling null values in SQL queries over object-oriented data
US5963936 *Jun 30, 1997Oct 5, 1999International Business Machines CorporationQuery processing system that computes GROUPING SETS, ROLLUP, and CUBE with a reduced number of GROUP BYs in a query graph model
US6003024 *Nov 5, 1997Dec 14, 1999Amazon. ComSystem and method for selecting rows from dimensional databases
US6032143 *Dec 4, 1998Feb 29, 2000International Business Machines CorporationEvaluation of existential and universal subquery in a relational database management system for increased efficiency
US6061689 *May 6, 1997May 9, 2000International Business Machines CorporationObject aggregation representation of relational database rows having nontraditional datatypes
US6192357 *Nov 3, 1998Feb 20, 2001Platinum Technology, Inc.Method and apparatus for optimizing query generation by selectively utilizing attributes or key values
US6233573Dec 13, 1999May 15, 2001Amazon.Com, Inc.System and method for selecting rows from dimensional databases having temporal dimention
US6240406 *Aug 31, 1998May 29, 2001The Trustees Of The University Of PennsylvaniaSystem and method for optimizing queries with aggregates and collection conversions
US6324535Dec 2, 1999Nov 27, 2001Amazon.Com, Inc.Method and apparatus for producing sequenced queries
US6339770 *Aug 12, 1999Jan 15, 2002International Business Machines CorporationQuery simplification and optimization involving eliminating grouping column from group by operation corresponds to group by item that is constant
US6385603Jun 14, 1999May 7, 2002International Business Machines CorporationJoined table expression optimization by query transformation
US6438542Aug 30, 1999Aug 20, 2002International Business Machines CorporationMethod of optimally determining lossless joins
US6442543Dec 18, 2000Aug 27, 2002Amazon.Com, Inc.Method and apparatus for changing temporal database information
US6493701 *Jul 5, 2001Dec 10, 2002Sybase, Inc.Database system with methodogy providing faster N-ary nested loop joins
US6526403 *Dec 17, 1999Feb 25, 2003International Business Machines CorporationMethod, computer program product, and system for rewriting database queries in a heterogenous environment
US6564212Dec 13, 2000May 13, 2003Lafayette SoftwareMethod of processing queries in a database system, and database system and software product for implementing such method
US6571233Dec 6, 2000May 27, 2003International Business Machines CorporationOptimization of SQL queries using filtering predicates
US6574623Aug 15, 2000Jun 3, 2003International Business Machines CorporationQuery transformation and simplification for group by queries with rollup/grouping sets in relational database management systems
US6601063May 23, 2002Jul 29, 2003International Business Machines CorporationMethod of optimally determining lossless joins
US6604095Sep 21, 1999Aug 5, 2003International Business Machines CorporationMethod, system, program, and data structure for pivoting columns in a database table
US6711563Mar 5, 2001Mar 23, 2004Lafayette Software Inc.Methods of organizing data and processing queries in a database system, and database system and software product for implementing such methods
US6748389Sep 21, 1999Jun 8, 2004International Business Machines CorporationMethod, system, and program for inverting columns in a database table
US6789085 *Jun 7, 2001Sep 7, 2004Microsoft CorporationSystem and method for maintaining a user's state within a database table and for accommodating null values
US6920443Sep 21, 1999Jul 19, 2005International Business Machines, CorporationMethod, system, program, and data structure for transforming database tables
US6947934Feb 16, 2000Sep 20, 2005International Business Machines CorporationAggregate predicates and search in a database management system
US6965888Sep 21, 1999Nov 15, 2005International Business Machines CorporationMethod, system, program, and data structure for cleaning a database table using a look-up table
US6965894Mar 22, 2002Nov 15, 2005International Business Machines CorporationEfficient implementation of an index structure for multi-column bi-directional searches
US6983291May 16, 2000Jan 3, 2006International Business Machines CorporationIncremental maintenance of aggregated and join summary tables
US7120638Sep 21, 1999Oct 10, 2006International Business Machines CorporationMethod, system, program, and data structure for cleaning a database table
US7143107Jun 26, 2003Nov 28, 2006Microsoft CorporationReporting engine for data warehouse
US7246124Dec 13, 2000Jul 17, 2007Virtual Key GraphMethods of encoding and combining integer lists in a computer system, and computer software product for implementing such methods
US7249151Jul 26, 2005Jul 24, 2007International Business Machines CorporationMethod, system, program, and data structure for cleaning a database table
US7263512Apr 2, 2002Aug 28, 2007Mcgoveran David OAccessing and updating views and relations in a relational database
US7437613 *Jan 30, 2004Oct 14, 2008Intel CorporationProtecting an operating system kernel from third party drivers
US7464084Jan 30, 2006Dec 9, 2008International Business Machines CorporationMethod for performing an inexact query transformation in a heterogeneous environment
US7480649Jul 6, 2005Jan 20, 2009International Business Machines CorporationMethod for efficient evaluation of index screening predicates
US7542962 *Apr 30, 2003Jun 2, 2009International Business Machines CorporationInformation retrieval method for optimizing queries having maximum or minimum function aggregation predicates
US7603347Sep 16, 2004Oct 13, 2009Oracle International CorporationMechanism for efficiently evaluating operator trees
US7620664 *Dec 31, 2006Nov 17, 2009Mcgoveran David OComputer-implemented method for translating among multiple representations and storage structures
US7702622Jun 29, 2007Apr 20, 2010Microsoft CorporationAdvanced techniques for SQL generation of performancepoint business rules
US7702627Mar 8, 2007Apr 20, 2010Oracle International CorporationEfficient interaction among cost-based transformations
US7716656Aug 30, 2005May 11, 2010Microsoft CorporationNullable and late binding
US7739219 *Sep 8, 2006Jun 15, 2010Oracle International CorporationTechniques of optimizing queries using NULL expression analysis
US7802180Oct 6, 2005Sep 21, 2010Oracle International CorporationTechniques for serialization of instances of the XQuery data model
US7814042Aug 17, 2004Oct 12, 2010Oracle International CorporationSelecting candidate queries
US7814067Jan 12, 2005Oct 12, 2010Oracle International CorporationAsynchronous actions using flashback
US7814091 *Sep 27, 2005Oct 12, 2010Oracle International CorporationMulti-tiered query processing techniques for minus and intersect operators
US7856462Oct 28, 2008Dec 21, 2010International Business Machines CorporationSystem and computer program product for performing an inexact query transformation in a heterogeneous environment
US7865515 *Aug 28, 2006Jan 4, 2011Microsoft CorporationServer side bucketization of parameterized queries
US7877379Sep 30, 2005Jan 25, 2011Oracle International CorporationDelaying evaluation of expensive expressions in a query
US7945562Mar 8, 2007May 17, 2011Oracle International CorporationJoin predicate push-down optimizations
US7979440Nov 21, 2008Jul 12, 2011International Business Machines CorporationSystem and article of manufacture for efficient evaluation of index screening predicates
US8020144Jun 29, 2007Sep 13, 2011Microsoft CorporationMetadata-based application deployment
US8069193Mar 28, 2001Nov 29, 2011International Business Machines CorporationMethod and system for utilizing a generic scalar function to allow a column function to operate on row data
US8140521 *Mar 15, 2004Mar 20, 2012Teradata Us, Inc.Performing SQL query optimization by simplifying sub-expressions
US8200604Nov 1, 2007Jun 12, 2012Microsoft CorporationMulti-platform business calculation rule language and execution environment
US8438152Oct 29, 2007May 7, 2013Oracle International CorporationTechniques for bushy tree execution plans for snowstorm schema
US8676865 *May 20, 2008Mar 18, 2014International Business Machines CorporationAdaptive lazy merging
US20090292714 *May 20, 2008Nov 26, 2009International Business Machines CorporationAdaptive lazy merging
EP1388793A2 *Dec 4, 2002Feb 11, 2004Joint Technology CorporationMethod for transforming SQL queries
WO2007027366A1 *Aug 4, 2006Mar 8, 2007Microsoft CorpNullable and late binding
WO2009055496A2 *Oct 22, 2008Apr 30, 2009Microsoft CorpCompressing null columns in rows of the tabular data stream protocol
Classifications
U.S. Classification1/1, 707/999.005
International ClassificationG06F17/30
Cooperative ClassificationG06F17/30454, Y10S707/99935, G06F17/30463
European ClassificationG06F17/30S4P3T2, G06F17/30S4P3T5
Legal Events
DateCodeEventDescription
Feb 17, 2009FPExpired due to failure to pay maintenance fee
Effective date: 20081231
Dec 31, 2008LAPSLapse for failure to pay maintenance fees
Jul 7, 2008REMIMaintenance fee reminder mailed
Jan 20, 2004FPAYFee payment
Year of fee payment: 8
Jan 5, 2000FPAYFee payment
Year of fee payment: 4
Jul 8, 1997CCCertificate of correction
May 17, 1995ASAssignment
Owner name: INTERNATIONAL BUSINESS MACHINES CORPORATION, NEW Y
Free format text: ASSIGNMENT OF ASSIGNORS INTEREST;ASSIGNORS:LEUNG, TING YU;PIRAHESH, MIR HAMID;JOU, MICHELLE MEI-CHIOU;AND OTHERS;REEL/FRAME:007615/0773
Effective date: 19950428