apothen
0 pts. | Oct 21 2004 10:21AM GMT
The problem is not the subquery, that’s running over a table that has only about 1000 records. My question really is about how DB2/400 optimizes the query.
This is how I think the query optimizer should do:
Step 1) Run the subquery over the table with 500 records - should run really fast.
Step 2) Now run the main query using the result set - The table is indeed indexed by the field (element) in the where clouse.
What seems to be happenning is:
DB2/400 runs the subquery for each one of the records in the big table with 22 million records.
I know in SQL Server and Oracle, we can look at the query execution path and make changes to it, if RDBMS did not pick the best route.
So my question really is, is there any such facility in DB2/400? or Does DB2/400 expect the queries to be structured in some other fashion that is specific to the platform?
BigKat
2540 pts. | Oct 21 2004 11:59AM GMT
SQL on the iSeries seems to have a couple of quirks from other platforms. It seems that in a statement using “where EVENT in (Select EVENT from INVEHH…” syntax, the “in” select is run for EVERY record in the outer statement.
Try using an inner join syntax. I’ve found it to help most of the time!
select EVENT, INUMBR, ISTORE, EVHYY, EVHMM, EVHDD
from INVEHD as a inner join (Select distinct EVENT from INVEHH where (EVHYY*10000 + EVHMM*100 + EVHDD)= 20040617) as b on a.EVENT = b.event
even better is to use EVHYY = 2004 and EVHMM = 06 and EVHDD = 17 instead of (EVHYY*10000 + EVHMM*100 + EVHDD)= 20040617 because SQL can make use of a logical over the date fields if they exist
if you can say that there can NEVER be duplicate EVENTs selected from the INVEHH file, you can eliminate the distinct from the statement. Not knowing the data relationships for sure I added the distinct just in case.






