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 Figure 62 shows a possible query plan for this query.
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 Figure 63 shows.
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 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.deptnoEnterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]