SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Query - Actual performance duration for every execution


SQL Query - Actual performance duration for every execution

Author
Message
mrinal.technology
mrinal.technology
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 16
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>Wink
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)
)
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41359 Visits: 20003
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.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
mrinal.technology
mrinal.technology
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 16
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
Attachments
Fast_Execution_Plan.sqlplan (6 views, 383.00 KB)
mrinal.technology
mrinal.technology
SSC Journeyman
SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)SSC Journeyman (78 reputation)

Group: General Forum Members
Points: 78 Visits: 16
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search