Greetings all (esp Terry !),
OK I'm quite new to SQL Server and Transact-SQL and know very little about getting the best performance.
I'm programming for a company and I have a massive query...Here it is...
SELECT S.sys_description AS STORE, V.Territory_Code, (IsNull(Sum(V.Conn_Profit),0) + IsNull(Sum(V.Prod_Profit),0)) as Profit, T.Profit_Target, CASE T.Profit_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V.Conn_Profit),0) + IsNull(Sum(V.Prod_Profit),0))/T.Profit_Target END AS Profit_Perf, Sum(V.LeaderBill) AS LBU, Sum(V.LeaderBillSimOnly) as LBSO, Sum(V.FR_Upgrade) as FRU, Sum(V.FR_New) as FRN, T.FRunner_Target, CASE T.FRunner_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V.FR_Upgrade),0) + IsNull(Sum(V.FR_New),0))/T.FRunner_Target END AS FR_Perf, Sum(V.PS_Upgrade) as PSU, Sum(V.PS_New) as PSN, T.PSaver_Target, CASE T.PSaver_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V.PS_Upgrade),0) + IsNull(Sum(V.PS_New),0))/T.PSaver_Target END AS PS_Perf, Sum(V.Upgrade_PlanInc) AS UGPlanInc, Sum(V.Other_New) AS OtherNEW, (IsNull(Sum(V.LeaderBill),0) + IsNull(Sum(V.LeaderBillSimOnly),0) + IsNull(Sum(V.FR_New),0) + IsNull(Sum(V.PS_New),0) + IsNull(Sum(V.Other_New),0)) AS TotalNew, (IsNull(Sum(V.FR_Upgrade),0) + IsNull(Sum(V.PS_Upgrade),0)) AS TotalUG, (IsNull(Sum(V.FR_New),0) + IsNull(Sum(V.PS_New),0) + IsNull(Sum(V.FR_Upgrade),0) + IsNull(Sum(V.PS_Upgrade),0)) AS Handsets FROM (VW_Conn_Summary_NCC V INNER JOIN system_table S ON V.Territory_Code = S.sys_tbl_code AND S.sys_tbl_type = 'TC' AND V.Invoice_Date >='01-Sep-2001' AND V.Invoice_Date <='10-Sep-2001') LEFT JOIN tbl_StoreTargets T ON V.Territory_Code = T.Territory AND T.ST_Month = '01-Sep-2001' GROUP BY S.sys_description, T.FRunner_Target, T.PSaver_Target, T.Profit_Target, V.Territory_Code
UNION
SELECT CCR.Dept AS STORE, '900' AS Terr_Code, (IsNull(Sum(V2.Conn_Profit),0) + IsNull(Sum(V2.Prod_Profit),0)) as Profit, T.Profit_Target, CASE T.Profit_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V2.Conn_Profit),0) + IsNull(Sum(V2.Prod_Profit),0))/T.Profit_Target END AS Profit_Perf, Sum(V2.LeaderBill) AS LBU, Sum(V2.LeaderBillSimOnly) as LBSO, Sum(V2.FR_Upgrade) as FRU, Sum(V2.FR_New) as FRN, T.FRunner_Target, CASE T.FRunner_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V2.FR_Upgrade),0) + IsNull(Sum(V2.FR_New),0))/T.FRunner_Target END AS FR_Perf, Sum(V2.PS_Upgrade) as PSU, Sum(V2.PS_New) as PSN, T.PSaver_Target, CASE T.PSaver_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V2.PS_Upgrade),0) + IsNull(Sum(V2.PS_New),0))/T.PSaver_Target END AS PS_Perf, Sum(V2.Upgrade_PlanInc) AS UGPlanInc, Sum(V2.Other_New) AS OtherNEW, (IsNull(Sum(V2.LeaderBill),0) + IsNull(Sum(V2.LeaderBillSimOnly),0) + IsNull(Sum(V2.FR_New),0) + IsNull(Sum(V2.PS_New),0) + IsNull(Sum(V2.Other_New),0)) AS TotalNew, (IsNull(Sum(V2.FR_Upgrade),0) + IsNull(Sum(V2.PS_Upgrade),0)) AS TotalUG, (IsNull(Sum(V2.FR_New),0) + IsNull(Sum(V2.PS_New),0) + IsNull(Sum(V2.FR_Upgrade),0) + IsNull(Sum(V2.PS_Upgrade),0)) AS Handsets FROM (VW_Conn_Summary_CC V2 INNER JOIN CallCentreReps CCR ON V2.Rep_Code = CCR.Rep_Code AND V2.Invoice_Date >='01-Sep-2001' AND V2.Invoice_Date <='10-Sep-2001') LEFT JOIN tbl_StoreTargets T ON T.Territory=900 AND T.ST_Month = '01-Sep-2001' GROUP BY CCR.Dept, T.FRunner_Target, T.PSaver_Target, T.Profit_Target ORDER BY Profit DESC
This query is (not surpisingly) very slow. I have heard that you can get much better performance by creating a view. The trouble is views are hard-coded, aren't they ? This query relies on parameters to be entered for date (and in some instances 'Territory'), so I build it on the fly.
What could I look at doing here to improve performance ?
What about creating indices ?
You can probably tell I'm struggling here, so any suggestions are like gold to me.
Brett.
OK I'm quite new to SQL Server and Transact-SQL and know very little about getting the best performance.
I'm programming for a company and I have a massive query...Here it is...
SELECT S.sys_description AS STORE, V.Territory_Code, (IsNull(Sum(V.Conn_Profit),0) + IsNull(Sum(V.Prod_Profit),0)) as Profit, T.Profit_Target, CASE T.Profit_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V.Conn_Profit),0) + IsNull(Sum(V.Prod_Profit),0))/T.Profit_Target END AS Profit_Perf, Sum(V.LeaderBill) AS LBU, Sum(V.LeaderBillSimOnly) as LBSO, Sum(V.FR_Upgrade) as FRU, Sum(V.FR_New) as FRN, T.FRunner_Target, CASE T.FRunner_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V.FR_Upgrade),0) + IsNull(Sum(V.FR_New),0))/T.FRunner_Target END AS FR_Perf, Sum(V.PS_Upgrade) as PSU, Sum(V.PS_New) as PSN, T.PSaver_Target, CASE T.PSaver_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V.PS_Upgrade),0) + IsNull(Sum(V.PS_New),0))/T.PSaver_Target END AS PS_Perf, Sum(V.Upgrade_PlanInc) AS UGPlanInc, Sum(V.Other_New) AS OtherNEW, (IsNull(Sum(V.LeaderBill),0) + IsNull(Sum(V.LeaderBillSimOnly),0) + IsNull(Sum(V.FR_New),0) + IsNull(Sum(V.PS_New),0) + IsNull(Sum(V.Other_New),0)) AS TotalNew, (IsNull(Sum(V.FR_Upgrade),0) + IsNull(Sum(V.PS_Upgrade),0)) AS TotalUG, (IsNull(Sum(V.FR_New),0) + IsNull(Sum(V.PS_New),0) + IsNull(Sum(V.FR_Upgrade),0) + IsNull(Sum(V.PS_Upgrade),0)) AS Handsets FROM (VW_Conn_Summary_NCC V INNER JOIN system_table S ON V.Territory_Code = S.sys_tbl_code AND S.sys_tbl_type = 'TC' AND V.Invoice_Date >='01-Sep-2001' AND V.Invoice_Date <='10-Sep-2001') LEFT JOIN tbl_StoreTargets T ON V.Territory_Code = T.Territory AND T.ST_Month = '01-Sep-2001' GROUP BY S.sys_description, T.FRunner_Target, T.PSaver_Target, T.Profit_Target, V.Territory_Code
UNION
SELECT CCR.Dept AS STORE, '900' AS Terr_Code, (IsNull(Sum(V2.Conn_Profit),0) + IsNull(Sum(V2.Prod_Profit),0)) as Profit, T.Profit_Target, CASE T.Profit_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V2.Conn_Profit),0) + IsNull(Sum(V2.Prod_Profit),0))/T.Profit_Target END AS Profit_Perf, Sum(V2.LeaderBill) AS LBU, Sum(V2.LeaderBillSimOnly) as LBSO, Sum(V2.FR_Upgrade) as FRU, Sum(V2.FR_New) as FRN, T.FRunner_Target, CASE T.FRunner_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V2.FR_Upgrade),0) + IsNull(Sum(V2.FR_New),0))/T.FRunner_Target END AS FR_Perf, Sum(V2.PS_Upgrade) as PSU, Sum(V2.PS_New) as PSN, T.PSaver_Target, CASE T.PSaver_Target WHEN 0 THEN 0 ELSE (IsNull(Sum(V2.PS_Upgrade),0) + IsNull(Sum(V2.PS_New),0))/T.PSaver_Target END AS PS_Perf, Sum(V2.Upgrade_PlanInc) AS UGPlanInc, Sum(V2.Other_New) AS OtherNEW, (IsNull(Sum(V2.LeaderBill),0) + IsNull(Sum(V2.LeaderBillSimOnly),0) + IsNull(Sum(V2.FR_New),0) + IsNull(Sum(V2.PS_New),0) + IsNull(Sum(V2.Other_New),0)) AS TotalNew, (IsNull(Sum(V2.FR_Upgrade),0) + IsNull(Sum(V2.PS_Upgrade),0)) AS TotalUG, (IsNull(Sum(V2.FR_New),0) + IsNull(Sum(V2.PS_New),0) + IsNull(Sum(V2.FR_Upgrade),0) + IsNull(Sum(V2.PS_Upgrade),0)) AS Handsets FROM (VW_Conn_Summary_CC V2 INNER JOIN CallCentreReps CCR ON V2.Rep_Code = CCR.Rep_Code AND V2.Invoice_Date >='01-Sep-2001' AND V2.Invoice_Date <='10-Sep-2001') LEFT JOIN tbl_StoreTargets T ON T.Territory=900 AND T.ST_Month = '01-Sep-2001' GROUP BY CCR.Dept, T.FRunner_Target, T.PSaver_Target, T.Profit_Target ORDER BY Profit DESC
This query is (not surpisingly) very slow. I have heard that you can get much better performance by creating a view. The trouble is views are hard-coded, aren't they ? This query relies on parameters to be entered for date (and in some instances 'Territory'), so I build it on the fly.
What could I look at doing here to improve performance ?
What about creating indices ?
You can probably tell I'm struggling here, so any suggestions are like gold to me.
Brett.