Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 SQL Best Coding Practice in PeopleTools 7.x David Kurtz Go-Faster Consultancy Ltd.

Similar presentations


Presentation on theme: "1 SQL Best Coding Practice in PeopleTools 7.x David Kurtz Go-Faster Consultancy Ltd."— Presentation transcript:

1 1 SQL Best Coding Practice in PeopleTools 7.x David Kurtz Go-Faster Consultancy Ltd. david@go-faster.co.uk www.go-faster.co.uk

2 2 Who are you? Familiar with SQL not necessarily the DBA Might be –Application developer –Team Leader

3 3 So Where is all the SQL? Views Scrollxxx() PeopleCode SQLExec() PeopleCode Mass Change Application Engine PS/Query / Crystal SQR Stored Statements

4 4 Views

5 5 Scrollxxx() PeopleCode

6 6 SQLExec() PeopleCode

7 7 Mass Change

8 8 Application Engine

9 9 PS/Query / Crystal

10 10 SQR

11 11 Stored Statements

12 12 Simplicity As simple as possible As complicated as necessary Data Model Avoid excessive I/O

13 13 SQL Coding Standard Implicit Type Conversion Sorts Indexes Sub-queries

14 14 Coding Standards Be explicit –Make it readable Indent sub-queries and brackets –Use table aliases Lower parse time Less unexpected results without error messages –Avoid implicit type conversion –Avoid possible Y2K issue Explicitly specify Y2K compliant date formats

