ORACLE OPTIMIZATION

[1] SELECT * FROM EMP
    WHERE NAME = 'SMITH' ===== CORRECT
   
    WHERE JOB_TITLE = 'CLEARK' ===== WRONG
    WHERE GENDER = 'MALE'      ===== WRONG
   
    INDEX: Ruin Performance if more then 10% of rows of the index column is poor.
   
[2] ARRANGE TABLE

Table should be arrange from smallest effective # of rows to largest effective # of rows.

SELECT /* ordered */ D.NAME,E.NAME
   FROM DEPT D,EMP E
   WHERE D.MGR = E.SS ==================== CORRECT
 
SELECT /* ordered */ D.NAME,E.NAME
  FROM EMP E,DEPT D
  WHERE D.MGR = E.SS ==================== WRONG
 
[3] If Column use/accept NULL then it will not use the Index.

[4] HASH JOIN ==== Faster then ==== NEXTED LOOP

    HASH JOIN = Effective when more memory intensive then nexted loop.
                You need to set HASH_AREA_SIZE or PGA_AGGREGATE_TARGET
               
    HINT = USE_HASH(T1,T2)
   
[5] SELECT COUNT(1) FROM T1 ====== BOTH ARE SAME
    SELECT COUNT(*) FROM T1 ====== BOTH ARE SAME
   
    SELECT NULL AS A FROM DUAL UNION
    SELECT 'ABC' FROM DUAL  
    OUTPUT : 2 <--- TWO ROWS
   
    SELECT COUNT(A) FROM (=====ABOVE QUERY=======)
    OUTPUT : 1 <--- Row (Null not include)
   
    Note : Count will not include the null value.
   
[6] Nester Loop

Use when joining small number of rows

use_nl(t1,t2) <---------- hint.

[7] Sort merge Join

=> use when source sorted already and
=> sort operation (order by) does not have to be done.

[8] use select col1,col2 from tbl1

 Instead select * from tbl1.

[9] Exec dbms_stats.gather_table_stats(
'schema_name',
'table_name',
estimate_percent =>100,
degree=>4,
cascade=>true);

[10] cluster table
cluster index

=> Determne the physical order of data in a table.
=> Only one cluster index per table.

[11] composit index

=> contain more then one field called composit index.
=> Effective when
All field combined will give significantly less rows then the first field alone.

child_age  ====|
parent_age ====| <------- not work

child_name  ====|
parent_name ====| <------- work

[12] GTT (Global Temperory Table - Index)

=> Session Specific (Data auto delete on session end)
=> Index can be created on GTT table.

Scope of index is same as session.
Run time index created.

=> Data store in TEMP SEGMENT in TEMP TABLESPACE.

=> On commit delete row.
= Delete data end of the transaction.
=> On commit preserve row.
= delete data end of the session.

[13] Large Data + Index

=> Drop index before bulk loading data.

-> Large data p Drop index
-> nsert become Faster
-> Recreate Index and analyze table.

[14] Force View.

Create ore replace force view testview
as Select * from none_existant_view.

with Force option you can create a view if base table does not exist.

[15] NVL2

NVL2(string2,value_if_not_null,Value_if_null)

[16] PURGE

=> Remove from recyclebin.
=> Can not rollback PURGE statement.

To see recyclebin content.

select * from recyclebin      <--- synonym
select * from user_recyclebin <--- synonym

Both statement gives same rows.

Use

=> PURGE table test; <---To bypass recyclebin.

=> DROP TABLE table1 PURGE; <--- Use it.

=> To Remove/make an empty recyclebin all objects.

PURGE RECYCLEBN;

[17] FORALL ..... DELETE
     FORALL ..... UPDATE ..... <--- use for batter performance.
   
[18] parallel hint.

If you have 36 processor, the full scane can run 35 time faster.

[19] Nologging

[20] Explain Plan

=> CBO = Cost based optimization.

=> COST : Represent the estimated resource usage for an execution.
Resource : [1] I/O, CPU, MEMORY
          [2] Number of row returned.

 => BYTE : Number of bytes returned by the operation.

 => CARDINALITY : How many unique value in a column of table.

Multiplication In SQL Server


SELECT 24/196 - OUTPUT : 0

SELECT CAST((24 * 0.1) / (196 * 0.1) * 100 as numeric(36,2))  OUTPUT : 12.24

Repeate Same String Multiple Time.


FUNCTION:  REPLICATE

SELECT REPLICATE ('ABC', 3)

OUTPUT (Print Same string 3 times)

ABCABCABC


Get Max Number or Date From List


How to find the Max From (5,8,124,498)

--Get Max Number from the list
SELECT
  (SELECT Max(v) FROM (VALUES (5), (6), (7)) AS value(v)) as [MaxValue]

  --Get Max Date from the list
SELECT  getdate(),
  (SELECT Max(v) FROM (VALUES (getdate()), (getdate()+1), (getdate()+2)) AS value(v)) as [MaxDate]

  --Syntex
SELECT
COL1,
(SELECT Max(v) FROM (VALUES (5), (6), (7)) AS value(v)) as [MaxValue],
COL2
FROM
  TABLE1

Simple Block

DECLARE

@YEAR VARCHAR(MAX);

Begin

set @YEAR='Aug-2011';

SELECT COUNT (*) ,@year ,'Test'

End

Search Column name from all tables.

 SELECT DISTINCT TABLE_NAME , COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME like '%semester%'

Comma separate column to row conversation function.

CREATE FUNCTION [dbo].[fnSplitString]
(
    @string NVARCHAR(MAX),
    @delimiter CHAR(1)
)
RETURNS @output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
    DECLARE @start INT, @end INT
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
    WHILE @start < LEN(@string) + 1 BEGIN
        IF @end = 0
            SET @end = LEN(@string) + 1
     
        INSERT INTO @output (splitdata)
        VALUES(SUBSTRING(@string, @start, @end - @start))
        SET @start = @end + 1
        SET @end = CHARINDEX(@delimiter, @string, @start)
       
    END
    RETURN
END

Query :  Select * from fnSplitString ('YELLOW, RED, PINK',',');

OUTPUT

YEWLLOW
RED
PINK

MS SQL - Date Format

SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM)
                                        -- Oct  2 2008 11:01AM
SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy - 10/02/2008                  
SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd – 2008.10.02           
SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy
SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy
SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy
SELECT convert(varchar, getdate(), 106) -- dd mon yyyy
SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy
SELECT convert(varchar, getdate(), 108) --  hh:mm:ss
SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM)
                                        -- Oct  2 2008 11:02:44:013AM   
SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy
SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd
SELECT convert(varchar, getdate(), 112) -- yyyymmdd
SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm
                                        --  02 Oct 2008 11:02:07:577     
SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h)
SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h)
SELECT convert(varchar, getdate(), 121) --  yyyy-mm-dd hh:mm:ss.mmm
SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm
                                        --  2008-10-02T10:52:47.513

-- SQL create different date styles with t-sql string functions
SELECT replace(convert(varchar, getdate(), 111), ‘/’,  ‘) -- yyyy mm dd
SELECT convert(varchar(7), getdate(), 126)                 -- yyyy-mm
SELECT right(convert(varchar, getdate(), 106), 8)          -- mon yyyy