I have inherited the following statement:
SELECT ISNULL((SELECT (-1)*SUM(CAST(ISNULL(NULLIF(TOTALMTDLCY,''),0) AS DECIMAL(20,5)))
FROM TI),0)
and although I know what it’s trying to do (prevent null values and return 0) I feel there must be a more performant way of doing this?
The reason is that I have a sproc that uses this statement 12 times to get values from different tables, for 40,000 customers (480,000) and then inserts three records for each (1.5M).
This takes around 13 mins to complete – and by gathering some simple stats on the process and totalling them it shows that although the actual time taken for the 12 statements is 00:00.015, the total time is therefore approx 10 mins.
So, my thinking is that if I can speed up this process, then I can speed up my sproc?
You should not use a varchar to store a numeric value, it will only cause you grief
. Casting all values from varchar to decimal is costly.
Change your table like
Now you can select with no problems