SQL Performance
SQL Performance
11/10/2008
Topics
11/10/2008
Tips in SQL
Limit the use of sub queries; often they can be replaced with a JOIN. Use EXISTS and more specific conditions in the where clause instead of the DISTINCT clause to eliminate duplicates. Never Mix Datatypes.Use character and number appropriately according to the usage.
11/10/2008
SELECT ename FROM emp e,dept d WHERE e.deptno = d.deptno AND e.comm = 10 AND e.sal > 4000
11/10/2008
11/10/2008
Contd.
Ex :
SELECT * FROM EMP A WHERE DEPTNO NOT IN (SELECT DEPTNO FROM DEPT WHERE DEPTNO=A.DEPTNO) SELECT * FROM EMP A WHERE NOT EXISTS (SELECT 1 FROM DEPT WHERE DEPTNO=A.DEPTNO)
select * from ap_invoices_all where invoice_id not in(select invoice_id from ap_invoice_payments_all )
SELECT aia.invoice_id FROM ap_invoices_all aia, ap_invoice_payments_all aipa WHERE aia.invoice_id = aipa.invoice_id(+);
11/10/2008
Contd.
Ex :
select * from ap_invoices_all where invoice_id not in(select invoice_id from ap_invoice_payments_all )
SELECT * FROM ap_invoices_all aia WHERE NOT EXISTS (SELECT * FROM ap_invoice_payments_all WHERE invoice_id = aia.invoice_id)
select * from ap_invoices_all where invoice_id in (select invoice_id from ap_invoices_all minus select invoice_id from ap_invoice_payments_all )
11/10/2008
11/10/2008
Contd.
Ex :
select * from dept where dname like 'RESEARCH'
11/10/2008
10
Contd.
Ex : Ex :
SELECT E_Name FROM Employees_Norway UNION ALL SELECT E_Name FROM Employees_USA
11/10/2008
12
SELECT COUNT(*) FROM emp WHERE status = 'Y' AND emp_name LIKE 'SMITH%';
SELECT COUNT(*) FROM emp WHERE status = 'N' AND emp_name LIKE 'SMITH%';
Ex :
SELECT DEPTID, SUM(SALARY) FROM EMP GROUP BY DEPTID HAVING DEPTID = 100;
SELECT COUNT(DECODE(status, 'Y', 'X', NULL)) Y_count, COUNT(DECODE(status, 'N', 'X', NULL)) N_count FROM emp WHERE emp_name LIKE 'SMITH%';
SELECT DEPTID, SUM(SALARY) FROM EMP WHERE DEPTID = 100 GROUP BY DEPTID;
11/10/2008
13
Contd.
SELECT COUNT (*) FROM emp WHERE sal < 2000; SELECT COUNT (*) FROM emp WHERE sal BETWEEN 2000 AND 4000; SELECT COUNT (*) FROM emp WHERE sal>4000;
SELECT COUNT (CASE WHEN sal < 2000 THEN 1 ELSE null END) count1, COUNT (CASE WHEN sal BETWEEN 2001 AND 4000 THEN 1 ELSE null END) count2, COUNT (CASE WHEN sal > 4000 THEN 1 ELSE null END) count3 FROM emp;
11/10/2008
14
Contd.
Ex :
select cdl.* from cs_incidents_all_b ciab, jtf_tasks_b jtb, jtf_task_assignments jta, csf_debrief_headers cdh, csf_debrief_lines cdl where ciab.incident_id = jtb.source_object_id and jtb.task_id = jta.task_id and jta.task_assignment_id = cdh.task_assignment_id and cdh.debrief_header_id = cdl.debrief_header_id and ciab.incident_number = '1097852'
11/10/2008
16
Contd.
Operation SELECT STATEMENT Optimizer Mode=CHOOSE TABLE ACCESS BY INDEX ROWID NESTED LOOPS NESTED LOOPS NESTED LOOPS NESTED LOOPS TABLE ACCESS BY INDEX ROWID INDEX UNIQUE SCAN TABLE ACCESS FULL TABLE ACCESS BY INDEX ROWID INDEX RANGE SCAN TABLE ACCESS BY INDEX ROWID INDEX RANGE SCAN INDEX RANGE SCAN Object Name Rows Bytes 4 3 4 1 1 1 1 1 1 1 2 1 1 4 Cost Object Node In/Out 1134 414 3 776 1134 56 1131 42 1129 22 1126 12 4 2 10 1122 20 3 2 14 2 1 2 PStart PStop
CSF_DEBRIEF_LINES
11/10/2008
17
Contd.
Ex :
select sys.resourceID, sys.client_version0 from dbo.v_R_System as sys where UPPER(netbios_name0) = 'COMPUTER'
11/10/2008
18
11/10/2008
19
Contd.
Ex :
select count(*) From emp select count(emp _id) from emp;
11/10/2008
21
Contd.
Ex :
select * from emp where emp_id != 007 select * from emp where emp_id > 0
11/10/2008
23
Contd.
Ex :
SELECT DOCUMENT_NO,COUNT(*) FROM documents GROUP BY DOCUMENT_NO HAVING COUNT(*) > 1;
11/10/2008
25
11/10/2008
26
Contd.
Ex :
SELECT empno FROM emp WHERE empno IN (508858, 508859, 508860, 508861,508862, 508863, 508864)
SELECT empno FROM emp WHERE empno BETWEEN 508858 and 508864
11/10/2008
27
11/10/2008
28
Contd.
Ex :
select tab_name from tables where tab_name = (select tab_name from tab_columns where version = 604) and db_ver = (select db_ver from tab_columns where version = 604) select tab_name from tables where (tab_name, db_ver) = (select tab_name,db_ver from tab_columns where version = 604)
update emp set emp_cat = (select max(category) from emp_categories),sal = (select max(salrange) from emp_categories)where emp_dept = 20
update emp set (emp_cat , sal )= (select max(category), max(salrange) from emp_categories) Where emp_dept = 20
11/10/2008
29
Contd.
Ex :
select * from emp where sal*12>25000
11/10/2008
30
11/10/2008
31
Contd.
Ex :
select * from oe_order_headers_all where order_number>3 select * from oe_order_headers_all where order_number>=4
11/10/2008
32
11/10/2008
33
Contd.
Ex :
/*+index(t1) index(t2)..index(tn)*/ /*+FULL(t1) FULL(t2)FULL(tn)*/ /*+OPTIMIZER_GOAL=CHOOSE*/ /*+OPTIMIZER_GOAL=FIRST_ROWS*/ /*+ORDERED*/ /*+RULE*/ /*+OPTIMIZER_GOAL=ALL_ROWS*/ /*+OPTIMIZER_GOAL=ORDERED*/ /*+OPTIMIZER_GOAL=RULE*/ /*+USE_NL(t1,t2,t3,t4,t5,t6) ORDERED */
11/10/2008
34
Tips in PL/SQL
Break the piece of code into small blocks to achieve modularity. Grouping related logic as a single block. Use Packages for each major functionality. Avoid hard-coding.
11/10/2008
35
Follow PL/SQL Coding standards for procedures, variables, table types,rec types, functions and packages. Encapsulate your SQL. Avoid repeating the SQL in different places. Exception Handling. Usage of Bind Variables and Global Variables. Make Function Calls efficiently.
11/10/2008
36
Ex :
CREATE OR REPLACE FUNCTION get_a_mess_o_emps (deptno_in IN dept.depno%TYPE) RETURN emplist_t IS emplist emplist_t := emplist_t(); TYPE numTab IS TABLE OF NUMBER; TYPE charTab IS TABLE OF VARCHAR2(12); TYPE dateTab IS TABLE OF DATE; enos numTab; names charTab; hdates dateTab; BEGIN SELECT empno, ename, hiredate BULK COLLECT INTO enos, names, hdates FROM emp WHERE deptno = deptno_in; emplist.EXTEND(enos.COUNT); FOR i IN enos.FIRST..enos.LAST LOOP emplist(i) := emp_t(enos(i), names(i), hiredates(i)); END LOOP; RETURN emplist; END;
VESL TECHNOLOGIES LTD 38
Contd.
11/10/2008
Contd.
Ex :
PROCEDURE reality_meets_dotcoms (deptlist dlist_t) IS BEGIN FORALL aDept IN deptlist.FIRST..deptlist.LAST DELETE emp WHERE deptno = deptlist(aDept); END;
11/10/2008
39
11/10/2008
40
11/10/2008
41
Contd
Unit Testing. Debugging. Analysis on the Exceptions. Test Cases.
11/10/2008
43
Contd
Thank You
11/10/2008
44