DB2 SQL – PART 1
Prepared by www.srinimf.com
Union Vs. Union All
• Union - will takes more time. Use it unless it is necessary
• Union All - we can use when to retain duplicates
Not Exists vs. Not in
• Not Exists – takes less CPU time
• NOT IN – Takes more CPU time. Before executing SQL it
creates result set
Reduce No of Tables in Join
• We can join 255 tables
• Practically it is less
• More joins takes more time to response
• Before JOIN, judge it how many tables really required. 5-7
tables is good
Denormalization
• Instead of joins use Denormalization objectively
• Denormalization eliminates, JOINS
Reduce No of Rows in Joins
SELECT E.LASTNAME, E.FIRSTNME FROM
DSN8610.DEPT D, DSN8610.EMP E
WHERE D.ADMRDEPT = 'D01'
AND D.DEPTNO = E.WORKDEPT
AND E.SEX = 'M'
AND E.SALARY >= 40000.00;
More qualifications takes more CPU time
Use Joins instead to write logic
• Always use JOINS in DB2 programs, instead of writing complex
logic
• Avoid using programming logic, unless it is necessary
Use JOINS instead of Sub-queries
• SELECT EMPNO, LASTNAME FROM DSN8610.EMP,
DSN8610.DEPT WHERE WORKDEPT = DEPTNO AND
DEPTNAME = 'PLANNING';
is usually more efficient than the subquery:
• SELECT EMPNO, LASTNAME FROM DSN8610.EMP
WHERE WORKDEPT IN (SELECT DEPTNO FROM
DSN8610.DEPT WHERE DEPTNAME = 'PLANNING');
Limit the Columns in Group By
• Always Reduce no of columns in Group by.
• It avoids more Sorts, and less CPU time.
Thank You
Visit my website
www.srinimf.com
Analytics
www.biganalytics.me

DB2 SQL-Part-1

  • 1.
    DB2 SQL –PART 1 Prepared by www.srinimf.com
  • 2.
    Union Vs. UnionAll • Union - will takes more time. Use it unless it is necessary • Union All - we can use when to retain duplicates
  • 3.
    Not Exists vs.Not in • Not Exists – takes less CPU time • NOT IN – Takes more CPU time. Before executing SQL it creates result set
  • 4.
    Reduce No ofTables in Join • We can join 255 tables • Practically it is less • More joins takes more time to response • Before JOIN, judge it how many tables really required. 5-7 tables is good
  • 5.
    Denormalization • Instead ofjoins use Denormalization objectively • Denormalization eliminates, JOINS
  • 6.
    Reduce No ofRows in Joins SELECT E.LASTNAME, E.FIRSTNME FROM DSN8610.DEPT D, DSN8610.EMP E WHERE D.ADMRDEPT = 'D01' AND D.DEPTNO = E.WORKDEPT AND E.SEX = 'M' AND E.SALARY >= 40000.00; More qualifications takes more CPU time
  • 7.
    Use Joins insteadto write logic • Always use JOINS in DB2 programs, instead of writing complex logic • Avoid using programming logic, unless it is necessary
  • 8.
    Use JOINS insteadof Sub-queries • SELECT EMPNO, LASTNAME FROM DSN8610.EMP, DSN8610.DEPT WHERE WORKDEPT = DEPTNO AND DEPTNAME = 'PLANNING'; is usually more efficient than the subquery: • SELECT EMPNO, LASTNAME FROM DSN8610.EMP WHERE WORKDEPT IN (SELECT DEPTNO FROM DSN8610.DEPT WHERE DEPTNAME = 'PLANNING');
  • 9.
    Limit the Columnsin Group By • Always Reduce no of columns in Group by. • It avoids more Sorts, and less CPU time.
  • 10.
    Thank You Visit mywebsite www.srinimf.com Analytics www.biganalytics.me