Tuning SQL Statement #1.......
Tuning SQL Statement #2.......
Consider whether a UNION ALL will suffice in place of a UNION.
The UNION statement performs the equivalent of a SELECT DISTINCT on the final result set. In other words, the UNION clause forces all rows returned by each portion of the UNION to be sorted and merged and duplicate to be filtered before the first row is returned. A UNION ALL simply returns all rows including duplicates and does not have to perform any sort, merge or filter. If your tables are mutually exclusive (include no duplicate records), or you don't care if duplicates are returned, the UNION ALL is much more efficient as it does not perform the SELECT DISTINCT function, which saves a lot of unnecessary resources from being used.
Do’s
SELECT acct_num, balance_amt
FROM debit_transactions
WHERE tran_date = '1-JAN-06'
UNION ALL
SELECT acct_num, balance_amt
FROM credit_transactions
WHERE tran_date = '1-JAN-06' ;
Don’ts
SELECT acct_num, balance_amt
FROM debit_transactions
WHERE tran_date = '1-JAN-06'
UNION
SELECT acct_num, balance_amt
FROM credit_transactions
WHERE tran_date = '1-JAN-06' ;
The DECODE Function
Using the DECODE function as a replacement for multiple query set operators is good for performance but should only be used in extreme cases such as the UNION clause joined SQL statements as shown.
Do’s
SELECT stock_id||' '||
DECODE(SIGN(qtyonhand)
,-1,'Out of Stock',0,'Out of Stock'
,1,DECODE(SIGN(qtyonhand-min)
,-1,'Under Stocked',0,'Stocked'
,1,DECODE(sign(qtyonhand-max)
,-1,'Stocked',0,'Stocked'
,1,'Over Stocked'
)
)
) FROM stock;
Don’ts
SELECT stock_id||' Out of Stock' FROM stock WHERE
qtyonhand <=0
UNION
SELECT stock_id||' Under Stocked' FROM stock
WHERE qtyonhand BETWEEN 1 AND min-1
UNION
SELECT stock_id||' Stocked' FROM stock
WHERE qtyonhand BETWEEN min AND max
UNION
SELECT stock_id||' Over Stocked' FROM stock
WHERE qtyonhand > max;
Datatype Conversions
Try to avoid using any type of data conversion function in any part of an SQL statement which could potentially match an index, especially if you are trying to assist performance by matching appropriate indexes.
Do’s
SELECT ...
FROM customer
WHERE cust_id = TO_NUMBER('11'); SELECT ...
FROM customer
WHERE cust_id = '11';
Don’ts
SELECT ...
FROM customer
WHERE cust_type = 1; SELECT ...
FROM emp
WHERE TO_NUMBER (cust_type) = 1;
https://www.facebook.com/datastage4you
https://twitter.com/datagenx
https://plus.google.com/+AtulSingh0/posts
https://datagenx.slack.com/messages/datascience/
No comments:
Post a Comment