To do a single file query, the system where the query was specified, the coordinator node, determines the nodes of the file to which to send the query. Those nodes run the query and return the queried records to the coordinator node.
All of the examples in this chapter use the following distributed files: DEPARTMENT and EMPLOYEE. The node group for these files consists of SYSA, SYSB, and SYSC. The data is partitioned on the department number.
CREATE TABLE DEPARTMENT (DEPTNO CHAR(3) NOT NULL, DEPTNAME VARCHAR(20) NOT NULL, MGRNO CHAR(6), ADMRDEPT CHAR(3) NOT NULL) IN NODGRP1 PARTITIONING KEY(DEPTNO)
|
CREATE TABLE EMPLOYEE (EMPNO CHAR(6) NOT NULL, FIRSTNME VARCHAR(12) NOT NULL, LASTNAME VARCHAR(15) NOT NULL, WORKDEPT CHAR(3) NOT NULL, JOB CHAR(8), SALARY DECIMAL(9,2)) IN NODGRP1 PARTITIONING KEY(WORKDEPT)
|
What follows is a query using the above defined distributed file EMPLOYEE, with index EMPIDX created over the field SALARY. The query is entered on SYSA.
SQL statement:
SELECT * FROM EMPLOYEE WHERE SALARY > 40000
OPNQRYF command:
OPNQRYF FILE((EMPLOYEE)) QRYSLT('SALARY > 40000')
In this case, SYSA sends the above query to all the nodes of EMPLOYEE, including SYSA. Each node runs the query and returns the records to SYSA. Because a distributed index exists on field SALARY of file EMPLOYEE, optimization that is done on each node decides whether to use the index.
In the next example, the query is specified on SYSA, but the query is sent to a subset of the nodes where the EMPLOYEE file exists. In this case, the query is run locally on SYSA only.
SQL statement:
SELECT * FROM EMPLOYEE WHERE WORKDEPT = 'A00'
OPNQRYF command:
OPNQRYF FILE((EMPLOYEE)) QRYSLT('WORKDEPT = 'A00')
The distributed query optimizer determines that there is an isolatable record selection, WORKDEPT = 'A00', involving the partitioning key, WORKDEPT, for this query. The optimizer hashes the value 'A00' and based on the hash value, finds the node at which all of the records satisfying this condition are located. In this case, all of the records satisfying this condition are on SYSA, thus the query is sent only to that node. Because the query originated on SYSA, the query is run locally on SYSA.
The following conditions subset the number of nodes at which a query runs: