Question
CFStoredProc Behavior Issues
This has to be one of the craziest things I have seen with CF
in a long time. We have a stored proc that is not playing nicely.
When the stored proc, written in SQL 2005 is ran in Management
Studio, it runs in less than a second. When we try and run this
same proc in CF8 using cfstoredproc, it runs around 20+ secs. We
tried 3 different approaches to diagnose this problem but to no
avail.
1)
<cfstoredproc procedure="usp_University_Geography_Sales" datasource="#request.dsn#">
<cfprocparam type="IN" cfsqltype="CF_SQL_INTEGER" value="#companyid#">
<cfprocparam type="IN" cfsqltype="CF_SQL_VARCHAR" value="#dateFromLastYear#">
<cfprocparam type="IN" cfsqltype="CF_SQL_VARCHAR" value="#dateToLastYear#">
<cfprocparam type="IN" cfsqltype="CF_SQL_INTEGER" value="#timeyearid#">
<cfprocparam type="IN" cfsqltype="CF_SQL_VARCHAR" value="#reportType#">
<cfprocresult name="qryRTDLY">
</cfstoredproc>
2)
<cfquery name="qryRTDLY" datasource="#request.dsn#">
Exec spdr..usp_University_Geography_Sales #companyid#,'#dateFromLastYear#','#dateToLastYear#',#timeyearid#,'#reportType#'
</cfquery>
3)
DECLARE
@companyID int,
@startDate smalldatetime,
@endDate smalldatetime,
@timeYearID int,
@grouptype varchar(10)
SET @companyID = #companyid#
SET @startDate = '#dateFrom#'
SET @endDate = '#dateTo#'
SET @timeYearID = #timeyearid#
SET @grouptype = '#reportType#'
--Hardcode TimeYearDivisionID to '5' which is for all year
DECLARE @timeYearDivisionID INT
SET @timeYearDivisionID = 5
--Set GeographyType
DECLARE @geographyTypeID INT
IF @CompanyID = 2
SET @geographyTypeID = 8
IF @CompanyID = 32
SET @geographyTypeID = 9
IF @CompanyID NOT IN (2,32)
SET @geographyTypeID = 0
SELECT
@companyID companyID,
@timeYearID timeYearID,
@timeYearDivisionID timeYearDivisionID,
@startDate startDate,
@endDate endDate,
gzt.GeographyID,
g.GeographyDescription,
ISNULL(SUM(sales.sales),0) as Sales
FROM
Geography_Zip_Time gzt
INNER JOIN Geography g
ON gzt.GeographyID = g.GeographyID
AND gzt.TimeYearID = g.TimeYearID
AND gzt.TimeYearDivisionID = g.TimeYearDivisionID
LEFT JOIN
(
SELECT
inv.InvoiceShipToZipID as ShipToZipID,
SUM(inv.invoicelinetotal) sales
FROM
SPDRWarehouse.dbo.InvoiceHistory inv
INNER JOIN dbo.udf_Active_Universities_by_CompanyID(@companyID, @grouptype) Unv
ON inv.InvoiceToAccountid = Unv.accountid
INNER JOIN Item
ON inv.itemid = Item.itemid
AND Item.ItemClassCode between 'AA' and 'ZZ'
WHERE
inv.InvoiceDate BETWEEN @startDate AND @endDate
GROUP BY
inv.InvoiceShipToZipID
) sales
ON gzt.ZipID = sales.ShipToZipID
WHERE
gzt.TimeYearID = @TimeYearID
AND gzt.TimeYearDivisionID = 5
AND gzt.geographytypeid = @geographyTypeID
GROUP BY
gzt.GeographyID,
g.GeographyDescription
1 and 2 take 20+ secs, 3 is less than a second. All 3 are the same query just called differently. Any ideas why?? Thanks in advance.
Kevin W. Ryan
Senior Database Applications Engineer
1)
<cfstoredproc procedure="usp_University_Geography_Sales" datasource="#request.dsn#">
<cfprocparam type="IN" cfsqltype="CF_SQL_INTEGER" value="#companyid#">
<cfprocparam type="IN" cfsqltype="CF_SQL_VARCHAR" value="#dateFromLastYear#">
<cfprocparam type="IN" cfsqltype="CF_SQL_VARCHAR" value="#dateToLastYear#">
<cfprocparam type="IN" cfsqltype="CF_SQL_INTEGER" value="#timeyearid#">
<cfprocparam type="IN" cfsqltype="CF_SQL_VARCHAR" value="#reportType#">
<cfprocresult name="qryRTDLY">
</cfstoredproc>
2)
<cfquery name="qryRTDLY" datasource="#request.dsn#">
Exec spdr..usp_University_Geography_Sales #companyid#,'#dateFromLastYear#','#dateToLastYear#',#timeyearid#,'#reportType#'
</cfquery>
3)
DECLARE
@companyID int,
@startDate smalldatetime,
@endDate smalldatetime,
@timeYearID int,
@grouptype varchar(10)
SET @companyID = #companyid#
SET @startDate = '#dateFrom#'
SET @endDate = '#dateTo#'
SET @timeYearID = #timeyearid#
SET @grouptype = '#reportType#'
--Hardcode TimeYearDivisionID to '5' which is for all year
DECLARE @timeYearDivisionID INT
SET @timeYearDivisionID = 5
--Set GeographyType
DECLARE @geographyTypeID INT
IF @CompanyID = 2
SET @geographyTypeID = 8
IF @CompanyID = 32
SET @geographyTypeID = 9
IF @CompanyID NOT IN (2,32)
SET @geographyTypeID = 0
SELECT
@companyID companyID,
@timeYearID timeYearID,
@timeYearDivisionID timeYearDivisionID,
@startDate startDate,
@endDate endDate,
gzt.GeographyID,
g.GeographyDescription,
ISNULL(SUM(sales.sales),0) as Sales
FROM
Geography_Zip_Time gzt
INNER JOIN Geography g
ON gzt.GeographyID = g.GeographyID
AND gzt.TimeYearID = g.TimeYearID
AND gzt.TimeYearDivisionID = g.TimeYearDivisionID
LEFT JOIN
(
SELECT
inv.InvoiceShipToZipID as ShipToZipID,
SUM(inv.invoicelinetotal) sales
FROM
SPDRWarehouse.dbo.InvoiceHistory inv
INNER JOIN dbo.udf_Active_Universities_by_CompanyID(@companyID, @grouptype) Unv
ON inv.InvoiceToAccountid = Unv.accountid
INNER JOIN Item
ON inv.itemid = Item.itemid
AND Item.ItemClassCode between 'AA' and 'ZZ'
WHERE
inv.InvoiceDate BETWEEN @startDate AND @endDate
GROUP BY
inv.InvoiceShipToZipID
) sales
ON gzt.ZipID = sales.ShipToZipID
WHERE
gzt.TimeYearID = @TimeYearID
AND gzt.TimeYearDivisionID = 5
AND gzt.geographytypeid = @geographyTypeID
GROUP BY
gzt.GeographyID,
g.GeographyDescription
1 and 2 take 20+ secs, 3 is less than a second. All 3 are the same query just called differently. Any ideas why?? Thanks in advance.
Kevin W. Ryan
Senior Database Applications Engineer
