Tags
case, DB2, PERFORMANCE, SQL
- Put the most frequently occurring conditions first. A CASE statement stops evaluations when the first TRUE test is fulfilled.
- When you are counting or summing. Do not use 0 (Zero), instead use NULL. Reason being a 0 (Zero) is added to your count/summation, whereas, a NULL is ignored. Depending on the amount of 0’s being added to your total, this can represent a significant CPU savings.
- Example:
, SUM(CASE WHEN ACT_INDA = ‘Y’ THEN 0
WHEN ACT_INDB = ‘Y’ THEN 0
WHEN ACT_INDC = ‘Y’ THEN 1
ELSE 0
END ) AS A
Instead use:
, SUM(CASE WHEN ACT_INDA = ‘Y’ THEN NULL
WHEN ACT_INDB = ‘Y’ THEN NULL
WHEN ACT_INDC = ‘Y’ THEN 1
ELSE NULL
END ) AS A
——————————————————————————————————–
In United States, If you would like to Earn Free Stocks, Credit card Points and Bank account Bonuses, Please visit My Finance Blog
——————————————————————————————————–
You may also like to look at: