I initially thought you were on to something here. Working assumption was along the lines that perhaps the buffer pool wasn't immediately flushed as it requires "some work" to do so and why bother until the memory was required. But...
Your test is flawed.
What you're seeing in the buffer pool is the pages read as a result of re-attaching the database, not the remains of the previous instance of the database.
And we can see that the buffer pool was not totally blown away by the
detach/attach. Seems like my buddy was wrong. Does anyone disagree or
have a better argument?
Yes. You're interpreting physical reads 0
as meaning there were not any physical reads
Table 'DatabaseLog'. Scan count 1, logical reads 782, physical reads
0, read-ahead reads 768, lob logical reads 94, lob physical reads 4,
lob read-ahead reads 24.
As described on Craig Freedman's blog the sequential read ahead mechanism tries to ensure that pages are in memory before they're requested by the query processor, which is why you see zero or a lower than expected physical read count reported.
When SQL Server performs a sequential scan of a large table, the
storage engine initiates the read ahead mechanism to ensure that pages
are in memory and ready to scan before they are needed by the query
processor. The read ahead mechanism tries to stay 500 pages ahead of
the scan.
None of the pages required to satisfy your query were in memory until read-ahead put them there.
As to why online/offline results in a different buffer pool profile warrants a little more idle investigation. @MarkSRasmussen might be able to help us out with that next time he visits.
Is there a way to make this top part loop 13 times after the initial run? only included the first 3 to save space.
DECLARE @BegDate AS DATE, @EndDate as Date
SET @BegDate = '2017-11-19'
SET @EndDate = DATEADD(day, 13, @BegDate)
IF OBJECT_ID('tempdb..#TEMPLIST') IS NOT NULL DROP TABLE #TEMPLIST
SELECT *
INTO #TEMPLIST
FROM
( SELECT tRegions.Region, COALESCE(tSubregions.Description,'') as SubRegion, tServiceReps.[Service Rep] as Service_Rep, tServiceReps.EmployeeNumber,
DATEADD(day, 0, @BegDate) as Date, DATENAME(dw,@BegDate) as DOW
FROM UnitDatabase.dbo.tServiceReps as tServiceReps
LEFT OUTER JOIN UnitDatabase.dbo.tSubregions as tSubregions on tServiceReps.Subregion = tSubregions.ID
LEFT OUTER JOIN UnitDatabase.dbo.tRegions as tRegions on tServiceReps.RegionCode = tRegions.RegionAcctCode and tRegions.Deactivated = 0
Where tServiceReps.RepID IN
( SELECT SR.RepID
FROM tServiceReps SR
LEFT JOIN tServiceReports SREP ON SR.RepID = SREP.Employee AND SREP.Date between @BegDate and @EndDate
LEFT JOIN tServiceHours AS SREPHRS ON SREP.ReportNo = SREPHRS.ReportNo
WHERE (SR.Deactivated = 0 AND SR.AllowUserToCreateServiceReport = 1)
OR (COALESCE(SREPHRS.Hours,0) + COALESCE(SREPHRS.OTHours,0)) > 0
GROUP BY SR.RepID )
) AS TMP
INSERT
INTO #TEMPLIST (Region, Subregion, Service_Rep, EmployeeNumber, Date, DOW)
( SELECT tRegions.Region, COALESCE(tSubregions.Description,'') as SubRegion, tServiceReps.[Service Rep] as Service_Rep, tServiceReps.EmployeeNumber,
DATEADD(day, 1, @BegDate) as Date, DATENAME(dw,DATEADD(day, 1, @BegDate)) as DOW
FROM UnitDatabase.dbo.tServiceReps as tServiceReps
LEFT OUTER JOIN UnitDatabase.dbo.tSubregions as tSubregions on tServiceReps.Subregion = tSubregions.ID
LEFT OUTER JOIN UnitDatabase.dbo.tRegions as tRegions on tServiceReps.RegionCode = tRegions.RegionAcctCode and tRegions.Deactivated = 0
Where tServiceReps.RepID IN
( SELECT SR.RepID
FROM tServiceReps SR
LEFT JOIN tServiceReports SREP ON SR.RepID = SREP.Employee AND SREP.Date between @BegDate and @EndDate
LEFT JOIN tServiceHours AS SREPHRS ON SREP.ReportNo = SREPHRS.ReportNo
WHERE (SR.Deactivated = 0 AND SR.AllowUserToCreateServiceReport = 1)
OR (COALESCE(SREPHRS.Hours,0) + COALESCE(SREPHRS.OTHours,0)) > 0
GROUP BY SR.RepID ))
INSERT
INTO #TEMPLIST (Region, Subregion, Service_Rep, EmployeeNumber, Date, DOW)
( SELECT tRegions.Region, COALESCE(tSubregions.Description,'') as SubRegion, tServiceReps.[Service Rep] as Service_Rep, tServiceReps.EmployeeNumber,
DATEADD(day, 2, @BegDate) as Date, DATENAME(dw,DATEADD(day, 2, @BegDate)) as DOW
FROM UnitDatabase.dbo.tServiceReps as tServiceReps
LEFT OUTER JOIN UnitDatabase.dbo.tSubregions as tSubregions on tServiceReps.Subregion = tSubregions.ID
LEFT OUTER JOIN UnitDatabase.dbo.tRegions as tRegions on tServiceReps.RegionCode = tRegions.RegionAcctCode and tRegions.Deactivated = 0
Where tServiceReps.RepID IN
( SELECT SR.RepID
FROM tServiceReps SR
LEFT JOIN tServiceReports SREP ON SR.RepID = SREP.Employee AND SREP.Date between @BegDate and @EndDate
LEFT JOIN tServiceHours AS SREPHRS ON SREP.ReportNo = SREPHRS.ReportNo
WHERE (SR.Deactivated = 0 AND SR.AllowUserToCreateServiceReport = 1)
OR (COALESCE(SREPHRS.Hours,0) + COALESCE(SREPHRS.OTHours,0)) > 0
GROUP BY SR.RepID ))
INSERT
INTO #TEMPLIST (Region, Subregion, Service_Rep, EmployeeNumber, Date, DOW)
( SELECT tRegions.Region, COALESCE(tSubregions.Description,'') as SubRegion, tServiceReps.[Service Rep] as Service_Rep, tServiceReps.EmployeeNumber,
DATEADD(day, 3, @BegDate) as Date, DATENAME(dw,DATEADD(day, 3, @BegDate)) as DOW
FROM UnitDatabase.dbo.tServiceReps as tServiceReps
LEFT OUTER JOIN UnitDatabase.dbo.tSubregions as tSubregions on tServiceReps.Subregion = tSubregions.ID
LEFT OUTER JOIN UnitDatabase.dbo.tRegions as tRegions on tServiceReps.RegionCode = tRegions.RegionAcctCode and tRegions.Deactivated = 0
Where tServiceReps.RepID IN
( SELECT SR.RepID
FROM tServiceReps SR
LEFT JOIN tServiceReports SREP ON SR.RepID = SREP.Employee AND SREP.Date between @BegDate and @EndDate
LEFT JOIN tServiceHours AS SREPHRS ON SREP.ReportNo = SREPHRS.ReportNo
WHERE (SR.Deactivated = 0 AND SR.AllowUserToCreateServiceReport = 1)
OR (COALESCE(SREPHRS.Hours,0) + COALESCE(SREPHRS.OTHours,0)) > 0
GROUP BY SR.RepID ))
--repeats 14 times total
select TMPLST.Region, TMPLST.Subregion, TMPLST.Service_Rep,
TMPLST.EmployeeNumber, TMPLST.Date, TMPLST.DOW, COALESCE(REST.REGWORK,0) AS
REGWORK, COALESCE(REST.OTWORK,0) AS OTWORK, COALESCE(REST.PTO,0) AS PTO,
COALESCE(REST.HOL,0) AS HOL, COALESCE(REST.SICK,0) AS SICK,
COALESCE(REST.JURY,0) AS JURY, COALESCE(REST.MIL,0) AS MIL,
COALESCE(REST.OTHERPTO,0) AS OTHERPOT, COALESCE(REST.TOTALWORK,0) AS
TOTALWORK, COALESCE(REST.TOTALOFF,0) AS TOTALOFF
FROM #TEMPLIST TMPLST
LEFT JOIN (
SELECT Region, SubRegion, Service_Rep, EmployeeNumber, Date, DOW,
SUM(REGWORK) AS REGWORK, SUM(OTWORK) AS OTWORK, SUM(PTO) AS PTO, SUM(HOL) AS
HOL, SUM(SICK) AS SICK, SUM(JURY) AS JURY, SUM(MIL) AS MIL, SUM(OTHERPTO) AS
OTHERPTO, SUM(REGWORK) + SUM(OTWORK) AS TOTALWORK, SUM(PTO) + SUM(HOL) +
SUM(SICK) + SUM(JURY) + SUM(MIL) + SUM(OTHERPTO) AS TOTALOFF
FROM (
Select tRegions.Region
, COALESCE(tSubregions.Description,'') as
SubRegion
, tServiceReps.[Service Rep] as Service_Rep
, tServiceReps.EmployeeNumber
, CAST(tServiceReports.Date AS DATE) DATE
, DATENAME(dw,tServiceReports.Date) as DOW
, CASE WHEN JobCode = 85 THEN
SUM(COALESCE(tServiceHours.Hours,0) + COALESCE(tServiceHours.OTHours,0)) ELSE 0 END AS PTO
, CASE WHEN JobCode = 86 THEN
SUM(COALESCE(tServiceHours.Hours,0) + COALESCE(tServiceHours.OTHours,0)) ELSE 0 END AS HOL
, CASE WHEN JobCode = 92 THEN
SUM(COALESCE(tServiceHours.Hours,0) + COALESCE(tServiceHours.OTHours,0)) ELSE 0 END AS JURY
, CASE WHEN JobCode = 93 THEN SUM(COALESCE(tServiceHours.Hours,0) + COALESCE(tServiceHours.OTHours,0)) ELSE 0 END AS MIL
, CASE WHEN JobCode = 89 THEN SUM(COALESCE(tServiceHours.Hours,0) + COALESCE(tServiceHours.OTHours,0)) ELSE 0 END AS OTHERPTO
, CASE WHEN JobCode = 88 THEN SUM(COALESCE(tServiceHours.Hours,0) + COALESCE(tServiceHours.OTHours,0)) ELSE 0 END AS SICK
, CASE WHEN JobCode NOT IN (85,86,92,93,89,88) THEN SUM(COALESCE(tServiceHours.Hours,0)) ELSE 0 END AS REGWORK
, CASE WHEN JobCode NOT IN (85,86,92,93,89,88) THEN SUM(COALESCE(tServiceHours.OTHours,0)) ELSE 0 END AS OTWORK
From UnitDatabase.dbo.tServiceReps as tServiceReps
LEFT OUTER JOIN UnitDatabase.dbo.tServiceReports as tServiceReports ON (tServiceReports.Employee = tServiceReps.RepID) AND (tServiceReports.Date between @BegDate and @EndDate)
LEFT OUTER JOIN UnitDatabase.dbo.tServiceHours as tServiceHours on (tServiceReports.ReportNo = tServiceHours.ReportNo)
LEFT OUTER JOIN UnitDatabase.dbo.tServiceJobCodes as tServiceJobCodes on (tServiceHours.JobCode = tServiceJobCodes.ServiceJobCodes)
LEFT OUTER JOIN UnitDatabase.dbo.tSubregions as tSubregions on tServiceReps.Subregion = tSubregions.ID
LEFT OUTER JOIN UnitDatabase.dbo.tRegions as tRegions on tServiceReps.RegionCode = tRegions.RegionAcctCode and tRegions.Deactivated = 0
GROUP BY tRegions.Region, COALESCE(tSubregions.Description,''), tServiceReps.[Service Rep], tServiceReps.EmployeeNumber, tServiceReports.Date, DATENAME(dw,tServiceReports.Date), JobCode
) TMP
GROUP BY Region, SubRegion, Service_Rep, EmployeeNumber, Date, DOW
) AS REST ON TMPLST.Service_Rep = REST.Service_Rep AND TMPLST.Date = REST.Date
Order by TMPLST.Region,
TMPLST.Service_Rep,
TMPLST.Date
Best Answer
Use your main query as a subquery: