Sql performance vesl technologies

  • View
    367

  • Download
    2

Embed Size (px)

DESCRIPTION

How to standardized querry and code for SQL and PL/SQL

Text of Sql performance vesl technologies

  • 1. Performance Tuning SQL and PL/SQL Presentation By P. S. Mukesh Yadav and Suresh C Mishra 11/10/2008 VESL TECHNOLOGIES LTD 1
  • 2. Tips in SQL Tips in PL/SQL 11/10/2008 Topics VESL TECHNOLOGIES LTD 2
  • 3. 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 VESL TECHNOLOGIES LTD 3
  • 4. Example of First Tip SELECT ename FROM emp WHERE deptno IN (SELECT deptno FROM emp WHERE comm = 10 AND sal>4000); SELECT ename FROM emp d WHERE EXISTS ( SELECT e.deptno FROM emp e WHERE 1=1 and e.deptno = d.deptno AND e.comm = 10 AND e.sal > 4000); SELECT ename FROM emp e,dept d WHERE e.deptno = d.deptno AND e.comm = 10 AND e.sal > 4000 11/10/2008 VESL TECHNOLOGIES LTD 4
  • 5. Example of Second Tip Ex : SELECT ooha.order_type_id,ooha.order_ number FROM oe_order_headers_all ooha WHERE EXISTS (SELECT 1 FROM oe_transaction_types_tl ottt WHERE ooha.order_type_id = ottt.transaction_type_id); select distinct ooha.order_type_id,ooha. order_number from oe_order_headers_all ooha,oe_transaction_type s_tl ottt where ooha.order_type_id = ottt.transaction_type_id 11/10/2008 VESL TECHNOLOGIES LTD 5
  • 6. Tips in SQL Contd.. Avoid the use of NOT IN.Instead, a NOT EXISTS sub query may run faster. Use Minus Operator instead of Exists sub queries(Not in and Not Exists) results in faster execution plan. Rewrite (negative(ex : not in)) sub queries as outer joins to improve performance. Never use numbers in sorting. 11/10/2008 VESL TECHNOLOGIES LTD 6
  • 7. Contd. Ex : SELECT * FROM EMP A WHERE DEPTNO NOT IN (SELECT DEPTNO FROM DEPT WHERE DEPTNO=A.DEPTNO) select * from ap_invoices_all where invoice_id not in(select invoice_id from ap_invoice_payments_all ) 11/10/2008 SELECT * FROM EMP A WHERE NOT EXISTS (SELECT 1 FROM DEPT WHERE DEPTNO=A.DEPTNO) SELECT aia.invoice_id FROM ap_invoices_all aia, ap_invoice_payments_all aipa WHERE aia.invoice_id = aipa.invoice_id(+); VESL TECHNOLOGIES LTD 7
  • 8. 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 VESL TECHNOLOGIES LTD 8
  • 9. Tips in SQL Contd.. Avoid running a query in a loop.Make use of UNION and execute it at the end of the loop. Avoid the LIKE predicate.Always replace LIKE with = when appropriate. Use LIKE rather than the SUBSTR function. Avoid Where Column Like '%string'. Consider Indexes for Max() and Min(). 11/10/2008 VESL TECHNOLOGIES LTD 9
  • 10. Contd. Ex : select * from dept where dname like 'RESEARCH' select ename from emp where ename like X% 11/10/2008 select * from dept where dname = 'RESEARCH' select ename from emp where substr(ename,1,1) = X VESL TECHNOLOGIES LTD 10
  • 11. Tips in SQL Contd.. Use Decode and Case for complex aggregations. Use WHERE instead of HAVING. Use UNION ALL instead of UNION. Prefer using GROUP BY only when any aggregate functions are present. Avoid usage of TO_CHAR and use TRUNC instead. 11/10/2008 VESL TECHNOLOGIES LTD 11
  • 12. Contd. Ex : SELECT E_Name FROM Employees_Norway UNION SELECT E_Name FROM Employees_USA select * from emp where to_char(emp_joindate, mmddyyyy) < to_char(sysdat e,mmddyyyy) 11/10/2008 Ex : SELECT E_Name FROM Employees_Norway UNION ALL SELECT E_Name FROM Employees_USA select * from emp where emp_joindate < trunc (sysdate) VESL TECHNOLOGIES LTD 12
  • 13. 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%'; 11/10/2008 SELECT DEPTID, SUM(SALARY) FROM EMP WHERE DEPTID = 100 GROUP BY DEPTID; VESL TECHNOLOGIES LTD 13
  • 14. 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 VESL TECHNOLOGIES LTD 14
  • 15. Tips in SQL Contd.. Use NVL to check against a number instead of the NULL value. Use = 1 instead of < 100 and > 0 for faster comparisons. Avoid using functions on indexed columns. Use SET TIMING ON to test the execution time of a slow query. Use EXPLAIN PLAN to view the execution steps of a slow query. 11/10/2008 VESL TECHNOLOGIES LTD 15
  • 16. 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 VESL TECHNOLOGIES LTD 16
  • 17. 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 11/10/2008 Object Name CSF_DEBRIEF_LINES CS_INCIDENTS_ALL_B CS_INCIDENTS_U2 JTF_TASKS_B JTF_TASK_ASSIGNMENTS JTF_TASK_ASSIGNMENTS_N2 CSF_DEBRIEF_HEADERS CSF_DEBRIEF_HEADERS_N1 CSF_DEBRIEF_LINES_N1 Rows Bytes 4 3 4 1 1 1 1 1 1 1 2 1 1 4 Cost Object Node In/Out PStart PStop 1134 3 1134 1131 1129 1126 4 2 10 1122 20 3 2 14 2 1 2 414 776 56 42 22 12 VESL TECHNOLOGIES LTD 17
  • 18. Contd. Ex : select sys.resourceID, sys.client_version0 from dbo.v_R_System as sys where UPPER(netbios_name0) = 'COMPUTER' select sys.resourceID, sys.client_version0 from dbo.v_R_System as sys where netbios_name0 = 'COMPUTER 11/10/2008 VESL TECHNOLOGIES LTD 18
  • 19. Tips in SQL Contd.. Remove unnecessary large full table scans. Minimize outer joins and use them only if necessary. Only use CHAR for data that must be an exact length such as phone numbers or gender identification fields because it pads variable length data with white-space that will cause string comparisons to fail. 11/10/2008 VESL TECHNOLOGIES LTD 19
  • 20. Tips in SQL Contd.. Use an indexed column or primary key column when counting rows. Avoid unnecessary sorting. Always better to use base tables instead of views. If a view is in a query,try to optimize the view first before optimizing the query. Always place all t