Question : Query too slow

Dear Experts,

The following question takes 30+ seconds to execute.  How can I speed it up to less than 1 second?

DECLARE @variable_one VARCHAR(4) SET @variable_one = 'xyz'
DECLARE @total TABLE ( total FLOAT )

INSERT INTO @total ( total )
SELECT SUM ( T.table1 )
FROM table1 T
JOIN table2 M ON M.M_ID = T.M_ID
JOIN table3 P ON P.E_ID = M.B_E_ID AND P.DEPARTMENT = @variable_one
WHERE [status] IN ( 'w', 'p' )
AND T.flag='B'

DECLARE @total_fig FLOAT SET @total_fig = ( SELECT total FROM @total )
SELECT P.E_NAME As [name], ( ( SUM ( T.table1 ) / @total_fig )) * 100 as [value], SUM ( T.table1 ) As [figure]
FROM table1 T
JOIN table2 M ON M.M_ID = T.M_ID
JOIN table3 P ON P.E_ID = M.B_E_ID AND P.DEPARTMENT = @variable_one
WHERE [status] IN ( 'w', 'p' )
AND T.flag = 'B'
GROUP BY P.E_NAME
ORDER BY P.E_NAME

Answer : Query too slow

I wouldn't attempt to combine both triggers and the use of a data layer such as nhibernate - you're always going to get synchronisation problems. nHibernate is intended to manage your data - and your business layer should manage processes such as updating your prices. Personally I don't go near triggers for any business logic - for auditing perhaps, but nothing else. I think they obscure business logic and make it nigh on impossible to implement other code solutions for managing the data - as you are finding.
Why not move your trigger code into a stored procedure and call the procedure from within the transaction scope of the update if the 'finished' flag is true? I would manage this within the business layer, but you could move this to the data layer if you wish.

Tim
Random Solutions  
 
programming4us programming4us