Question : Conditions in joins

Hi,
I have a rather theoretical/practical questions. When I use the SQL shown in the snippet I get database errors because the database is doing a full table scan, applying the function to each row and the function fails, because it tries to convert strings to numbers. There is an additional condition (the one used in the join) which would filter out those invalid rows.

If I put the join condition additional into the where I don't get database errors.

So do I build the SQL wrong?
Does the optimizer does to much what he shouldn't?
Which priority do the conditions in the JOIN have over the ones specified in the WHERE?
Code Snippet:
1:
2:
3:
4:
5:
6:
select distinct v0."LK" as "Lkey" from dt0 v0
 inner join dt0v v1 on (v1."Field"=103533 and v1."FK_LK"=v0."PK") 
 inner join ACLEntry acl30 on (acl30."FK_ACL"=v0."FK_ACL") 
WHERE ((acl30."FK_ACL"=v0."FK_ACL")) and ((((TO_NUMBER(v1."Value")=3.0))) and ((acl30."FK_Benutzer"=1 or acl30."FK_Gruppe" IN (6 , 1 , 2)) and acl30."Type"=\'r\') and v0."histtop"=\'Y\')  ORDER BY v0."LK" desc
 
WHERE (v1."Field"=103533 and v1."FK_LK"=v0."PK") AND ((acl30."FK_ACL"=v0."FK_ACL")) and ((((TO_NUMBER(v1."Value")=3.0))) and ((acl30."FK_Benutzer"=1 or acl30."FK_Gruppe" IN (6 , 1 , 2)) and acl30."Type"=\'r\') and v0."histtop"=\'Y\')  ORDER BY v0."LK" desc

Answer : Conditions in joins

You use SQL99 syntax.
Join condition is normally part of the WHERE clause.
It is a hard task to command the optimizer how to do the query.
One way to achive this is to use views or inline views (selects in the FROM clause).

This has the advantage that you will insist thejpin be done first
and only after that to restrict the result in the where clause.

SELECT some_columns
FROM (select   ... from ... where ...join here)
WHERE  .. .restrict the join  here
ORDER BY ....  ;
Random Solutions  
 
programming4us programming4us