sql - Performance comparison on Join with OR on 2 predicates vs 2 separate joins 1 predicate each -
what's performance impact on using join using 2 predicates or on on clause so:
select gs.guitartype,gd,guitarcolor prod.guitars gs left join prod.guitar_detail gd on (gs.guitarid = gd.guitarid or gs.guitarid = gd.guitarcatnum)
vs. this:
select gs.guitartype,gd,guitarcolor prod.guitars gs left join prod.guitar_detail gd on gs.guitarid = gd.guitarid left join prod.guitar_detail gd2 on gs.guitarid = gd.guitarcatnum
couple caveats: have use left join can't use inner. i've ran both of queries , latter performs better.
also question, 2nd won't return more rows right? because they're both being joined on same table, should both preserve gs table right?
in first query have match twice? or why perform different second?
let me answer in reversed order.
also question, 2nd won't return more rows right? because they're both being joined on same table, should both preserve gs table right?
the queries different (the difference being in how nulls treated), , different execution times should expected. boils down how gd.guitarid , gd.guitarcatnum used.
a) if gd.guitarid set , gd.guitarcatnum null, queries return same data.
b) if gd.guitarid set , gd.guitarcatnum contains same value gd.guitarid, second query return duplicate rows.
c) if gd.guitarid null , gd.guitarcatnum set, queries return same number of rows, gd.guitarcolor returned null.
now, assuming case a), execution plans this:
case 1)
select gs.guitartype, gd.guitarcolor guitars gs left join guitar_detail gd on (gs.guitarid = gd.guitarid or gs.guitarid = gd.guitarcatnum) access plan: ----------- total cost: 18.3602 query degree: 1 rows return ( 1) cost i/o | 3 >nljoin ( 2) 18.3602 2 /-----+------\ 2 1.5 tbscan tbscan ( 3) ( 4) 8.99536 9.07676 1 1 | | 2 2 table: db2inst1 table: db2inst1 guitars guitar_detail q2 q1
case 2)
select gs.guitartype, gd.guitarcolor guitars gs left join guitar_detail gd on gs.guitarid = gd.guitarid left join guitar_detail gd2 on gs.guitarid = gd.guitarcatnum total cost: 27.2798 query degree: 1 rows return ( 1) cost i/o | 2 >nljoin ( 2) 27.2798 3 /--------------+---------------\ 2 1 hsjoin< nljoin ( 3) ( 6) 18.0326 9.01796 2 1 /-----+------\ /-----+------\ 2 2 0.5 2 tbscan tbscan tbscan tbscan ( 4) ( 5) ( 7) ( 8) 8.99536 8.99536 0.0226 8.99536 1 1 0 1 | | | | 2 2 1 2 table: db2inst1 table: db2inst1 tabfnc: sysibm table: db2inst1 guitar_detail guitars genrow guitar_detail q2 q1 q4 q6
hope helps.
Comments
Post a Comment