15 15 Readability CREATE TABLE PS_GEN_JOB_TBL(...) AS SELECT DISTINCT... FROM PS_PERSONAL_DATA A, PS_JOB B, PS_PERS_NID ND, PS_NID_TYPE_TBL NDT WHERE A.EMPLID=B.EMPLID ANDA.EMPLID=ND.EMPLID ANDB.EMPLID=ND.EMPLID ANDND.NATIONAL_ID_TYPE=NDT.NATIONAL_ID_TYPE AND (B.EFFDT>=%CURRENTDATEIN OR (B.EFFDT=(SELECTMAX(B2.EFFDT) FROM PS_JOB B2 WHERE B2.EMPLID=B.EMPLID ANDB2.EMPL_RCD#=B.EMPL_RCD# AND B2.EFFDT<=%CURRENTDATEIN) ANDB.EFFSEQ=(SELECTMAX(B3.EFFSEQ) FROM PS_JOB B3 WHERE B3.EMPLID=B.EMPLID AND B3.EMPL_RCD#=B.EMPL_RCD# ANDB3.EFFDT=B.EFFDT)))

16 16 Minimum table aliases SELECT COUNT(*) FROM PS_JOB B WHERE (EFFDT=(SELECTMAX(B1.EFFDT) FROM PS_JOB B1 WHERE B.EMPLID=EMPLID ANDB.EMPL_RCD#=EMPL_RCD# AND B.EFFDT<=%CURRENTDATEIN) ANDEFFSEQ=(SELECT MAX(B2.EFFSEQ) FROM PS_JOB B2 WHERE B.EMPLID=EMPLID AND B.EMPL_RCD#=EMPL_RCD# ANDB.EFFDT=EFFDT))

17 17 Maximum table aliases SELECT COUNT(*) FROM PS_JOB B WHERE (B.EFFDT=(SELECTMAX(B1.EFFDT) FROM PS_JOB B1 WHERE B1.EMPLID=B.EMPLID ANDB1.EMPL_RCD#=B.EMPL_RCD# AND B1.EFFDT<=%CURRENTDATEIN) ANDB.EFFSEQ=(SELECT MAX(B2.EFFSEQ) FROM PS_JOB B2 WHERE B2.EMPLID=B.EMPLID AND B2.EMPL_RCD#=B.EMPL_RCD# ANDB2.EFFDT=B.EFFDT))

18 18 Wrong table alias SELECT COUNT(*) FROM PS_JOB B WHERE (EFFDT=(SELECTMAX(B1.EFFDT) FROM PS_JOB B1 WHERE B.EMPLID=EMPLID ANDB.EMPL_RCD#=EMPL_RCD# AND B.EFFDT<=%CURRENTDATEIN) ANDEFFSEQ=(SELECT MAX(B2.EFFSEQ) FROM PS_JOB B2 WHERE B2.EMPLID=EMPLID AND B2.EMPL_RCD#=EMPL_RCD# ANDB2.EFFDT=EFFDT))

19 19 Implicit Type Conversion SELECT * FROM PS_JOB WHERE EMPLID=8001 SELECT * FROM PS_JOB WHERE EMPLID=8001

20 20 Implicit Type Conversion SELECT * FROM PS_JOB WHERE EMPLID=8001 TABLE ACCESS (FULL) OF 'PS_JOB why did this not use the index? SELECT * FROM PS_JOB WHERE EMPLID=8001 INDEX (RANGE SCAN) OF 'PSAJOB' (NON-UNIQUE)

21 21 Implicit Type Conversion SELECT * FROM PS_JOB WHERE EMPLID=8001 TABLE ACCESS (FULL) OF 'PS_JOB why did this not use the index? SELECT * FROM PS_JOB WHERE TO_NUMBER(EMPLID)=8001 SELECT * FROM PS_JOB WHERE EMPLID=8001 INDEX (RANGE SCAN) OF 'PSAJOB' (NON-UNIQUE)

22 22 Sorts Updating indexed columns Distinct Order by Group by Union -v- Union All

23 23 Indexes >~ 200 rows <~ 10% –Very rough guidelines –Avoid updating indexed columns

24 24 Distinct Sorts whole select list Can drive the join order of the tables Avoid distinct & order by –Order one way for the distinct –Order another way for the order by

25 25 Distinct SELECTDISTINCT A,B,C,D FROMtable ORDER BY A,B,C Order by clause unnecessary

26 26 Distinct Instead of SELECTDISTINCT A,B,C,D FROMtable ORDER BY A,C,B

27 27 Distinct Instead of SELECTDISTINCT A,B,C,D FROMtable ORDER BY A,C,B Try this SELECT DISTINCT A,C,B, D FROMtable

28 28 Group by Instead of SELECTA,B,C, SUM(D) FROMtable GROUP BY A,B,C ORDER BY A,C,B

29 29 Group by Instead of SELECTA,B,C, SUM(D) FROMtable GROUP BY A,B,C ORDER BY A,C,B Try this SELECT A,C,B, SUM(D) FROMtable GROUP BY A,C,B

30 30 Union -v- Union All Union –Each query is distinct, and so is sorted –Duplicates are eliminated Union All –One query followed by the next

31 31 Union -v- Union All SELECT1 FROMdual UNION SELECT 1 FROM dual Returns Dummy ----- 1 SELECT 1 FROMdual UNION ALL SELECT1 FROMdual Returns Dummy ----- 1

32 32 Union -v- Union All SELECT2 FROMdual UNION SELECT 1 FROM dual Returns Dummy ----- 1 2 SELECT 2 FROMdual UNION ALL SELECT1 FROMdual Returns Dummy ----- 2 1

33 33 Disabling Indexes Functions on columns Index disabled –TO_CHAR(column,DD-MM-YYYY) = :bind Index enabled –column = TO_DATE(:bind, DD-MM-YYYY) Oracle syntax, generic principle

34 34 Use all indexed columns Specify all indexed columns –Cannot exact scan column unless exact scan all previous columns

35 35 Use all indexed columns SELECT… FROMPS_JOB B WHEREEMPLID = :1 ANDEFFSEQ = ( SELECTMAX(EFFSEQ) FROM PS_JOB B1 WHEREB1.EMPLID = B.EMPLID ANDB1.EFFDT = B.EFFDT) SELECT… FROMPS_JOB B WHEREEMPLID = :1 ANDEFFDT = ( SELECTMAX(EFFSEQ) FROM PS_JOB B1 WHEREB1.EMPLID = B.EMPLID ANDB1.EMPL_RCD# = B.EMPL_RCD# ANDB1.EFFDT = B.EFFDT)

36 36 High Water Marks (Oracle) Oracle specific Delete -v- Truncate DELETE FROM table; TRUNCATE TABLE table; –Full Scans - HWM

37 37 Sub-queries Correlated –executed once per parent row Not Correlated –executed once in advance

38 38 Sub-queries –Correlated DELETE FROM table1 t1 WHERE EXISTS( SELECTx FROMtable2 t2 WHERE t1.keycolumn = t2.keycolumn) –Non-Correlated DELETE FROM table1 t1 WHERE t1.keycolumn IN( SELECT t2.keycolumn FROM table2 t2)

39 39 Sub-queries (Oracle) Oracle specific DELETE FROM table1 t1 WHERE (t1.keycolumn1, t1.keycolumn2) IN( SELECT t2.keycolumn1, t2.keycolumn2 FROM table2 t2)

40 40 ROWID (Oracle) Oracle specific physical address of row

41 41 Sub-queries (Oracle) Oracle specific DELETE FROM table1 t1 WHERE t1.rowid IN( SELECTt1b.rowid FROM table1 t1b, table2 t2 WHERE t1b.keycolumn1 = t2.keycolumn2 ANDt1b.keycoulmn2 = t2.keycolumn2)

42 42 Order of From clause (Oracle) Oracle specific Rule Based Optimiser –backwards Cost Based Optimiser –doesnt matter CBO + Ordered Hint –forwards Distinct tends to override this

43 43 Summary Lots of places to write SQL Code should be –efficient –simple –readable –explicit

44 44 SQL Best Coding Practice in PeopleTools 7.x David Kurtz Go-Faster Consultancy Ltd. david@go-faster.co.uk www.go-faster.co.uk


Download ppt "1 SQL Best Coding Practice in PeopleTools 7.x David Kurtz Go-Faster Consultancy Ltd."

Similar presentations


Ads by Google