Twisting the Original Select TopLink Batch-Attribute Reading

9-50 Oracle Fusion Middleware Users Guide for Technology Adapters Master Column1 column2 ….. column100 Master1 ... Detail Detail Column1 column2 Detail1 ... Detail2 ... Detail100 ... In this example, 300 column values are returned as shown: columns in master + columns in detail x details per master = 100 + 2 x 100 = 300 With one query for all tables, the result set appears, as shown in the following example: Master Detail Column1 Column2 ... Column100 Column1 Column2 Master1 ... Detail1 ... Master1 ... Detail2 ... Master1 ... Detail100 ... Note that, in the case of one query for all tables, 10,200 column values are returned in a single result set, versus 300 in two result sets, as shown here: columns in master + columns in detail x details per master = 100 + 2 x 100 = 10,200 This can have a serious drain on network traffic and computation because 97 percent of the data returned is duplicate data. Also, if the master had two related tables detail1 and detail2 and there were 100 each in each master, then the number of column values returned would be over 10 million per master row. In general, you can use the following simple formula to estimate the relative cost of returning all rows in a single result set: Master columns + Detail1 columns + Detail2 columns + ... x Detail1s per Master x Detail2s per Master x ... bloat = ___________________________________________________________ Master columns + Detail1 columns X Detail1s per Master + Detail2 columns X Detail2s per Master + ... Note that for 1-1 relationships, this value is always 1, and if in the same example each master had two columns only and the details had 100 columns instead, and each master had only 3 or 4 details each, then the bloat would be 2 + 100 x 4 408 bloat = ____________ = ___________ ~= 1 2 + 100 x 4 402 Another disadvantage is that this setting could distort the meaning of the maxRows setting on an outbound select. Oracle JCA Adapter for Database 9-51 Configuration To configure, select Use Outer Joins to return a Single Result Set for both Master and Detail Tables on the Adapter Configuration Wizard - Define Selection Criteria page.

9.4.1.3.4 Comparison of the Methods Used for Querying over Multiple Tables

On the surface, returning a single result set looks best 1 query, followed by batch attribute reading altering the select statement: 2 queries, and finally by default relationship reading n + 1 queries. However, there are several pitfalls to both of the more advanced options, as explained below: Altering User-Defined SQL If you specify customhybrid SQL, the TopLink cannot alter that SQL string to build the details select. For this reason, you must avoid using hybrid SQL and build selects using the wizards visual expression builder as often as possible. Show Me the SQL The additional queries executed by TopLink in both, the default and the batch attribute reading cases can be somewhat of a mystery to users. For this reason, the raw SQL shown to users in the Adapter Configuration Wizard assumes returning a single result set, to make things clearer and also to improve manageability. Note: When you create a SQL query such as the following by using the TopLink Expression Builder, the result may not be as expected: SELECT DISTINCT t1.TABLE1_ID, t1.COLUMN_A FROM TABLE2 t0, TABLE1 t1 WHERE t0.STATUS = 1 AND t0.TABLE1_ID = t1.TABLE1_ID The expected result for this query is that only rows with Table 1s and their owned Table 2s with status = 1 be returned. However, what this query actually translates to is table 1s, where any of its table 2s have status = 1, resulting in the return of table 1s that match the selection criteria, and ALL of the table 2s they own, including those with other statuses, whether or not their statuses =1. The DISTINCT keyword ensures the table 1s are not repeated and the join happens across table 2. The misunderstanding happens in the way Toplink works. Through the Expression Builder, you can only specify a selection criteria for Table 1 and have no control over the Table 2s they own, this part is automatically done. However, you can get the expected result by using either of the following two approaches: 1. Query directly for table 2 using the selection criteria of status = 1, that is, do not go through table 1 and get the table 2s they own. 2. Use direct custom SQL, as shown in the following example: SELECT TABLE1.TABLE1_ID, TABLE1.COLUMN_A, TABLE2.STATUS FROM TABLE2, TABLE1 WHERE TABLE2.STATUS=1 AND TABLE1.TABLE1_ID = TABLE2.TABLE1_ID