Why does a conditional not affect query speed?
- by Telos
I have a stored procedure that was taking a "long" period of time to execute. The query only needs to return data in one case, so I figured I could check for that case and just return before hitting the actual query.
The only problem is that it still takes the same amount of time to execute with an if statement. I have verified that the code inside the if is not executing, and that if I replace the complex query with a simple select the speed is fine... so now I'm confused.
Why is the query being slowed down by code that doesn't get executed when the conditional is false?
Here's the query itself:
ALTER PROCEDURE [dbo].[pr_cbc_GetCokeInfo]
@pa_record int,
@pb_record int
AS
BEGIN
SET NOCOUNT ON;
declare @ticketRec int
SELECT @ticketRec = TicketRecord
FROM eservice_live..v_sdticket
where TicketRecord=@pa_record
AND serviceCompanyID = 1139
AND @pb_record IS NULL
if @ticketRec IS NULL return
select record = null,
doc_ref = @pa_record,
memo_type = 'I',
memo = 'Bottler: ' + isnull(Bottler, '') + '
' + 'Sales Loc: ' + isnull(SalesLocation, '') + '
' + 'Outlet Desc: ' + isnull(OutletDesc, '') + '
' + 'City: ' + isnull(OutletCity, '') + '
' + 'EquipNo: ' + isnull(EquipNo, '') + '
' + 'SerialNo: ' + isnull(SerialNo, '') + '
' + 'PhaseNo: ' + isnull(cast(PhaseNo as varchar(255)), '') + '
' + 'StaticIP: ' + isnull(StaticIP, '') + '
' + 'Air Card: ' + isnull(AirCard, '')
FROM eservice_live..v_SDExtendedInfoField ef
JOIN eservice_live..CokeSNList csl ON ef.valueText=csl.SerialNo
where ef.docType='CLH'
AND ef.docref = @ticketRec
AND ef.ExtendedDocNumber=5
SET NOCOUNT OFF;
END