Sunday, May 17, 2009

Avoiding Outer Joins

Solution 1:

Oracle “Normal” joins are used to retrieve records from both the tables which satisfy join conditions. But sometimes we need records from the table which satisfy the join condition and also those records which are not present in another table. In this situation we go for Outer Joins.

If the tables being joined are having millions of records then using outer joins on such tables takes lots of time for execution. Even though the query is returning 10 or 100 records, the execution time taken by a query will be huge.

Please have a look at a simple query mentioned below. The query provides department names for, employees with ID between 1 and 100 and also those employees which are not allocated to any department.

select e.ID,e.NAME, d.NAME

from dept d, emp e

where d.id (+) = e.DEPT_ID

and e.id between 1 AND 100

In the above example we need only one column, department name, from DEPT table. But for this we need to perform a costly outer join on DEPT table. In such situations it is possible to avoid outer joins. Let’s have a look at the example given below,

select e.ID,e.NAME,(select NAME from dept d where d.id = e.DEPT_ID)

from emp e

where e.id between 1 AND 100

In this solution we are removing the DEPT table from the FROM clause of main query. The DEPT table is used as a sub query to retrieve values for department name column. This sub query will be executed only 100 times. If the DEPT (ID) column is indexed then the query on DEPT table will be faster.

Above solution gives positive results when,

  1. Only one column is being selected from the huge INNER table
  2. The query is joining tables with millions of records
  3. Query return very few records as an output

Solution 2:

In case of normalized database we need to join multiple tables and some times we need to use outer joins. These queries take lots of time for retrieving 10 or 100 records.

Please see the example mentioned below. The query is trying to retrieve all the transaction between dates 01-Jan-2009 and 10-Jan-2009. For each demat transaction the query provides details for Beneficiary account, Depository participant account and settlement account involved in the transaction. The demat transaction details are stored in the DEMAT_TRANSACTION table and demat account details are stored in DEMAT_ACCOUNT table. DEMAT_ACCOUNT represents multiple accounts involved in a transaction and stores one record per account.

SELECT demat_trn.transaction_id,

benacct.party_name "Beneficiary Account",

dpacct.party_name "Depository Participant Party",

settleacct.part_name "Settlement Account"

FROM demat_transaction demat_trn,

demat_account benacct,

demat_account dpacct,

demat_account settleacct

WHERE demat_trn.transaction_id = benacct.transaction_id(+)

AND demat_trn.transaction_id = dpacct.transaction_id(+)

AND demat_trn.transaction_id = settleacct.transaction_id(+)

AND demat_transaction.transaction_date BETWEEN

TO_DATE ('01012009', 'DDMMYYYY')

AND TO_DATE ('10012009', 'DDMMYYYY')

In this example we can avoid outer joins by denormalizing the DEMAT_TRANSACTION and DEMAT_ACCOUNT tables. If there are maximum 3 accounts involved in the demat transaction, then the DEMAT_TRANSACTION table should be altered to accommodate details for all the three accounts for each transaction. After denormalization the final query will look like as mentioned below,

SELECT demat_trn.transaction_id,

benacct.party_name "Beneficiary Account",

dpacct.party_name "Depository Participant Party",

settleacct.part_name "Settlement Account"

FROM demat_transaction demat_trn

WHERE demat_trn.transaction_date BETWEEN

TO_DATE ('01012009', 'DDMMYYYY')

AND TO_DATE ('10012009', 'DDMMYYYY')

Since the denormalized query is fetching records from only one table, hence this query will definitely be executed faster than the normalized query.

Conclusion:

Outer joins are very costly when the tables being joined are very big and having millions of records. Depending on queries output it is possible to easily avoid the outer joins using Sub queries or denormalizing the tables involved in the Join.

15 Tips for New Employees

  1. Don’t be a know-it-all
    By jumping into things, older and more experienced colleagues will perceive you as arrogant and you could lose your respect.
  2. Appreciate others
    Saying “thank you” to your boss and other colleagues for their help and guidance will have long-term positive effects.
  3. Give in something extra
    Volunteer for extra tasks and assignments to show your commitment, loyalty and diligence.
  4. Listen before you speak
    Try to understand and grasp every piece of information and make sure you understand everything before you give in your contributions.
  5. Understand the culture of the organization
    Observe how people interact with others and how things are done. Find out who is most admired and/or most influential and why.
  6. Learn more about yourself
    Your first job gives you a chance to figure out what you are best at and what kind of work you enjoy the most.
  7. Take time out for developing new skills
    Look out for opportunities of career development within and outside the organization and acquire new skills for your professional growth.
  8. Dress professionally
    Observe how people are generally dressed in the organization and follow that dress code.
  9. Don’t feel shy to ask questions
    The embarrassment will be much more if you end up messing up your work because you did not take clear instructions. So ask questions and take down notes.
  10. Be punctual
    Never be late for work and keep a good attendance. Try to come in before the official work time and stay a little late.
  11. Stay away from office gossip
    Don’t get involved in the office politics and gossip. This is especially important in the first few months.
  12. Get involved in the informal events
    Joining the sports club or other social events organized by the firm will give you a chance to develop cordial relationships with your co-workers.
  13. Keep your boss informed
    Give your boss updates of all your activities at work. This will increase your reliability and will show how responsible you are.
  14. Remember people’s names
    As soon as possible try to remember names of people you come into contact with.
  15. Don’t complain or criticize
    You might not be happy with the entire management system but wait before you start to give suggestions