GOODSQL
GOODSQL
Date : 22-May
For : All in Automation
Re : How to write good SQL
Valid till : Forever
I received the following from Tina London in the UK. I think its worth
posting to the group. While I format things slightly differently, her
reasons are valid and worth browsing. Her notes on the V6 optimiser
are great. I will be incorporating an abridged version of some of this in
the non-DBA FAQ 0.4. Her current address is tinalondon@artemis.demon.co.uk
Email : tlondon@cix.compulink.co.uk
1.0 Introduction
1.0 Introduction
SELECT
INTO
FROM
WHERE
AND/OR
GROUP BY
HAVING
CONNECT BY
FOR UPDATE OF
ORDER BY
SELECT sal,
Job,
ename,
dept
FROM emp
WHERE sal > any
(SELECT sal
FROM emp
WHERE deptno = 30)
ORDER BY sal;
SELECT ename,
dept
FROM emp
SELECT ename
FROM emp e,
dept d
WHERE e.empno = d.empno(+)
SELECT count(*)
FROM oe o,
oe_link l,
oe_link_name n
WHERE o.oe = l.oe
AND l.name = n.name
Indexes :
unique on oe(id)
unique on oe_link(oe)
unique on oe_link_name(name)
non unique on oe(oe)
SELECT count(*)
FROM oe_link l,
oe_link_name n,
oe o
WHERE o.oe = l.oe
AND l.name = n.name
SELECT count(*)
FROM oe o,
oe_link l,
oe_link_name n
WHERE o.oe = l.oe
AND l.name = n.name
Time 97 secs.
whereas
SELECT id,
oe
FROM oe
WHERE id = 1232
Time .4 secs.
SELECT id,
oe
FROM oe
WHERE id+1 = 1233
SELECT *
FROM oe
WHERE trunc(timestamp) = '26-MAR-91'
SELECT *
FROM oe
WHERE timestamp between '26-mar-91'
AND '27-mar-91'
SELECT *
The second one only takes 1.05 seconds to
a)
SELECT *
FROM job
WHERE db_id||job_no = 'AZ0100201'
b)
SELECT *
FROM job
WHERE db_id = 'AZ'
AND job_no = '0100201'
indexes
SELECT deptno
FROM dept
WHERE deptno = to_char(1324)
SELECT deptno
FROM dept
WHERE deptno = '1324'
Char char||''
number number+0
date add_months(date,0)
SELECT deptno
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 10
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 11
AUTHOR : Tina London
DATE : August 7, 1992
SELECT p.name
FROM people p,
job j
WHERE p.name = j.name(+)
AND j.name is null
fred the 27
1 record selected.
SELECT name
FROM people
WHERE name not in
(SELECT name
FROM job)
fred the 27
1 record selected.
1 record selected.
1 record selected.
SELECT 'x'
FROM dual
WHERE exists
( SELECT 'x'
FROM job
where name = 'fred the 45')
1 record selected.
SELECT 'x'
FROM dual
WHERE exists
(SELECT 'x'
FROM job
WHERE name = 'fred the 34')
SELECT 'x'
FROM dual
WHERE exists
(SELECT 'x'
FROM job
WHERE name = 'fred the 9999')
missing entities.
SELECT p.name
FROM people p,
job j
WHERE p.name = j.name(+)
AND j.name is null
fred the 218
SELECT name
FROM people p
WHERE not name in
(SELECT name
FROM job j
WHERE p.name = j.name)
Step 1
Step 2
Step 3
Update Target_Table
Set Target_Field = (Select Source_Information
From Source_Table
Where Source_Table.Key =
Target_Table.Key)
Where exists (Select 'x'
processing time.
Declare
Cursor Source is
Select *
From Source_Table;
Begin
For Row in Source Loop
Update Target_Table
Set Target_Field = Row.Source_Information
Where Key = Row.Key;
End Loop;
Exception
When OTHERS Then
Null;
End;
on column COMM
SELECT *
FROM emp
WHERE comm is NULL
SELECT *
FROM emp
WHERE comm is not NULL
SELECT *
FROM emp
WHERE comm > -0.01
avg(sal)
FROM emp
WHERE job = 'president'
OR job = 'manager'
GROUP BY job
SELECT ename
FROM emp
WHERE deptno=20
AND job='manager'
SELECT *
FROM emp
WHERE job='manager'
AND deptno > 10
indexes:
SELECT ename
FROM emp
WHERE sal > 1
AND empno > 1
indexes :
SELECT ename
FROM emp
WHERE sal = 3000
AND empno = 7902
indexes :
indexes :
SELECT *
FROM emp
WHERE job = 'president'
AND deptno = 10
SELECT *
FROM emp
WHERE deptno = 10
AND job = 'president'
SELECT *
FROM emp
developers should check the selectivity of this
SELECT *
FROM emp
WHERE job != 'clerk'
AND deptno = 10
6.9 Or optimisation
SELECT ename,
sal,
job
FROM emp
WHERE sal = 3000
OR job = 'clerk'
index :
indexes :
Becomes :
SELECT ename,
sal,
job
FROM emp
WHERE job = 'clerk'
UNION
SELECT ename,
sal,
job
FROM emp
WHERE sal = 3000
AND job != 'clerk'
SELECT ename,
sal,
job
FROM emp
WHERE job = 'clerk'
OR sal = 3000
job
FROM emp
WHERE sal = 3000
UNION
SELECT ename,
sal,
job
FROM emp
WHERE job = 'clerk'
AND sal != 3000
indexes :
SORT(UNIQUE)
MERGE JOIN
TABLE ACCESS (FULL) OF 'MEN'
SORT(JOIN)
TABLE ACCESS (FULL) OF 'JOB'
indexes :
unique on job(jobno)
non unique on job(pin)
SORT(UNIQUE)
NESTED LOOPS
TABLE ACCESS (FULL) OF 'MEN'
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 27
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 28
AUTHOR : Tina London
DATE : August 7, 1992
SELECT *
FROM dept
WHERE deptno not in
(SELECT deptno
FROM emp)
SELECT d.*
FROM dept d,emp e
WHERE d.deptno = e.deptno(+)
AND e.rowid is NULL
APPENDIX A
1 ROWID = constant
2 Unique indexed column = constant
3 entire unique concatenated index =
constant
4 entire cluster key = corresponding
cluster key in another table in
the same cluster
5 entire cluster key = constant
6 entire non-unique concatenated
index = constant
7 non-unique single column index
merge
8 most leading concatenated index =
constant
9 indexed column BETWEEN low value
AND high value, or indexed
column LIKE 'C%' (bounded range)
10 sort/merge (joins only)
11 MAX or MIN of single indexed
column
12 ORDER BY entire index
13 full table scans
14 unindexed column = constant, or
column IS NULL, or column LIKE
'%C%' (full table scan)
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 29
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 30
AUTHOR : Tina London
DATE : August 7, 1992
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 30
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 31
AUTHOR : Tina London
DATE : August 7, 1992
APPENDIX B
unique index on id
nonunique index on oe
with 2 records
With 1 record
job(job_id,name)
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 31
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 32
AUTHOR : Tina London
DATE : August 7, 1992
APPENDIX C
Update Target_Table
Set Target_Field = (Select Source_Information
From Source_Table
Where Source_Table.Key =
Target_Table.Key)
Where exists (Select 'x'
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 32
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 33
AUTHOR : Tina London
DATE : August 7, 1992
Declare
Cursor Source is
Select *
From Source_Table;
Begin
For Row in Source Loop
Update Target_Table
Set Target_Field = Row.Source_Information
Where Key = Row.Key;
End Loop;
Exception
When OTHERS Then
Null;
End;
APPENDIX D
1 INTRODUCTION
EMP.EMPNO(+) = PERS.EMPNO
AND PERS.DEPTNO(+) = DEPT.DEPTNO
AND DEPT.JOB(+) = EMP.JOB - circular outer
join relationship
APPENDIX E
THOU SHALL NOT ASK THE KERNEL FOR MORE THAN THOU
WANTEST.
APPENDIX F
EXPLAIN Facility
MOTIVATION
There is a need for users to be able to determine the steps the system
takes in performing various operations on a database. The EXPLAIN facility
provides users with a convenient mechanism for getting this information.
The facility stores this information in a standard database table that can
be manipulated with standard SQL statements.
SYNTAX
The following syntax is based on the syntax used by DB2 for their
EXPLAIN facility:
where
TIMESTAMP - The date and time when the statement was analysed.
REMARKS - Any comment the user wishes to associate with this step of
the analysis.
OPERATION - the name of the operation being performed. The following table
provides a listing of the operations described by the facility.
Operation Description
---------------------------------------------------------------
And-Equal A retrieval utilising intersection of
rowids from index searches
Connect by A retrieval that is based on a tree walk
Concatenation A retrieval from a group of tables. It is
essentially a UNION ALL operation of the
sources. Used for OR operations.
Counting A node that is used to count the number of
rows returned from a table. Used for queries
that use the ROWNUM meta-column.
Filter A restriction of the rows returned from a table
First Row A retrieval of only the first row
For Update A retrieval that is used for updating
Index A retrieval from an index
Intersection A retrieval of rows common to two tables
Merge Join A join utilising merge scans
Minus A retrieval of rows in Source 1 table but not in
Source 2 table
Nested Loops A join utilising nested loops. Each
value in the
first subnode is looked up in the second subnode.
This is often used when one table in a join is
indexed and the other is not.
Project A retrieval of a subset of columns from a table
Remote A retrieval from a database other than the current
database
Sequence An operation involving a sequence table
Sort A retrieval of rows ordered on some column or group
of columns
Table A retrieval from a base table
Union A retrieval of unique rows from two tables
View A retrieval from a virtual table
of index scan, type of filter, etc. The following table
OBJECT_NODE - the name of the node that owns the database object.
OBJECT_OWNER - the name of the schema the owns the database object.
An SQL script to create this table resides in file xplainpl.sql in the same
directory containing the file catalog.sql. This table must reside in the
current schema unless you use the optional INTO clause of the EXPLAIN
command.
EXAMPLES
EXPLAIN PLAN
SET STATEMENT_ID = 'query1'
INTO QUERY_PLANS
FOR SELECT * FROM T1,T2,T3 WHERE T1.F1 = T2.F1 AND T2.F2 = T3.F2;
8 RECORDS selected
EXPLAIN PLAN
SET STATEMENT_ID = 'query2'
INTO QUERY_PLANS
FOR SELECT * FROM T1 WHERE F1 > 1;
2 RECORDS selected
EXPLAIN PLAN
SET STATEMENT_ID = 'query3'
INTO QUERY_PLANS
FOR SELECT F1 FROM T1 WHERE F1 > 1;
EXPLAIN PLAN
SET STATEMENT_ID = 'query4'
INTO QUERY_PLANS
FOR SELECT AVG(F1),F2 FROM T1 GROUP BY F2;
2 RECORDS selected
EXPLAIN PLAN
SET STATEMENT_ID = 'query5'
INTO QUERY_PLANS
FOR SELECT DISTINCT F1 FROM T1;
2 RECORDS selected
3 RECORDS selected
The final example displays a complex query whose output is sent to the
default plan table. ( It is assumed that this table has been created before
issuing the statement.)
EXPLAIN PLAN
SET STATEMENT_ID = 'query7'
FOR SELECT * FROM T1,T2 WHERE T1.F1 = T2.F1 UNION
SELECT * FROM T2,T3 WHERE T2.F1 = T3.F1;
13 RECORDS selected
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 46
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 47
AUTHOR : Tina London
DATE : August 7, 1992
LPAD(' ',2*LEVEL)||OPERATION
------------------------------------------------------------------------------
OPTIONS OBJECT_NAME
-------------------------------------
PROJECTION
UNION
SORT
DISTINCT
NEST LOOP
TABLE SCAN
BY ROWID T1
INDEX SCAN
RANGE IT1
TABLE SCAN
FULL T2
SORT
DISTINCT
MERGE JOIN
SORT
JOIN
TABLE SCAN
FULL T2
SORT
JOIN
TABLE SCAN
FULL T3
13 RECORDS selected
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 47
GUIDELINES AND GOOD PRACTICE FOR DEVELOPING SQL Page 48
AUTHOR : Tina London
DATE : August 7, 1992
APPENDIX G
SQL_PASSWD is wms6/hoddengrey
ORACLE_SID is oracle
ORACLE RDBMS V6.0.33.1.4, transaction processing option - Production
Session altered.
'PROGRAM|'||PID||'|'||PROGRAM
--------------------------------------------------------------------------------
PROGRAM|29|sqlplus@onyx (TCP Two-Task) orapid:19422
SQL>
--
David T. Bath | Email:dtb@otto.bf.rmit.oz.au (131.170.40.10)
Senior Tech Consultant | Phone: +61 3 347-7511 TZ=AEST-10AEDST-11
Global Technology Group | 179 Grattan St, Carlton, Vic, 3153, AUSTRALIA
"The robber of your free will does not exist" - Epictetus