Example of directives that can alter a query plan
Directives can alter the query plan. You can use particular directives to force the optimizer to choose a particular type of query plan, for example one that uses hash joins and the order of tables as they appear in the query.
The following example shows how directives can alter the query plan.
Suppose you have the following query:
SELECT * FROM emp,job,dept
WHERE emp.location = 10
AND emp.jobno = job.jobno
AND emp.deptno = dept.deptno
AND dept.location = "DENVER";
Assume that the following indexes exist:
ix1: emp(empno,jobno,deptno,location)
ix2: job(jobno)
ix3: dept(location)
You run the query with SET EXPLAIN ON to display the query path
that the optimizer uses.
QUERY:
------
SELECT * FROM emp,job,dept
WHERE emp.location = "DENVER"
AND emp.jobno = job.jobno
AND emp.deptno = dept.deptno
AND dept.location = "DENVER"
Estimated Cost: 5
Estimated # of Rows Returned: 1
1) informix.emp: INDEX PATH
Filters: informix.emp.location = 'DENVER'
(1) Index Keys: empno jobno deptno location (Key-Only)
2) informix.dept: INDEX PATH
Filters: informix.dept.deptno = informix.emp.deptno
(1) Index Keys: location
Lower Index Filter: informix.dept.location = 'DENVER'
NESTED LOOP JOIN
3) informix.job: INDEX PATH
(1) Index Keys: jobno (Key-Only)
Lower Index Filter: informix.job.jobno = informix.emp.jobno
NESTED LOOP JOIN
The diagram in Possible
query plan without directives shows
a possible query plan for this query. The query plan has three levels
of information: (1) a nested-loop join, (2) an index scan on one table
and a nested-loop join, and (3) index scans on two other tables.
Perhaps you are concerned that using a nested-loop join might not
be the fastest method to execute this query. You also think that the
join order is not optimal. You can force the optimizer to choose a
hash join and order the tables in the query plan according to their
order in the query, so the optimizer uses the query plan that Possible
query plan with directives shows. This query plan
that has three levels of information: (1) a hash join, (2) an index
scan and a hash join, and (3) an index scan on two other tables.
To force the optimizer to choose the query plan that uses hash
joins and the order of tables shown in the query, use the directives
that the following partial SET EXPLAIN output shows:
QUERY:
------
SELECT {+ORDERED,
INDEX(emp ix1),
FULL(job),
USE_HASH(job /BUILD),
USE_HASH(dept /BUILD),
INDEX(dept ix3)}
* FROM emp,job,dept
WHERE emp.location = 1
AND emp.jobno = job.jobno
AND emp.deptno = dept.deptno
AND dept.location = "DENVER"
DIRECTIVES FOLLOWED:
ORDERED
INDEX ( emp ix1 )
FULL ( job )
USE_HASH ( job/BUILD )
USE_HASH ( dept/BUILD )
INDEX ( dept ix3 )
DIRECTIVES NOT FOLLOWED:
Estimated Cost: 7
Estimated # of Rows Returned: 1
1) informix.emp: INDEX PATH
Filters: informix.emp.location = 'DENVER'
(1) Index Keys: empno jobno deptno location (Key-Only)
2) informix.job: SEQUENTIAL SCAN
DYNAMIC HASH JOIN
Dynamic Hash Filters: informix.emp.jobno = informix.job.jobno
3) informix.dept: INDEX PATH
(1) Index Keys: location
Lower Index Filter: informix.dept.location = 'DENVER'
DYNAMIC HASH JOIN
Dynamic Hash Filters: informix.emp.deptno = informix.dept.deptno