[Tips] Inner join: just don't use it
[image unavailable: Picture 3]
8 years later and Servoy is still populating the relation join type with "inner join" as the default. Even Servoy's Wiki says to use left outer join instead:
[image unavailable: Picture 4]
This is a bad thing because there is one hidden side effect of inner joins that can lead to a number of unexpected results. Namely, if you do a sort on the related records, parent records with no related records are removed from their foundset.
Examples:
1 Search and sort removes records
Let's say a search of companies.name = %Factory% returns 10 records. Then you do a a sort on companies_to_contacts.name_last and 8 records are now showing instead of 10. The two records that go missing don't have any companies_to_contacts records.
2 Search doesn't return expected records
Let's say a search of companies.name = %Factory% and companies_to_contacts.name_last = %Smith% (an OR search) returns 2 records less than you're expecting. The two records that should have been returned by the first match were removed from the result set because they didn't have any child records as defined by the inner join on the second match.
3 Extended search removes records
Doing an extended search (ie, foundset.search(false, false)) using related search criteria removes valid records from the starting foundset. Same reason as #2. This is a bug in my opinion as an extended search should never remove records from the foundset you are starting from.
Summary
The inner join unexpected behaviors are even more noticeable now that we have the ability to add default sorts to relations. So there is a good chance just about anyone now will stumble across something they don't understand.
Unless you have a good case for using inner joins, don't!
Comments (5)