Wis it that Coalesce takes so much longer than 3 if statements?
IF Example
COALESCE Example:
[red]Stats Compare[/red]
Major differences are in red.
COALESCE
Client Execution Time 16:40:18 16:40:09 16:36:59
Query Profile Statistics
Number of INSERT, DELETE and UPDATE statements 0 0 0 0.0000
Rows affected by INSERT, DELETE, or UPDATE statements 0 0 0 0.0000
Number of SELECT statements 5 5 5 5.0000
Rows returned by SELECT statements 5490 5490 5490 [red]5490.0000[/red]
Number of transactions 0 0 0 0.0000
Network Statistics
Number of server roundtrips 3 3 3 3.0000
TDS packets sent from client 3 3 3 3.0000
TDS packets received from server 202 202 202 202.0000
Bytes sent from client 3972 3972 3972 3972.0000
Bytes received from server 818842 818842 818842 818842.0000
Time Statistics
Client processing time 187 203 188 192.6667
Total execution time 7655 8718 7640 8004.3330
Wait time on server replies 7468 8515 7452 [red]7811.6670[/red]
IF
Client Execution Time 16:40:06 16:39:59 16:36:49
Query Profile Statistics
Number of INSERT, DELETE and UPDATE statements 0 0 0 0.0000
Rows affected by INSERT, DELETE, or UPDATE statements 0 0 0 0.0000
Number of SELECT statements 8 8 8 8.0000
Rows returned by SELECT statements 0 0 0 [red]0.0000[/red]
Number of transactions 0 0 0 0.0000
Network Statistics
Number of server roundtrips 6 6 6 6.0000
TDS packets sent from client 7 7 7 7.0000
TDS packets received from server 204 204 204 204.0000
Bytes sent from client 5428 5428 5428 5428.0000
Bytes received from server 813094 813094 813094 813094.0000
Time Statistics
Client processing time 516 46 62 208.0000
Total execution time 3687 1499 1421 2202.3330
Wait time on server replies 3171 1453 1359 [red]1994.3330[/red]
Thanks
John Fuhrman
IF Example
Code:
USE [MailroomTracking]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: John Fuhrman
-- Create date: 2010/06/22
-- Description: QC Report
-- =============================================
SET NOCOUNT ON;
Declare
@Enter_Starting_Date DateTime,
@Enter_Ending_Date DateTime,
@Enter_Employee_Last_Name VarChar(50)
Set @Enter_Starting_Date = Null
Set @Enter_Ending_Date = Null
Set @Enter_Employee_Last_Name = Null
IF IsDate(@Enter_Starting_Date) <> 1
Set @Enter_Starting_Date = Convert(varchar,Getdate(),101)
IF IsDate(@Enter_Ending_Date) <> 1
Set @Enter_Ending_Date = Convert(varchar,Getdate(),101)
IF @Enter_Employee_Last_Name IS NULL
SET @Enter_Employee_Last_Name = '%'
SELECT TOP (100) PERCENT
dbo.tblTrackingTable.Tracking_ID,
EmployeeFullName = Case
When EmployeeMI = '' Then EmployeeLN + ', ' + EmployeeFN
When EmployeeMI Is Null Then EmployeeLN + ', ' + EmployeeFN
When EmployeeMI <> '' Then EmployeeLN + ', ' + EmployeeFN + ' ' + EmployeeMI
End,
dbo.tblEmployee.EmployeeFN,
dbo.tblEmployee.EmployeeLN,
dbo.tblTrackingTable.EmployeeID,
dbo.tblTrackingTable.MachineName,
UPPER(dbo.tblTrackingTable.BoxNumber) AS BoxNumber,
UPPER(dbo.tblTrackingTable.FileNumber) AS FileNumber,
dbo.tblTrackingTable.TrackingDate
FROM dbo.tblTrackingTable FULL OUTER JOIN
dbo.tblEmployee ON dbo.tblTrackingTable.EmployeeID = dbo.tblEmployee.EmployeeID
WHERE
(dbo.tblTrackingTable.TrackingDate BETWEEN
DATEADD(dd, DATEDIFF(dd, 0, @Enter_Starting_Date), 0)
And DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, @Enter_Ending_Date), 1)))
AND (dbo.tblEmployee.EmployeeLN Like @Enter_Employee_Last_Name + '%')
AND (dbo.tblTrackingTable.EmployeeID IS NOT NULL)
AND (dbo.tblTrackingTable.FileNumber <> '')
AND (dbo.tblTrackingTable.BoxNumber <> '')
AND (dbo.tblTrackingTable.FileNumber <> '.BOX.END.')
AND (dbo.tblTrackingTable.TrackingDate IS NOT NULL)
ORDER BY dbo.tblTrackingTable.TrackingDate, BoxNumber
COALESCE Example:
Code:
--Select
-- Coalesce(Null, DATEADD(dd, DATEDIFF(dd, 0, Getdate()), 0)), -- Beginning of Day
-- Coalesce(Null, DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, Getdate()), 1))) -- Ending of Day
--
Declare
@Enter_Starting_Date DateTime,
@Enter_Ending_Date DateTime,
@Enter_Employee_Last_Name VarChar(50)
Set @Enter_Starting_Date = Null
Set @Enter_Ending_Date = Null
Set @Enter_Employee_Last_Name = Null
SELECT TOP (100) PERCENT
dbo.tblTrackingTable.Tracking_ID,
EmployeeFullName = Case
When EmployeeMI = '' Then EmployeeLN + ', ' + EmployeeFN
When EmployeeMI Is Null Then EmployeeLN + ', ' + EmployeeFN
When EmployeeMI <> '' Then EmployeeLN + ', ' + EmployeeFN + ' ' + EmployeeMI
End,
dbo.tblEmployee.EmployeeFN,
dbo.tblEmployee.EmployeeLN,
dbo.tblTrackingTable.EmployeeID,
dbo.tblTrackingTable.MachineName,
UPPER(dbo.tblTrackingTable.BoxNumber) AS BoxNumber,
UPPER(dbo.tblTrackingTable.FileNumber) AS FileNumber,
dbo.tblTrackingTable.TrackingDate
FROM dbo.tblTrackingTable FULL OUTER JOIN
dbo.tblEmployee ON dbo.tblTrackingTable.EmployeeID = dbo.tblEmployee.EmployeeID
WHERE
(dbo.tblTrackingTable.TrackingDate
BETWEEN Coalesce(@Enter_Starting_Date, DATEADD(dd, DATEDIFF(dd, 0, Getdate()), 0)) -- Beginning of Day
And Coalesce(@Enter_Ending_Date, DATEADD(ss, - 1, DATEADD(dd, DATEDIFF(dd, 0, Getdate()), 1))) -- Ending of Day
AND (dbo.tblEmployee.EmployeeLN Like Coalesce(@Enter_Employee_Last_Name + '%','%'))
AND (dbo.tblTrackingTable.EmployeeID IS NOT NULL)
AND (dbo.tblTrackingTable.FileNumber <> '')
AND (dbo.tblTrackingTable.BoxNumber <> '')
AND (dbo.tblTrackingTable.FileNumber <> '.BOX.END.')
AND (dbo.tblTrackingTable.TrackingDate IS NOT NULL)
)
ORDER BY dbo.tblTrackingTable.EmployeeID, dbo.tblTrackingTable.TrackingDate, BoxNumber
[red]Stats Compare[/red]
Major differences are in red.
COALESCE
Client Execution Time 16:40:18 16:40:09 16:36:59
Query Profile Statistics
Number of INSERT, DELETE and UPDATE statements 0 0 0 0.0000
Rows affected by INSERT, DELETE, or UPDATE statements 0 0 0 0.0000
Number of SELECT statements 5 5 5 5.0000
Rows returned by SELECT statements 5490 5490 5490 [red]5490.0000[/red]
Number of transactions 0 0 0 0.0000
Network Statistics
Number of server roundtrips 3 3 3 3.0000
TDS packets sent from client 3 3 3 3.0000
TDS packets received from server 202 202 202 202.0000
Bytes sent from client 3972 3972 3972 3972.0000
Bytes received from server 818842 818842 818842 818842.0000
Time Statistics
Client processing time 187 203 188 192.6667
Total execution time 7655 8718 7640 8004.3330
Wait time on server replies 7468 8515 7452 [red]7811.6670[/red]
IF
Client Execution Time 16:40:06 16:39:59 16:36:49
Query Profile Statistics
Number of INSERT, DELETE and UPDATE statements 0 0 0 0.0000
Rows affected by INSERT, DELETE, or UPDATE statements 0 0 0 0.0000
Number of SELECT statements 8 8 8 8.0000
Rows returned by SELECT statements 0 0 0 [red]0.0000[/red]
Number of transactions 0 0 0 0.0000
Network Statistics
Number of server roundtrips 6 6 6 6.0000
TDS packets sent from client 7 7 7 7.0000
TDS packets received from server 204 204 204 204.0000
Bytes sent from client 5428 5428 5428 5428.0000
Bytes received from server 813094 813094 813094 813094.0000
Time Statistics
Client processing time 516 46 62 208.0000
Total execution time 3687 1499 1421 2202.3330
Wait time on server replies 3171 1453 1359 [red]1994.3330[/red]
Thanks
John Fuhrman