HowHow To Write Efficient SQL To Write Efficient SQL
HowHow To Write Efficient SQL To Write Efficient SQL
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
User Name
Log in
Help
Register
Search
Home
Forum
Blogs
What's New?
Experience
TF4U News and Announcements Programming Web Design DBMS Operating Systems Software Networking
Home
DBMS
10 Online
Recent posts
Share
Join Chat
1 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
This article describes some of the techniques to be adhered to while writing SQL queries. A novice developer can use some of these to write effective SQL statements. Upon using these correctly, a remarkable improvement can be seen in the performance of the queries in the output. Note: The examples in this document are based on Oracle database. 1)
ORACLE parser always processes table names from right to left, the table name specified last (driving table) is actually the first table processed. If more than one table is specified in a FROM clause of a SELECT statement, the table containing the lowest number of rows should be chosen as the driving table. When ORACLE processes multiple tables, it uses an internal sort/merge procedure to join those tables. First, it scans & sorts the first table (the one specified last in the FROM clause). Next, it scans the second table (the one prior to the last in the FROM clause) and merges all of the rows retrieved from the second table with those retrieved from the first table. e.g. Table TAB1 has 20,000 rows Table TAB2 has 1 row. SELECT COUNT(*) FROM TAB1, TAB2 (Best Approach) SELECT COUNT(*) FROM TAB2, TAB1 (Poor Approach) If three tables are being joined, select the intersection table as the driving table. The intersection table is the table that has many tables dependent on it.
2)
Table joins should be written first, before any condition of WHERE clause. And the conditions which filter out the maximum records should be placed at the end after the joins as the parsing is done from BOTTOM to TOP. For e.g. Least Efficient: Code:
SELECT . . . . . . . . . . . .
10 Online Recent posts Share Join Chat
2 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
3)
3 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
Note: One simplest way to increase the number of rows of data that can be fetched with one database access (and thus reduce the number of physical calls) is to reset the ARRAYSIZE parameter in SQL*Plus, SQL*Forms & Pro*C. Suggested value is 200. 5)
The same result can be achieved much more efficiently using DECODE: Code:
SELECT COUNT(DECODE(DEPT_NO, 0020, X, NULL)) D0020_COUNT, COUNT(DECODE(DEPT_NO, 0030, X, NULL)) D0030_COUNT, SUM(DECODE(DEPT_NO, 0020, SAL, NULL)) D0020_SAL, SUM(DECODE(DEPT_NO, 0030, SAL, NULL)) D0030_SAL FROM EMP WHERE ENAME LIKE SMITH%;
4 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
Code:
SELECT NAME FROM EMP WHERE EMP_NO = 1234; SELECT NAME FROM DPT WHERE DPT_NO = 10; SELECT NAME FROM CAT WHERE CAT_TYPE = 'RD';
7)
8)
Join Chat
5 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
Space in the redo log buffer cache. Overhead associated with any internal Oracle mechanisms to manage the resources in the previous three items. 10)
12)
e.g: To improve performance, minimize the number of table lookups in queries, particularly if the statements include sub-query SELECTs or multi-column UPDATEs. e.g. Least Efficient: Code:
SELECT TAB_NAME FROM TABLES WHERE TAB_NAME = (SELECT TAB_NAME FROM TAB_COLUMNS WHERE VERSION = 100) AND DB_VER = (SELECT DB_VER FROM TAB_COLUMNS
10 Online Recent posts Share Join Chat
6 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
13)
10 Online
Recent posts
Share
Join Chat
7 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
15)
To improve the performance, replace this code with: Method 1 (Efficient): Code:
SELECT . . . FROM EMP A, DEPT B WHERE A.DEPT_NO = B.DEPT_NO (+) AND B.DEPT_NO IS NULL AND B.DEPT_CAT(+) = 'A';
16)
8 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
17)
EXISTS is a faster alternative because the RDBMS kernel realizes that when the sub-query has been satisfied once, the query can be terminated. 18)
19)
10 Online Recent posts Share Join Chat
9 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
21)
10 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
22)
instead of Code:
SELECT LOC_ID, LOC_DESC, REGION FROM LOCATION WHERE LOC_ID = 100 OR REGION = DELHI
23)
Code:
Recent posts
Share
Join Chat
11 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
Use: Code:
SELECT ACCOUNT_NAME FROM TRANSACTION WHERE AMOUNT > 0;
In the following example, || is the concatenate function. It, like other functions, disables indexes. Do Not Use: Code:
SELECT ACCOUNT_NAME, AMOUNT FROM TRANSACTION WHERE ACCOUNT_NAME || ACCOUNT_TYPE = AMEXA;
Use: Code:
SELECT ACCOUNT_NAME, AMOUNT FROM TRANSACTION WHERE ACCOUNT_NAME = AMEX AND ACCOUNT_TYPE = A
24)
10 Online
Recent posts
Share
Join Chat
12 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
Reliant Energy Reliant solar panel leasing is a great option for renewable energy! www.reliant.com/solar 1. Fast Database Response It's Reason 1 of 6 To Change To IBM DB2. Get All 6 In Your Free eBook. www.IBM.com/facts Database Security Guide Practical Guide to Database Security & Compliance. Free Copy! www.mcafee.com
Categories: Oracle Tags : sql
Find us on Facebook
TechForum4U
Like 46 people like TechForum4U.
Sangita
Nihar
Sonam
Deepak
Chandan
Gunasekar
Anu
Eidrian
Vicky
Fharhan
Harsh
Twana
Manish
Suresh
Angad
Twum
Pankaj
Zsanett
Arale
Prayag
Ryan Joseph
Sudipa
Linda
Ven
Sujeet
Harsha
13 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
Join Chat
14 of 15
9/8/2011 9:45 AM
http://techforum4u.com/content.php/323-How-To-Write-Efficie...
All times are GMT. The time now is 04:21 PM. Content Copyright of Users, Anything Else TechForum4U. All rights reserved. Powered by vBulletin Version 4.0.7 Copyright 2011 vBulletin Solutions, Inc. All rights reserved.
10 Online
Recent posts
Share
Join Chat
15 of 15
9/8/2011 9:45 AM