US20090012945A1 - System for executing a query having multiple distinct key columns - Google Patents

System for executing a query having multiple distinct key columns Download PDF

Info

Publication number
US20090012945A1
US20090012945A1 US12/212,431 US21243108A US2009012945A1 US 20090012945 A1 US20090012945 A1 US 20090012945A1 US 21243108 A US21243108 A US 21243108A US 2009012945 A1 US2009012945 A1 US 2009012945A1
Authority
US
United States
Prior art keywords
distinct
query
data
sort
module
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
US12/212,431
Other versions
US8041726B2 (en
Inventor
Yao-Ching Chen
You-Chin Fuh
Allan B. Lebovitz
Li-Mey Lee
Fen-Ling Lin
Lee-Chin Hsu Liu
Yun Wang
Binghua Zhen
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Airbnb Inc
Original Assignee
International Business Machines Corp
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by International Business Machines Corp filed Critical International Business Machines Corp
Priority to US12/212,431 priority Critical patent/US8041726B2/en
Publication of US20090012945A1 publication Critical patent/US20090012945A1/en
Application granted granted Critical
Publication of US8041726B2 publication Critical patent/US8041726B2/en
Assigned to Airbnb, Inc. reassignment Airbnb, Inc. ASSIGNMENT OF ASSIGNORS INTEREST (SEE DOCUMENT FOR DETAILS). Assignors: INTERNATIONAL BUSINESS MACHINES CORPORATION
Expired - Fee Related legal-status Critical Current
Adjusted expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/28Databases characterised by their database models, e.g. relational or object models
    • G06F16/284Relational databases
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y10TECHNICAL SUBJECTS COVERED BY FORMER USPC
    • Y10STECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y10S707/00Data processing: database and file management or data structures
    • Y10S707/99931Database or file accessing
    • Y10S707/99933Query processing, i.e. searching
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y10TECHNICAL SUBJECTS COVERED BY FORMER USPC
    • Y10STECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y10S707/00Data processing: database and file management or data structures
    • Y10S707/99931Database or file accessing
    • Y10S707/99937Sorting

Definitions

  • the present invention relates to database management systems, and in particular, to a system for executing a query having multiple DISTINCT key columns.
  • Databases are computerized information storage and retrieval systems.
  • a Relational Database Management System (RDBMS) is a database management system (DBMS) which uses relational techniques for storing and retrieving data. Relational databases are organized into tables which consist of rows and columns of data. The rows are formally called tuples or records.
  • a database will typically have many tables and each table will typically have multiple tuples and multiple columns.
  • the tables are typically stored on direct access storage devices (DASD), such as magnetic or optical disk drives for semi-permanent storage.
  • DASD direct access storage devices
  • SQL Structured Query Language
  • ANSI American National Standards Institute
  • ISO International Standards Organization
  • 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 and COBOL. SQL allows the user to manipulate the data.
  • DEPT is a key, which designates the column upon which the DISTINCT operation will be applied.
  • Execution of the above query returns the set of unique departments from the table DATA_TABLE. Even if a particular department number appears in fifty rows of the table, it will only appear once in the result set of the query.
  • DISTINCT operations are frequently associated with aggregatation operations, such as AVG, COUNT, and SUM. Oftentimes, it is desirable to perform multiple DISTINCT operations for multiple columns in a table.
  • most current DBMSs restrict a user to only one DISTINCT key in the SELECT clause in any given query. In order to perform multiple DISTINCT operations, a single query is not allowed. Rather, the user must compose and submit separate queries to the DBMS for each one of the multiple DISTINCT keys. This leads to inefficiencies for the user, who is required to write multiple queries, and for the DBMS because multiple queries must be executed. Each query is associated with a cost of going to the DASD and retrieving the requested information. This cost is measured in computing resources and time expended.
  • Each table expression is then executed in the same manner as would two separate queries.
  • the result sets are then merged and returned to the user.
  • the present invention discloses a system and computer readable medium for executing a query to access data stored in a database, wherein the query includes multiple DISTINCT key columns.
  • the method and system of the present invention includes providing a first DISTINCT operation on a first DISTINCT key, storing data fetched from the first DISTINCT operation in a master workfile if more than one sort process is needed to execute the query, and utilizing the master workfile to perform subsequent DISTINCT operations for the other DISTINCT keys.
  • DISTINCT key in a SELECT clause for a query is allowed. This enhancement is accomplished by performing multiple sorts on multiple DISTINCT columns. As a result, when multiple DISTINCT column values need to be processed, only one query is required, and there is no need to rewrite the query to perform multiple queries for each DISTINCT key column.
  • FIG. 1 is a block diagram illustrating an exemplary computer hardware environment that could be used in accordance with the present invention.
  • FIG. 2 is a block diagram illustrating the Multiple Distinct Key system in accordance with a preferred embodiment of the present invention
  • FIG. 3 is a flowchart illustrating a process according to a preferred embodiment of the present invention.
  • the present invention relates to executing a query having multiple DISTINCT key columns.
  • the following description is presented to enable one of ordinary skill in the art to make and use the invention and is provided in the context of a patent application and its requirements.
  • Various modifications to the preferred embodiments and the generic principles and features described herein will be readily apparent to those skilled in the art.
  • the present invention is not intended to be limited to the embodiments shown but is to be accorded the widest scope consistent with the principles and features described herein.
  • FIG. 1 illustrates an exemplary computer hardware environment that could be used in accordance with the present invention.
  • a computer system 102 is comprised of one or more processors connected to one or more data storage devices 104 and 106 that store one or more relational databases, such as a fixed or hard disk drive, a floppy disk drive, a CDROM drive, a tape drive, or other device.
  • relational databases such as a fixed or hard disk drive, a floppy disk drive, a CDROM drive, a tape drive, or other device.
  • Operators of the computer system 102 use a standard operator interface 108 , such as IMS/DB/DC®, CICS®, TSO®, OS/390®, ODBC®, 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.
  • these queries conform to the Structured Query Language (SQL) standard, and invoke functions performed by Relational DataBase Management System (RDBMS) software.
  • SQL Structured Query Language
  • RDBMS Relational DataBase Management System
  • the RDBMS software comprises the DB2® product offered by IBM for the OS/390® operating system.
  • IBM for the OS/390® operating system.
  • the present invention has application program to any RDBMS software, whether or not the RDBMS software uses SQL.
  • the DB2® system for the OS/390® operating system includes three major components: the Internal Resource Lock Manager (IRLM) 110 , the Systems Services module 112 , and the Database Services module 114 .
  • the IRLM 110 handles locking services for the DB2® system, which treats data as a shared resource, thereby allowing any number of users to access the same data simultaneously. 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.
  • the Database Services module 114 contains several submodules, including the Relational Database System (RDS) 116 , the Data Manager 118 , the Buffer Manager 120 , the Multiple Distinct Key System 124 , and other components 122 such as an SQL compiler/interpreter. These submodules support the functions of the SQL language, i.e. definition, access control, interpretation, compilation, database retrieval, and update of user and system data.
  • the Multiple Distinct Key system 124 works in conjunction with the other submodules.
  • the Multiple Distinct Key system 124 includes fetch and sort programs that are used to perform the DISTINCT operations.
  • the present invention is generally implemented using SQL statements executed under the control of the Database Services module 114 .
  • the Database Services module 114 retrieves or receives the SQL statements, wherein the SQL statements are generally stored in a text file on the data storage devices 104 and 106 or are interactively entered into the computer system 102 by an operator sitting at a monitor 126 via operator interface 108 .
  • the Database Services module 114 then derives or synthesizes instructions from the SQL statements for execution by the computer system 102 .
  • the RDBMS software, the SQL statements, and the instructions derived therefrom are all tangibly embodied in a computer-readable medium, e.g., one or more of the data storage devices 104 and 106 .
  • the RDBMS software, the SQL statements, and the instructions derived therefrom are all comprised of instructions which, when read and executed by the computer system 102 , causes the computer system 102 to perform the steps necessary to implement and/or use the present invention.
  • the RDBMS software, the SQL statements, and the instructions derived therefrom may be loaded from the data storage devices 104 and 106 into a memory of the computer system 102 for use during actual operations.
  • FIG. 1 is not intended to limit the present invention. Indeed, those skilled in the art will recognize that other alternative hardware environments may be used without departing from the scope of the present invention.
  • the Multiple Distinct Key (MDK) system 124 of the present invention provides an efficient technique for executing a query having multiple DISTINCT key columns.
  • the MDK system 124 recognizes when multiple DISTINCT keys exist, and invokes a SORT process to perform multiple sorts on the multiple DISTINCT key columns.
  • the SORT process executes a first DISTINCT operation on a first DISTINCT key.
  • the SORT process creates a temporary master workfile to store the data retrieved for the first DISTINCT operation, and sorts the data on the first DISTINCT key. On all subsequent DISTINCT operations, the input data is fetched from the temporary master workfile, thereby eliminating the need to access repeatedly the database.
  • FIG. 2 is a block diagram illustrating the MDK system 124 in accordance to a preferred embodiment of the present invention.
  • FIG. 3 is a flowchart illustrating a process for executing a query having multiple DISTINCT key columns.
  • the MDK system 124 includes a capture module 204 and a sort module 206 , which work in conjunction with one another to process a query having multiple DISTINCT keys.
  • the capture module 204 receives the query submitted via step 302 .
  • the query is optimized prior to its submission to the capture module 204 in order to minimize the number of DISTINCT keys. Optimization can be performed by any number of optimization techniques known to those skilled in the art.
  • the capture module 204 will notify the sort module that multiple DISTINCT keys exist in step 304 .
  • the capture module 204 notifies the sort module 206 by identifying the DISTINCT keys and, if applicable, a GROUP BY key(s), storing that information in a control block 202 , and transmitting the control block 202 to the sort module 206 .
  • the sort module 206 initializes the environment for processing the DISTINCT keys.
  • the sort module 206 determines the total number of sorts that the sort module 206 will perform based on the DISTINCT keys. In general, the number of sorts will be equal to the number of DISTINCT keys. Nevertheless, under certain circumstances, one sort can be performed for more than one DISTINCT key, as is well known to those skilled in the art. Thus, the number of sorts is not necessarily equal to the number of DISTINCT keys. This becomes relevant later in the process when the sort module 206 determines whether to create a temporary workfile. Also at this time, if a GROUP BY key exists, the sort module 206 creates a structure to evaluate each DISTINCT group, e.g., by designating a different keysize for each DISTINCT key field.
  • the sort module 206 also determines which set functions will be performed for each DISTINCT sort. In a preferred embodiment, the sort module 206 will evaluate most non-DISTINCT set functions in the first DISTINCT sort. Then, for subsequent DISTINCT sorts, the sort module 206 will not need to evaluate non-DISTINCT set functions.
  • the following example illustrates this preferred embodiment. Suppose the following query is submitted:
  • the sort module 206 determines that two DISTINCT keys exist (A 1 , A 2 ) and that two sorts will be performed to evaluate two DISTINCT groups (A 5 +A 1 ; A 5 +A 2 ). During the first sort routine, the sort module 206 will sort for A 5 +A 1 and evaluate the following set functions: COUNT (DISTINCT A 1 ), COUNT (A 4 ), COUNT (A 9 ), SUM (A 6 ), AVG (A 7 ). Note that the set function COUNT (A 2 ) will not be evaluated because A 2 is a DISTINCT key column. The sort module 206 will evaluate COUNT (A 2 ) when it sorts for A 5 +A 2 .
  • the sort module 206 performs a first DISTINCT operation for a first DISTINCT key. While performing the first DISTINCT operation, the sort module 206 invokes a fetch module 207 to retrieve data from the database 104 ( FIG. 1 ), e.g., rows from one or more tables in the database. If the sort module 206 has determined that more than one sort will be performed (in step 306 ), the sort module 206 stores the data in a temporary master workfile 208 as the data is retrieved, via step 308 . The sort module 206 then sorts the retrieved data according to the first DISTINCT key, and stores the sorted data in a result subset 210 a corresponding to the first DISTINCT key, via step 310 .
  • step 312 the sort module 206 performs a next DISTINCT operation for another DISTINCT key.
  • the fetch module 207 reads the data from the master workfile 208 , and the sort module 206 sorts the data according to the corresponding DISTINCT key, in step 314 .
  • the sort module 206 stores the sorted data in a result subset 210 b corresponding to the DISTINCT key.
  • the sort module 206 determines, in step 318 , whether more DISTINCT keys exist. If more exist, then the process described in steps 312 - 318 are repeated.
  • a merge module 211 collects the result subsets 210 a - 210 N and generates a final result set 212 , in step 320 .
  • the merge module 211 generates the final result set 212 by reading out rows of each result subset 210 a - 210 N and placing the data back into an original field position corresponding to an original table. After each row is read out of each result subset 210 a - 210 N and merged into its appropriate position, the merge module 211 will write the data to the final result set 212 .
  • the final result set 212 is then returned to the Data Services module 114 for normal processing in step 322 .
  • step 302 submitting the query
  • step 304 notifying the sort module 206
  • step 304 the remainder of the process
  • steps 306 - 322 is executed at runtime during a SORT process.
  • the method and system of the present invention saves processing resources and time by fetching data from the database only once during the first DISTINCT operation. On all subsequent DISTINCT operations, the data is fetched from the master workfile, where the data is already stored in its proper format. Thus, the processing needed to perform all of the DISTINCT operations in accordance with the method and system of the present invention is much less than that needed to perform multiple queries for each DISTINCT key.
  • more than one DISTINCT key in a SELECT clause for a query is allowed. This enhancement is accomplished by performing multiple sorts on multiple DISTINCT columns. As a result, when multiple DISTINCT column values need to be processed, only one query is required, and there is no need to rewrite the query to perform multiple queries for each DISTINCT column.

Abstract

A system and computer readable medium for executing a query to access data stored in a database, wherein the query includes a plurality of DISTINCT keys, is disclosed. The system and computer readable medium includes a capture module for identifying each of the plurality of DISTINCT keys in the query and a sort module coupled to the capture module for determining if more than one sort is needed to execute the query, performing a first DISTINCT operation on a first DISTINCT key of the plurality of DISTINCT keys, storing data fetched from the first DISTINCT operation in a master workfile only if more than one sort process is needed to execute the query, and utilizing the master workfile to perform subsequent DISTINCT operations on the other of the plurality of DISTINCT keys.

Description

    CROSS-REFERENCE TO RELATED APPLICATIONS
  • Under 35 USC § 120, this application is a continuation application and claims the benefit of priority to U.S. patent application Ser. No. 10/320,909, filed Dec. 17, 2002, entitled “Method and System for Executing a Query Having Multiple Distinct Key Columns”, all of which is incorporated herein by reference.
  • FIELD OF THE INVENTION
  • The present invention relates to database management systems, and in particular, to a system for executing a query having multiple DISTINCT key columns.
  • BACKGROUND OF THE INVENTION
  • Databases are computerized information storage and retrieval systems. A Relational Database Management System (RDBMS) is a database management system (DBMS) which uses relational techniques for storing and retrieving data. Relational databases are organized into tables which consist of rows and columns of data. The rows are formally called tuples or records. A database will typically have many tables and each table will typically have multiple tuples and multiple columns. The tables are typically stored on direct access storage devices (DASD), such as magnetic or optical disk drives for semi-permanent storage.
  • The Structured Query Language (SQL) interface has evolved into a standard language for RDBMS software and has been adopted as such by both the American National Standards Institute (ANSI) and the International Standards Organization (ISO). 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 and COBOL. SQL allows the user to manipulate the data.
  • One such operation is known as a DISTINCT operation, which causes the elimination of duplicate values in specified sets of data. In the SQL query
  • SELECT (DISTINCT DEPT)
  • FROM DATA_TABLE
  • DEPT is a key, which designates the column upon which the DISTINCT operation will be applied. Execution of the above query returns the set of unique departments from the table DATA_TABLE. Even if a particular department number appears in fifty rows of the table, it will only appear once in the result set of the query.
  • DISTINCT operations are frequently associated with aggregatation operations, such as AVG, COUNT, and SUM. Oftentimes, it is desirable to perform multiple DISTINCT operations for multiple columns in a table. Unfortunately, most current DBMSs restrict a user to only one DISTINCT key in the SELECT clause in any given query. In order to perform multiple DISTINCT operations, a single query is not allowed. Rather, the user must compose and submit separate queries to the DBMS for each one of the multiple DISTINCT keys. This leads to inefficiencies for the user, who is required to write multiple queries, and for the DBMS because multiple queries must be executed. Each query is associated with a cost of going to the DASD and retrieving the requested information. This cost is measured in computing resources and time expended.
  • One approach to resolving this problem has been implemented in DB2 UNO®, developed by International Business Machines of Armonk, N.Y. Here, the user is allowed to specify multiple DISTINCT keys in a query, such as:
  • SELECT COUNT(DISTINCT EMPLOYEE), COUNT(DISTINCT DEPT)
  • FROM DATA_TABLE
  • GROUP BY (LAB_LOC)
  • This query is broken down and rewritten into two table expressions:
  • SELECT COUNT(DISTINCT EMPLOYEE)
  • FROM DATA_TABLE
  • GROUP BY (LAB_LOC);
  • SELECT COUNT(DISTINCT DEPT)
  • FROM DATA_TABLE
  • GROUP BY (LAB_LOC)
  • Each table expression is then executed in the same manner as would two separate queries. The result sets are then merged and returned to the user.
  • While the above described system allows the user to submit a query with multiple DISTINCT keys, it is still highly inefficient because it requires the system to execute multiple queries. As stated above, this burdens the system and increases the response time. Moreover, the query submitted by the user is broken down and rewritten during a pre-execution period known to those skilled in the art as a bind time. During bind time, execution parameters are determined and bound, i.e., saved, for use later during execution or runtime.
  • Thus, there exists a need for a method and system that allows for executing a query having multiple DISTINCT key columns. The method and system should be efficient and cost effective. The present invention addresses these needs.
  • SUMMARY OF THE INVENTION
  • The present invention discloses a system and computer readable medium for executing a query to access data stored in a database, wherein the query includes multiple DISTINCT key columns. The method and system of the present invention includes providing a first DISTINCT operation on a first DISTINCT key, storing data fetched from the first DISTINCT operation in a master workfile if more than one sort process is needed to execute the query, and utilizing the master workfile to perform subsequent DISTINCT operations for the other DISTINCT keys.
  • Through aspects of the system and computer readable medium of the present invention, more than one DISTINCT key in a SELECT clause for a query is allowed. This enhancement is accomplished by performing multiple sorts on multiple DISTINCT columns. As a result, when multiple DISTINCT column values need to be processed, only one query is required, and there is no need to rewrite the query to perform multiple queries for each DISTINCT key column.
  • BRIEF DESCRIPTION OF THE DRAWINGS
  • FIG. 1 is a block diagram illustrating an exemplary computer hardware environment that could be used in accordance with the present invention.
  • FIG. 2 is a block diagram illustrating the Multiple Distinct Key system in accordance with a preferred embodiment of the present invention
  • FIG. 3 is a flowchart illustrating a process according to a preferred embodiment of the present invention.
  • DETAILED DESCRIPTION
  • The present invention relates to executing a query having multiple DISTINCT key columns. The following description is presented to enable one of ordinary skill in the art to make and use the invention and is provided in the context of a patent application and its requirements. Various modifications to the preferred embodiments and the generic principles and features described herein will be readily apparent to those skilled in the art. Thus, the present invention is not intended to be limited to the embodiments shown but is to be accorded the widest scope consistent with the principles and features described herein.
  • FIG. 1 illustrates an exemplary computer hardware environment that could be used in accordance with the present invention. In the exemplary environment, a computer system 102 is comprised of one or more processors connected to one or more data storage devices 104 and 106 that store one or more relational databases, such as a fixed or hard disk drive, a floppy disk drive, a CDROM drive, a tape drive, or other device.
  • Operators of the computer system 102 use a standard operator interface 108, such as IMS/DB/DC®, CICS®, TSO®, OS/390®, ODBC®, 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 OS/390® operating system. Those skilled in the art will recognize, however, that the present invention has application program to any RDBMS software, whether or not the RDBMS software uses SQL.
  • As illustrated in FIG. 1, the DB2® system for the OS/390® operating system includes three major components: the Internal Resource Lock Manager (IRLM) 110, the Systems Services module 112, and the Database Services module 114. The IRLM 110 handles locking services for the DB2® system, which treats data as a shared resource, thereby allowing any number of users to access the same data simultaneously. 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® system 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, the Multiple Distinct Key System 124, and other components 122 such as an SQL compiler/interpreter. These submodules support the functions of the SQL language, i.e. definition, access control, interpretation, compilation, database retrieval, and update of user and system data. The Multiple Distinct Key system 124 works in conjunction with the other submodules. The Multiple Distinct Key system 124 includes fetch and sort programs that are used to perform the DISTINCT operations.
  • The present invention is generally implemented using SQL statements executed under the control of the Database Services module 114. The Database Services module 114 retrieves or receives the SQL statements, wherein the SQL statements are generally stored in a text file on the data storage devices 104 and 106 or are interactively entered into the computer system 102 by an operator sitting at a monitor 126 via operator interface 108. The Database Services module 114 then derives or synthesizes instructions from the SQL statements for execution by the computer system 102.
  • Generally, the RDBMS software, the SQL statements, and the instructions derived therefrom, are all tangibly embodied in a computer-readable medium, e.g., one or more of the data storage devices 104 and 106. Moreover, the RDBMS software, the SQL statements, and the instructions derived therefrom, are all comprised of instructions which, when read and executed by the computer system 102, causes the computer system 102 to perform the steps necessary to implement and/or use the present invention. Under control of an operating system, the RDBMS software, the SQL statements, and the instructions derived therefrom, may be loaded from the data storage devices 104 and 106 into a memory of the computer system 102 for use during actual operations.
  • Those skilled in the art will recognize that the exemplary environment illustrated in FIG. 1 is not intended to limit the present invention. Indeed, those skilled in the art will recognize that other alternative hardware environments may be used without departing from the scope of the present invention.
  • The Multiple Distinct Key (MDK) system 124 of the present invention provides an efficient technique for executing a query having multiple DISTINCT key columns. In particular, the MDK system 124 recognizes when multiple DISTINCT keys exist, and invokes a SORT process to perform multiple sorts on the multiple DISTINCT key columns. According to a preferred embodiment of the present invention, when the SORT process is notified of a query having multiple DISTINCT keys and the SORT process determines that more than one sort will be performed, the SORT process executes a first DISTINCT operation on a first DISTINCT key. As the data is fetched from the relevant table(s), the SORT process creates a temporary master workfile to store the data retrieved for the first DISTINCT operation, and sorts the data on the first DISTINCT key. On all subsequent DISTINCT operations, the input data is fetched from the temporary master workfile, thereby eliminating the need to access repeatedly the database. The method and system of the present invention will be described in more detail with reference to FIG. 2 and FIG. 3.
  • FIG. 2 is a block diagram illustrating the MDK system 124 in accordance to a preferred embodiment of the present invention. FIG. 3 is a flowchart illustrating a process for executing a query having multiple DISTINCT key columns. As is shown in FIG. 2, the MDK system 124 includes a capture module 204 and a sort module 206, which work in conjunction with one another to process a query having multiple DISTINCT keys. The capture module 204 receives the query submitted via step 302. In a preferred embodiment, the query is optimized prior to its submission to the capture module 204 in order to minimize the number of DISTINCT keys. Optimization can be performed by any number of optimization techniques known to those skilled in the art.
  • If the query has multiple DISTINCT keys, the capture module 204 will notify the sort module that multiple DISTINCT keys exist in step 304. In a preferred embodiment, the capture module 204 notifies the sort module 206 by identifying the DISTINCT keys and, if applicable, a GROUP BY key(s), storing that information in a control block 202, and transmitting the control block 202 to the sort module 206.
  • In step 306, once the sort module 206 is notified of the query, the sort module 206 initializes the environment for processing the DISTINCT keys. In a preferred embodiment, the sort module 206 determines the total number of sorts that the sort module 206 will perform based on the DISTINCT keys. In general, the number of sorts will be equal to the number of DISTINCT keys. Nevertheless, under certain circumstances, one sort can be performed for more than one DISTINCT key, as is well known to those skilled in the art. Thus, the number of sorts is not necessarily equal to the number of DISTINCT keys. This becomes relevant later in the process when the sort module 206 determines whether to create a temporary workfile. Also at this time, if a GROUP BY key exists, the sort module 206 creates a structure to evaluate each DISTINCT group, e.g., by designating a different keysize for each DISTINCT key field.
  • The sort module 206 also determines which set functions will be performed for each DISTINCT sort. In a preferred embodiment, the sort module 206 will evaluate most non-DISTINCT set functions in the first DISTINCT sort. Then, for subsequent DISTINCT sorts, the sort module 206 will not need to evaluate non-DISTINCT set functions. The following example illustrates this preferred embodiment. Suppose the following query is submitted:
  • SELECT COUNT (DISTINCT A1), COUNT (DISTINCT A2),
  • COUNT (A4), COUNT (A9), SUM (A6), AVG (A7), COUNT (A2)
  • FROM T1
  • GROUP BY A5
  • The sort module 206 determines that two DISTINCT keys exist (A1, A2) and that two sorts will be performed to evaluate two DISTINCT groups (A5+A1; A5+A2). During the first sort routine, the sort module 206 will sort for A5+A1 and evaluate the following set functions: COUNT (DISTINCT A1), COUNT (A4), COUNT (A9), SUM (A6), AVG (A7). Note that the set function COUNT (A2) will not be evaluated because A2 is a DISTINCT key column. The sort module 206 will evaluate COUNT (A2) when it sorts for A5+A2.
  • Referring again to FIGS. 2 and 3, after the sort module has initialized the environment, the sort module 206 performs a first DISTINCT operation for a first DISTINCT key. While performing the first DISTINCT operation, the sort module 206 invokes a fetch module 207 to retrieve data from the database 104 (FIG. 1), e.g., rows from one or more tables in the database. If the sort module 206 has determined that more than one sort will be performed (in step 306), the sort module 206 stores the data in a temporary master workfile 208 as the data is retrieved, via step 308. The sort module 206 then sorts the retrieved data according to the first DISTINCT key, and stores the sorted data in a result subset 210 a corresponding to the first DISTINCT key, via step 310.
  • In step 312, the sort module 206 performs a next DISTINCT operation for another DISTINCT key. Now, instead of going to the database 104, the fetch module 207 reads the data from the master workfile 208, and the sort module 206 sorts the data according to the corresponding DISTINCT key, in step 314. Next, in step 316, the sort module 206 stores the sorted data in a result subset 210 b corresponding to the DISTINCT key. The sort module 206 then determines, in step 318, whether more DISTINCT keys exist. If more exist, then the process described in steps 312-318 are repeated.
  • After the data has been sorted for each of the DISTINCT keys, a merge module 211 collects the result subsets 210 a-210 N and generates a final result set 212, in step 320. Preferably, the merge module 211 generates the final result set 212 by reading out rows of each result subset 210 a-210 N and placing the data back into an original field position corresponding to an original table. After each row is read out of each result subset 210 a-210 N and merged into its appropriate position, the merge module 211 will write the data to the final result set 212. The final result set 212 is then returned to the Data Services module 114 for normal processing in step 322.
  • In a preferred embodiment of the present invention, only two process steps, submitting the query (step 302) and notifying the sort module 206 (step 304), are performed at bind time. The remainder of the process (steps 306-322) is executed at runtime during a SORT process. By executing the bulk of the process at runtime, as opposed to bind time, performance is improved while minimizing any impact on other components in the RDBMS.
  • Moreover, the method and system of the present invention saves processing resources and time by fetching data from the database only once during the first DISTINCT operation. On all subsequent DISTINCT operations, the data is fetched from the master workfile, where the data is already stored in its proper format. Thus, the processing needed to perform all of the DISTINCT operations in accordance with the method and system of the present invention is much less than that needed to perform multiple queries for each DISTINCT key.
  • Through aspects of the method and system of the present invention, more than one DISTINCT key in a SELECT clause for a query is allowed. This enhancement is accomplished by performing multiple sorts on multiple DISTINCT columns. As a result, when multiple DISTINCT column values need to be processed, only one query is required, and there is no need to rewrite the query to perform multiple queries for each DISTINCT column.
  • Although the present invention has been described in accordance with the embodiments shown, one of ordinary skill in the art will readily recognize that there could be variations to the embodiments and those variations would be within the spirit and scope of the present invention. Accordingly, many modifications may be made by one of ordinary skill in the art without departing from the spirit and scope of the appended claims.

Claims (17)

1. A computer readable medium containing program instructions for executing a query to access data stored in a database, wherein the query includes a plurality of DISTINCT keys, the program instructions for:
(a) determining if more than one sort is needed to execute the query;
(b) performing a first DISTINCT operation on a first DISTINCT key of the plurality of DISTINCT keys;
(c) storing data from the first DISTINCT operation in a master workfile only if more than one sort is needed to execute the query; and
(d) utilizing the master workfile to perform subsequent DISTINCT operations on one or more other of the plurality of DISTINCT keys,
wherein the one or more subsequent DISTINCT operations are performed after completion of the first DISTINCT operation, each operation being controlled by the sort process.
2. The computer readable medium of claim 1, wherein the providing instruction (b) includes:
(b1) fetching data from at least one table in the database;
(b2) sorting the fetched data on a column designated by the first DISTINCT key; and
(b3) storing the sorted data in result subset associated with the first DISTINCT key.
3. The computer readable medium of claim 1, wherein the master workfile is a temporary workfile.
4. The computer readable medium of claim 1, wherein the utilizing instruction (d) includes:
(d1) identifying a next DISTINCT key;
(d2) fetching data stored in the master workfile;
(d3) sorting the fetched data on the next DISTINCT key; and
(d4) storing the sorted data in a result subset associated with the next DISTINCT key.
5. The computer readable medium of claim 4, wherein the utilizing instruction (d) further includes:
(d5) repeating steps (d1)-(d4) for each of the plurality of DISTINCT keys in the query.
6. The computer readable medium of claim 5, further comprising the instruction for:
(e) generating a final result set based on each result subset associated with each DISTINCT key.
7. The computer readable medium of claim 1, wherein the providing instruction (b) includes the instructions for:
(b1) optimizing the query; and
(b2) analyzing the query.
8. The computer readable medium of claim 7, wherein the analyzing instruction (b2) includes:
(b2i) identifying the plurality of DISTINCT keys; and
(b2ii) writing the plurality of DISTINCT keys to a control block, wherein the control block is used to execute the query.
9. The computer readable medium of claim 7, wherein the optimizing instruction (b1) and the analyzing instruction (b2) are performed during a bind time.
10. The computer readable medium of claim 1, wherein the providing instruction (b), the storing instruction (c) and the utilizing instruction (d) are executed by a SORT process at runtime.
11. A relational database management system for executing a query to access data stored in a database, wherein the query includes a plurality of DISTINCT keys, comprising:
a capture module for identifying each of the plurality of DISTINCT keys in the query; and
a sort module coupled to the capture module for determining if more than one sort is needed to execute the query, performing a first DISTINCT operation on a first DISTINCT key of the plurality of DISTINCT keys, storing data fetched from the first DISTINCT operation in a master workfile only if more than one sort process is needed to execute the query, and utilizing the master workfile to perform subsequent DISTINCT operations on the other of the plurality of DISTINCT keys.
12. The system of claim 11, wherein the sort module includes a fetch module for fetching data from at least one table in the database during the first DISTINCT operation.
13. The system of claim 12, wherein the fetch module fetches data in the master workfile for each subsequent DISTINCT operation.
14. The system of claim 13, wherein for each DISTINCT key, the sort module reads the data fetched by the fetch module, sorts the data on the DISTINCT key, and stores the sorted data for the DISTINCT key in an associated result subset.
15. The system of claim 14, wherein the sort module includes a merge module for merging each of the associated result subsets to form a final result set.
16. The system of claim 11, wherein the capture module is invoked during a bind time.
17. The system of claim 11, wherein the sort module is invoked during a runtime.
US12/212,431 2002-12-17 2008-09-17 System for executing a query having multiple distinct key columns Expired - Fee Related US8041726B2 (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
US12/212,431 US8041726B2 (en) 2002-12-17 2008-09-17 System for executing a query having multiple distinct key columns

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
US10/320,909 US7487140B2 (en) 2002-12-17 2002-12-17 Method for executing a query having multiple distinct key columns
US12/212,431 US8041726B2 (en) 2002-12-17 2008-09-17 System for executing a query having multiple distinct key columns

Related Parent Applications (1)

Application Number Title Priority Date Filing Date
US10/320,909 Continuation US7487140B2 (en) 2002-12-17 2002-12-17 Method for executing a query having multiple distinct key columns

Publications (2)

Publication Number Publication Date
US20090012945A1 true US20090012945A1 (en) 2009-01-08
US8041726B2 US8041726B2 (en) 2011-10-18

Family

ID=32506988

Family Applications (2)

Application Number Title Priority Date Filing Date
US10/320,909 Expired - Fee Related US7487140B2 (en) 2002-12-17 2002-12-17 Method for executing a query having multiple distinct key columns
US12/212,431 Expired - Fee Related US8041726B2 (en) 2002-12-17 2008-09-17 System for executing a query having multiple distinct key columns

Family Applications Before (1)

Application Number Title Priority Date Filing Date
US10/320,909 Expired - Fee Related US7487140B2 (en) 2002-12-17 2002-12-17 Method for executing a query having multiple distinct key columns

Country Status (1)

Country Link
US (2) US7487140B2 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US11860674B1 (en) * 2013-03-15 2024-01-02 Progress Software Corporation Query system

Families Citing this family (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8005868B2 (en) * 2008-03-07 2011-08-23 International Business Machines Corporation System and method for multiple distinct aggregate queries
US10162851B2 (en) 2010-04-19 2018-12-25 Salesforce.Com, Inc. Methods and systems for performing cross store joins in a multi-tenant store
US8447754B2 (en) * 2010-04-19 2013-05-21 Salesforce.Com, Inc. Methods and systems for optimizing queries in a multi-tenant store
US10176226B2 (en) * 2014-11-26 2019-01-08 Looker Data Sciences, Inc. Relation aware aggregation (RAA) on normalized datasets
US10503731B2 (en) 2016-10-11 2019-12-10 International Business Machines Corporation Efficient analysis of distinct aggregations

Citations (24)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US4595995A (en) * 1983-02-17 1986-06-17 At&T Bell Laboratories Sort circuit and method using multiple parallel sorts of the sorted items
US5027388A (en) * 1990-02-01 1991-06-25 Motorola, Inc. Method of selecting the most cost effective cellular service plan provided by cellular telephone resellers to multi-line customers
US5519860A (en) * 1992-01-31 1996-05-21 Syncsort Incorporated Central processor index sort followed by direct record sort and write by an intelligent control unit
US5548758A (en) * 1995-02-07 1996-08-20 International Business Machines Corporation Optimization of SQL queries using early-out join transformations of column-bound relational tables
US5615361A (en) * 1995-02-07 1997-03-25 International Business Machines Corporation Exploitation of uniqueness properties using a 1-tuple condition for the optimization of SQL queries
US5706495A (en) * 1996-05-07 1998-01-06 International Business Machines Corporation Encoded-vector indices for decision support and warehousing
US5713015A (en) * 1996-05-30 1998-01-27 International Business Machines Corporation Reordering of complex SQL queries involving GROUPBYs, joins, outer joins and full outer joins
US5806074A (en) * 1996-03-19 1998-09-08 Oracle Corporation Configurable conflict resolution in a computer implemented distributed database
US5822748A (en) * 1997-02-28 1998-10-13 Oracle Corporation Group by and distinct sort elimination using cost-based optimization
US5857180A (en) * 1993-09-27 1999-01-05 Oracle Corporation Method and apparatus for implementing parallel operations in a database management system
US5864842A (en) * 1995-10-23 1999-01-26 Ncr Corporation Optimization of SQL queries using hash star join operations
US6009265A (en) * 1994-02-25 1999-12-28 International Business Machines Corporation Program product for optimizing parallel processing of database queries
US6112198A (en) * 1997-06-30 2000-08-29 International Business Machines Corporation Optimization of data repartitioning during parallel query optimization
US6289334B1 (en) * 1994-01-31 2001-09-11 Sun Microsystems, Inc. Apparatus and method for decomposing database queries for database management system including multiprocessor digital data processing system
US6341277B1 (en) * 1998-11-17 2002-01-22 International Business Machines Corporation System and method for performance complex heterogeneous database queries using a single SQL expression
US6356888B1 (en) * 1999-06-18 2002-03-12 International Business Machines Corporation Utilize encoded vector indexes for distinct processing
US6430550B1 (en) * 1999-12-03 2002-08-06 Oracle Corporation Parallel distinct aggregates
US6438538B1 (en) * 1999-10-07 2002-08-20 International Business Machines Corporation Data replication in data warehousing scenarios
US6449605B1 (en) * 1998-12-28 2002-09-10 Oracle Corporation Using a materialized view to process a related query containing a one to many lossless join
US20020133507A1 (en) * 2001-03-16 2002-09-19 Iti, Inc. Collision avoidance in database replication systems
US20020152422A1 (en) * 2001-03-26 2002-10-17 Rahul Sharma Method and apparatus for managing replicated and migration capable session state for a Java platform
US20020188624A1 (en) * 2001-04-12 2002-12-12 William Landin Active control protocol for peer-to-peer database replication
US20030037029A1 (en) * 2001-08-15 2003-02-20 Iti, Inc. Synchronization of plural databases in a database replication system
US6529917B1 (en) * 2000-08-14 2003-03-04 Divine Technology Ventures System and method of synchronizing replicated data

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP2000172714A (en) 1998-12-01 2000-06-23 Hitachi Ltd Method for processing table connection

Patent Citations (27)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US4595995A (en) * 1983-02-17 1986-06-17 At&T Bell Laboratories Sort circuit and method using multiple parallel sorts of the sorted items
US5027388A (en) * 1990-02-01 1991-06-25 Motorola, Inc. Method of selecting the most cost effective cellular service plan provided by cellular telephone resellers to multi-line customers
US5519860A (en) * 1992-01-31 1996-05-21 Syncsort Incorporated Central processor index sort followed by direct record sort and write by an intelligent control unit
US5857180A (en) * 1993-09-27 1999-01-05 Oracle Corporation Method and apparatus for implementing parallel operations in a database management system
US6289334B1 (en) * 1994-01-31 2001-09-11 Sun Microsystems, Inc. Apparatus and method for decomposing database queries for database management system including multiprocessor digital data processing system
US6009265A (en) * 1994-02-25 1999-12-28 International Business Machines Corporation Program product for optimizing parallel processing of database queries
US5548758A (en) * 1995-02-07 1996-08-20 International Business Machines Corporation Optimization of SQL queries using early-out join transformations of column-bound relational tables
US5615361A (en) * 1995-02-07 1997-03-25 International Business Machines Corporation Exploitation of uniqueness properties using a 1-tuple condition for the optimization of SQL queries
US5864842A (en) * 1995-10-23 1999-01-26 Ncr Corporation Optimization of SQL queries using hash star join operations
US5806074A (en) * 1996-03-19 1998-09-08 Oracle Corporation Configurable conflict resolution in a computer implemented distributed database
US5706495A (en) * 1996-05-07 1998-01-06 International Business Machines Corporation Encoded-vector indices for decision support and warehousing
US5713015A (en) * 1996-05-30 1998-01-27 International Business Machines Corporation Reordering of complex SQL queries involving GROUPBYs, joins, outer joins and full outer joins
US5822748A (en) * 1997-02-28 1998-10-13 Oracle Corporation Group by and distinct sort elimination using cost-based optimization
US6112198A (en) * 1997-06-30 2000-08-29 International Business Machines Corporation Optimization of data repartitioning during parallel query optimization
US6341277B1 (en) * 1998-11-17 2002-01-22 International Business Machines Corporation System and method for performance complex heterogeneous database queries using a single SQL expression
US6449605B1 (en) * 1998-12-28 2002-09-10 Oracle Corporation Using a materialized view to process a related query containing a one to many lossless join
US6356888B1 (en) * 1999-06-18 2002-03-12 International Business Machines Corporation Utilize encoded vector indexes for distinct processing
US6438538B1 (en) * 1999-10-07 2002-08-20 International Business Machines Corporation Data replication in data warehousing scenarios
US6430550B1 (en) * 1999-12-03 2002-08-06 Oracle Corporation Parallel distinct aggregates
US6529917B1 (en) * 2000-08-14 2003-03-04 Divine Technology Ventures System and method of synchronizing replicated data
US20030158852A1 (en) * 2000-08-14 2003-08-21 William Zoltan System and method of synchronizing replicated data
US6668260B2 (en) * 2000-08-14 2003-12-23 Divine Technology Ventures System and method of synchronizing replicated data
US20020133507A1 (en) * 2001-03-16 2002-09-19 Iti, Inc. Collision avoidance in database replication systems
US20020152422A1 (en) * 2001-03-26 2002-10-17 Rahul Sharma Method and apparatus for managing replicated and migration capable session state for a Java platform
US20020188624A1 (en) * 2001-04-12 2002-12-12 William Landin Active control protocol for peer-to-peer database replication
US20030037029A1 (en) * 2001-08-15 2003-02-20 Iti, Inc. Synchronization of plural databases in a database replication system
US20030131027A1 (en) * 2001-08-15 2003-07-10 Iti, Inc. Synchronization of plural databases in a database replication system

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US11860674B1 (en) * 2013-03-15 2024-01-02 Progress Software Corporation Query system

Also Published As

Publication number Publication date
US7487140B2 (en) 2009-02-03
US20040117356A1 (en) 2004-06-17
US8041726B2 (en) 2011-10-18

Similar Documents

Publication Publication Date Title
US6249783B1 (en) Method and apparatus for efficiently executing built-in functions
US6219662B1 (en) Supporting database indexes based on a generalized B-tree index
US5615361A (en) Exploitation of uniqueness properties using a 1-tuple condition for the optimization of SQL queries
US6339770B1 (en) Query simplification and optimization involving eliminating grouping column from group by operation corresponds to group by item that is constant
US5548758A (en) Optimization of SQL queries using early-out join transformations of column-bound relational tables
US6343286B1 (en) Efficient technique to defer large object access with intermediate results
US5963933A (en) Efficient implementation of full outer join and anti-join
US6529896B1 (en) Method of optimizing a query having an existi subquery and a not-exists subquery
US6285996B1 (en) Run-time support for user-defined index ranges and index filters
US6505188B1 (en) Virtual join index for relational databases
US6560593B1 (en) Method and apparatus for viewing the effect of changes to an index for a database table on an optimization plan for a database query
JP3297403B2 (en) Method and apparatus for query optimization
US6606617B1 (en) Optimized technique for prefetching LOB table space pages
US8745033B2 (en) Database query optimization using index carryover to subset an index
US6581205B1 (en) Intelligent compilation of materialized view maintenance for query processing systems
US6098075A (en) Deferred referential integrity checking based on determining whether row at-a-time referential integrity checking would yield the same results as deferred integrity checking
US6289355B1 (en) Fast log apply
US7783625B2 (en) Using data in materialized query tables as a source for query optimization statistics
US6366902B1 (en) Using an epoch number to optimize access with rowid columns and direct row access
US6748377B1 (en) Facilitating query pushdown in a multi-tiered database environment
US7542962B2 (en) Information retrieval method for optimizing queries having maximum or minimum function aggregation predicates
US8041726B2 (en) System for executing a query having multiple distinct key columns
US6775676B1 (en) Defer dataset creation to improve system manageability for a database system
US20050050083A1 (en) Method, system, and article of manufacture for processing updates to insert operations
US6266663B1 (en) User-defined search using index exploitation

Legal Events

Date Code Title Description
REMI Maintenance fee reminder mailed
LAPS Lapse for failure to pay maintenance fees
STCH Information on status: patent discontinuation

Free format text: PATENT EXPIRED DUE TO NONPAYMENT OF MAINTENANCE FEES UNDER 37 CFR 1.362

FP Lapsed due to failure to pay maintenance fee

Effective date: 20151018

AS Assignment

Owner name: AIRBNB, INC., CALIFORNIA

Free format text: ASSIGNMENT OF ASSIGNORS INTEREST;ASSIGNOR:INTERNATIONAL BUSINESS MACHINES CORPORATION;REEL/FRAME:056427/0193

Effective date: 20210106