When performing the following query as either a SP or from Query
Analyser,there is a substantial difference in performance between using
variables for the date range (>50sec) and hard coding the date (instant).
The tbl_unit_result_log.date field has an index. The index does not appear
to be used when variables are implemented (Table Scan), but are, when hard
coding the dates.- This table has 1.8 million rows.
declare @user_tag nchar(5)
declare @startd datetime
declare @endd datetime
set @user_tag='2001'
set @startd='2001-10-16 0:0:0'
set @endd='2001-10-16 23:0:0'
SELECT
tbl_Unit_Result_Matrix.description,
count(tbl_Unit_Result_Log.Result_Type)
FROM
tbl_Unit_Result_Log INNER JOIN tbl_Admin_Users ON
tbl_Unit_Result_Log.User_Tag = tbl_Admin_Users.User_Tag
INNER JOIN tbl_Unit_Result_Matrix ON
tbl_Unit_Result_Log.Result_Type=tbl_Unit_Result_Matrix.Result_Type
WHERE
tbl_Unit_Result_Log.Date between @startd and @endd
and tbl_Unit_Result_Log.User_Tag=@user_tag
group by tbl_Unit_Result_Matrix.description
Does anyone know how the performance can be improved when using variables
Thank you