April 16, 2013 at 5:14 am
Hi,
I have a SQL query with couple of nested queries (query at the bottom for the reference), now the very first time I execute the query on a SQL Server instance, it takes around 50-60 seconds to execute, but there on for every consecutive execution, it barely takes 1 second and I am baffled, because I want it to take its original time for me to do anything about reduction of its original execution time. I want to try various tuning and query modification aspects.
I have tried clearing all caches using the commands listed underneath, but they does not help in getting the original execution time.
We are using "SQL Server 2012 Enterprise Edition"
use <Database_Name>
GO
CHECKPOINT;
GO
DBCC FLUSHPROCINDB(<Database_ID>)
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
DBCC FREESYSTEMCACHE ('ALL')
DBCC FREESESSIONCACHE
GO
Normally anyone would be happy that query is performing great after first execution but my aim is different, I want it to take actual time for every consecutive execution
Please help me to resolve the issue mentioned above, any pointer would be great, please let me know if you need more details
thanks,
Mrinal
Query Text for Reference
=======================
SELECT
OprtngOrigHandlingNodeID,
OprtngDestHandlingNodeID,
ShpmtOrigHandlingNodeID,
ShpmtDestHandlingNodeID,
CAST(COUNT(*) as real)/ (
SELECT ABS(DATEDIFF(d,'2013-02-01','2013-02-28'))+1 as DaysInRange
) AS cars
FROM (
select distinct S.*
from RBIS_UI.Trip_Plan TP,
RBIS_UI.combined_shipments S,
(
SELECT *
FROM (
SELECT T.TrainRunID,
T.Rundate,
T.TrainRunCode,
T.Distance,
T.Speed,
T.Cars,
T.InterModal,
T.Length,
T.Weight,
T.WorkStops,
T.NonDesignStops,
T.HP,
T.HPTT,
T.ActLocoCount,
T.DeactLocoCount,
T.TotalLocoCount,
T.DepDesignTimeDiff,
T.ArrDesignTimeDiff,
T.TransitTime,
T.DesignTransitTime,
T.LocalArrtime AS Arrtime,
T.LocalDeptime AS Deptime,
T.LocalDepSchedTime as DepSchedTime,
T.LocalArrSchedTime as ArrSchedTime,
T.DepDelay,
T.ArrDelay,
T.titanID,
T.tyesID,
T.CorCapacity,
COALESCE (dt_TrainName, trainname) as trainname,
COALESCE (dt_OpsTypeId, OpsTypeID) as OpsTypeID,
COALESCE (dt_BizType, BizType) as BizType,
COALESCE (dt_ActualOrigHandlingNodeID, ActualOrigHandlingNodeID) as ActualOrigHandlingNodeID,
COALESCE (dt_ActualDestHandlingNodeID, ActualDestHandlingNodeID) as ActualDestHandlingNodeID,
ActualOrigHandlingNodeID as Actual_ActualOrigHandlingNodeID,
ActualDestHandlingNodeID as Actual_ActualDestHandlingNodeID,
DesignOrigHandlingNodeID,
DesignDestHandlingNodeID,
OpsTypeID as Actual_OpsTypeID,
BizType as Actual_BizType,
TrainName as Actual_TrainName,
T.DesigntrainID,
DETOURFLAG,
Via,
CarsLoaded,
IntermodalLoaded,
CarsEmpty,
IntermodalEmpty,
Addition -- ,
-- DepDesignTimeDiff,
--ArrDesignTimeDiff
FROM (
SELECT *
FROM (
SELECT * FROM RBIS_UI.train_runs
WHERE RunDate BETWEEN '2013-02-01' AND
'2013-02-28'
) FT
WHERE OpsTypeID IN ( 1,2,3,5,6,9 ) ) T
) TR
WHERE ((TR.TrainName = '470')) AND ((TR.ActualOrigHandlingNodeID = 1190)) AND ((TR.ActualDestHandlingNodeID = 504)) AND ((TR.DesignTrainID = 1901)) AND ((TR.OpsTypeID = 3)) AND ((TR.BizType = 'SECO'))) TR
where TR.TrainRunID = TP.TrainRunID
and TP.CSN = S.CycleSerialID
and TP.Shipment_ReleaseDate = S.ReleaseDate
AND TP.EqpmtType <> 2 ) FS
GROUP BY GROUPING SETS (
(OprtngOrigHandlingNodeID),
(OprtngDestHandlingNodeID),
(ShpmtOrigHandlingNodeID),
(ShpmtDestHandlingNodeID)
)
April 16, 2013 at 5:51 am
mrinal.technology (4/16/2013)
Hi,I have a SQL query with couple of nested queries (query at the bottom for the reference), now the very first time I execute the query on a SQL Server instance, it takes around 50-60 seconds to execute, but there on for every consecutive execution, it barely takes 1 second and I am baffled, because I want it to take its original time for me to do anything about reduction of its original execution time. I want to try various tuning and query modification aspects.
I have tried clearing all caches using the commands listed underneath, but they does not help in getting the original execution time.
We are using "SQL Server 2012 Enterprise Edition"
...
Can you please post the actual execution plan (as attachments) for a slow run and a fast run? If you're not sure how to do this, there are instructions in the first link in my sig.
Please detail how you will get a new "very first time" now that it's been run.
The query is unnecessarily convoluted and has potentially serious design flaws. Unless this is a deliberate attempt to generate a poor plan, I'd consider rewriting it as a higher priority than performance concerns.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 16, 2013 at 6:17 am
Challenge here is that Query is not performing bad, in fact after first execution it takes less than 1 second, which is good, but I want it to revert back to original duration and then I can plan re write, if various other optimization steps fail. I do not want it rely on various caches to start with. If after first execution I cannot get the original time, then there's little I can do to to optimize
Currently, I am attaching the execution plan after first execution where it takes less than 1 second, as per a colleague, strange part is that there's no difference in the execution plan in the either case
April 17, 2013 at 2:11 am
Hi,
Issue resolved, our database has the auto create and update statistics on, so first time it was slow due to statistics creation and thereon since the optimum plan was generated to it use to be very fast.
I am planning to manually update stats to avoid the first time slow execution. Thanks for the suggestion
thanks,
Mrinal
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply