Leila,
As I understand from your private message, the chosen join is a nested loop join, with the 88 million row table as outer table.
And his outer table is accessed through a matching index access (on one column, the condition EMPLID = :host-var).
So only a fraction of the 88 million rows are accessed, through the EMPLID index.
Next, the nested loop will access the inner table's 4-column index, apparently in an index-only way.
Over and over again, of course, but that might be an index look-aside access, which essentially means that the index is traversed just once.
And since it's only 15000 rows, that might be an efficient access (even if it's not using index look-aside).
Anyhow, since the optimizer chose this nested loop join with the large, partitioned table as outer table, this access path is "cheaper" than a join with the smaller table as outer table. If not, the optimizer would have chosen a different access path.
Still, of course, that subset (EMPLID = :hostvar) of the 88 million rows could be a substantial set of rows, and you still might prefer to force the optimizer to start from the smaller table for its join.
This can be done by "discouraging" the use of the EMPLID index on the large table as primary access.
Namely by changing the SELECT statement as follows:
Replace
... EMPLID = :host-var
by
... EMPLID = :host-var + 0
(I'm assuming EMPLID is numeric; if not do the following:
... EMPLID = :host-var || ''
)
------------------------------
Peter Vanroose
ABIS Training & Consulting
https://www.abis.be/html/enDB2Calendar.html------------------------------
Original Message:
Sent: Jun 17, 2022 10:00 PM
From: Leila Hosseini
Subject: Trying to change order to join tables by using Runstat
Hi
I have two table those contributing in one select statement.
One table has 15000 rows , other one has 88 million rows and has 15 partitions,
Both tables have proper indices those can be used in this query.
The issue is Db2 optimizer using wrong order to join the tables. It starts with bigger table and scanning small table for 88 million times! It uses unappreciated indices.
I have tried Runstat to make optimizer aware of indices, and also tables already have statistics.
In one of the Db2 subsystem RUNSTA INdex worked, to change order of joining tables.but for the rest of the Db2 subsystems not worked.All subsystems look the same.
I am confuse why Db2 behavior is different?
Thanks for any help
Regards
Leila