Random slow query performance in SQL Server 2016












-2















I will see you what is the problem...



SELECT 
SUM(A.POSTEDQTY),
SUM(A.POSTEDVALUE),
SUM(A.PHYSICALVALUE),
SUM(A.DEDUCTED),
SUM(A.RECEIVED),
SUM(A.RESERVPHYSICAL),
SUM(A.RESERVORDERED),
SUM(A.REGISTERED),
SUM(A.PICKED),
SUM(A.ONORDER),
SUM(A.ORDERED),
SUM(A.ARRIVED),
SUM(A.QUOTATIONRECEIPT),
SUM(A.QUOTATIONISSUE),
SUM(A.AVAILPHYSICAL),
SUM(A.AVAILORDERED),
SUM(A.PHYSICALINVENT)
FROM
INVENTSUM A WITH(NOLOCK)
WHERE
((A.DATAAREAID = @P1)
AND ((A.ITEMID = @P2)
AND (A.CLOSED = @P3)))
AND EXISTS (SELECT 'x' FROM INVENTDIM B WITH (NOLOCK)
WHERE ((B.DATAAREAID = @P4)
AND ((((B.INVENTDIMID = A.INVENTDIMID) AND (B.CONFIGID = @P5))
AND (B.INVENTLOCATIONID = @P6)) AND (B.INVENTBATCHID = @P7))))


Query slow performance



declare @p2 int
set @p2=180157421
declare @p3 int
set @p3=16
declare @p4 int
set @p4=1
declare @p5 int
set @p5=1
exec sp_cursorexecute 1073741895,@p2 output,@p3 output,@p4 output,@p5 output,N'ens',N'V89710821',0,N'ens',N'PO16628',N'AS',N'OFS028748'
select @p2, @p3, @p4, @p5


Query parameters slow performance



SELECT SUM(A.POSTEDQTY),SUM(A.POSTEDVALUE),SUM(A.PHYSICALVALUE),SUM(A.DEDUCTED),SUM(A.RECEIVED),SUM(A.RESERVPHYSICAL),SUM(A.RESERVORDERED),SUM(A.REGISTERED),SUM(A.PICKED),SUM(A.ONORDER),SUM(A.ORDERED),SUM(A.ARRIVED),SUM(A.QUOTATIONRECEIPT),SUM(A.QUOTATIONISSUE),SUM(A.AVAILPHYSICAL),SUM(A.AVAILORDERED),SUM(A.PHYSICALINVENT) FROM INVENTSUMDELTA A WHERE ((A.DATAAREAID=@P1) AND (((A.ITEMID=@P2) AND (A.ISAGGREGATED=@P3)) AND (A.TTSID=@P4))) AND EXISTS (SELECT 'x' FROM INVENTDIM B WHERE ((B.DATAAREAID=@P5) AND ((((B.INVENTDIMID=A.INVENTDIMID) AND (B.CONFIGID=@P6)) AND (B.INVENTLOCATIONID=@P7)) AND (B.INVENTBATCHID=@P8))))


Same query good performance



declare @p2 int
set @p2=180157423
declare @p3 int
set @p3=16
declare @p4 int
set @p4=1
declare @p5 int
set @p5=1
exec sp_cursorexecute 1073741904,@p2 output,@p3 output,@p4 output,@p5 output,N'ens',N'V89710821',0,5666550353,N'ens',N'PO16628',N'AS',N'OFS028748'
select @p2, @p3, @p4, @p5


Parameters same query good performance



In SQLServer Profiler you can see that the same query executes in 0ms or over 10000ms



And if I try to execute the same query (10000ms) with the same parameters directly it takes 0ms



What is happening?



Thanks!! :)










share|improve this question




















  • 1





    Don't make us read your profiler output. Get the code that is being executed and put it in your post instead of using 4 images and links.

    – dfundako
    Nov 19 '18 at 15:50











  • Thanks! Edited! :)

    – TBP
    Nov 19 '18 at 15:55











  • you need to get the execution plans

    – Martin Smith
    Nov 19 '18 at 22:00











  • It's also possible you're suffering from parameter sniffing.

    – Eric Brandt
    Nov 20 '18 at 20:47
















-2















I will see you what is the problem...



SELECT 
SUM(A.POSTEDQTY),
SUM(A.POSTEDVALUE),
SUM(A.PHYSICALVALUE),
SUM(A.DEDUCTED),
SUM(A.RECEIVED),
SUM(A.RESERVPHYSICAL),
SUM(A.RESERVORDERED),
SUM(A.REGISTERED),
SUM(A.PICKED),
SUM(A.ONORDER),
SUM(A.ORDERED),
SUM(A.ARRIVED),
SUM(A.QUOTATIONRECEIPT),
SUM(A.QUOTATIONISSUE),
SUM(A.AVAILPHYSICAL),
SUM(A.AVAILORDERED),
SUM(A.PHYSICALINVENT)
FROM
INVENTSUM A WITH(NOLOCK)
WHERE
((A.DATAAREAID = @P1)
AND ((A.ITEMID = @P2)
AND (A.CLOSED = @P3)))
AND EXISTS (SELECT 'x' FROM INVENTDIM B WITH (NOLOCK)
WHERE ((B.DATAAREAID = @P4)
AND ((((B.INVENTDIMID = A.INVENTDIMID) AND (B.CONFIGID = @P5))
AND (B.INVENTLOCATIONID = @P6)) AND (B.INVENTBATCHID = @P7))))


Query slow performance



declare @p2 int
set @p2=180157421
declare @p3 int
set @p3=16
declare @p4 int
set @p4=1
declare @p5 int
set @p5=1
exec sp_cursorexecute 1073741895,@p2 output,@p3 output,@p4 output,@p5 output,N'ens',N'V89710821',0,N'ens',N'PO16628',N'AS',N'OFS028748'
select @p2, @p3, @p4, @p5


Query parameters slow performance



SELECT SUM(A.POSTEDQTY),SUM(A.POSTEDVALUE),SUM(A.PHYSICALVALUE),SUM(A.DEDUCTED),SUM(A.RECEIVED),SUM(A.RESERVPHYSICAL),SUM(A.RESERVORDERED),SUM(A.REGISTERED),SUM(A.PICKED),SUM(A.ONORDER),SUM(A.ORDERED),SUM(A.ARRIVED),SUM(A.QUOTATIONRECEIPT),SUM(A.QUOTATIONISSUE),SUM(A.AVAILPHYSICAL),SUM(A.AVAILORDERED),SUM(A.PHYSICALINVENT) FROM INVENTSUMDELTA A WHERE ((A.DATAAREAID=@P1) AND (((A.ITEMID=@P2) AND (A.ISAGGREGATED=@P3)) AND (A.TTSID=@P4))) AND EXISTS (SELECT 'x' FROM INVENTDIM B WHERE ((B.DATAAREAID=@P5) AND ((((B.INVENTDIMID=A.INVENTDIMID) AND (B.CONFIGID=@P6)) AND (B.INVENTLOCATIONID=@P7)) AND (B.INVENTBATCHID=@P8))))


Same query good performance



declare @p2 int
set @p2=180157423
declare @p3 int
set @p3=16
declare @p4 int
set @p4=1
declare @p5 int
set @p5=1
exec sp_cursorexecute 1073741904,@p2 output,@p3 output,@p4 output,@p5 output,N'ens',N'V89710821',0,5666550353,N'ens',N'PO16628',N'AS',N'OFS028748'
select @p2, @p3, @p4, @p5


Parameters same query good performance



In SQLServer Profiler you can see that the same query executes in 0ms or over 10000ms



And if I try to execute the same query (10000ms) with the same parameters directly it takes 0ms



What is happening?



Thanks!! :)










share|improve this question




















  • 1





    Don't make us read your profiler output. Get the code that is being executed and put it in your post instead of using 4 images and links.

    – dfundako
    Nov 19 '18 at 15:50











  • Thanks! Edited! :)

    – TBP
    Nov 19 '18 at 15:55











  • you need to get the execution plans

    – Martin Smith
    Nov 19 '18 at 22:00











  • It's also possible you're suffering from parameter sniffing.

    – Eric Brandt
    Nov 20 '18 at 20:47














-2












-2








-2








I will see you what is the problem...



SELECT 
SUM(A.POSTEDQTY),
SUM(A.POSTEDVALUE),
SUM(A.PHYSICALVALUE),
SUM(A.DEDUCTED),
SUM(A.RECEIVED),
SUM(A.RESERVPHYSICAL),
SUM(A.RESERVORDERED),
SUM(A.REGISTERED),
SUM(A.PICKED),
SUM(A.ONORDER),
SUM(A.ORDERED),
SUM(A.ARRIVED),
SUM(A.QUOTATIONRECEIPT),
SUM(A.QUOTATIONISSUE),
SUM(A.AVAILPHYSICAL),
SUM(A.AVAILORDERED),
SUM(A.PHYSICALINVENT)
FROM
INVENTSUM A WITH(NOLOCK)
WHERE
((A.DATAAREAID = @P1)
AND ((A.ITEMID = @P2)
AND (A.CLOSED = @P3)))
AND EXISTS (SELECT 'x' FROM INVENTDIM B WITH (NOLOCK)
WHERE ((B.DATAAREAID = @P4)
AND ((((B.INVENTDIMID = A.INVENTDIMID) AND (B.CONFIGID = @P5))
AND (B.INVENTLOCATIONID = @P6)) AND (B.INVENTBATCHID = @P7))))


Query slow performance



declare @p2 int
set @p2=180157421
declare @p3 int
set @p3=16
declare @p4 int
set @p4=1
declare @p5 int
set @p5=1
exec sp_cursorexecute 1073741895,@p2 output,@p3 output,@p4 output,@p5 output,N'ens',N'V89710821',0,N'ens',N'PO16628',N'AS',N'OFS028748'
select @p2, @p3, @p4, @p5


Query parameters slow performance



SELECT SUM(A.POSTEDQTY),SUM(A.POSTEDVALUE),SUM(A.PHYSICALVALUE),SUM(A.DEDUCTED),SUM(A.RECEIVED),SUM(A.RESERVPHYSICAL),SUM(A.RESERVORDERED),SUM(A.REGISTERED),SUM(A.PICKED),SUM(A.ONORDER),SUM(A.ORDERED),SUM(A.ARRIVED),SUM(A.QUOTATIONRECEIPT),SUM(A.QUOTATIONISSUE),SUM(A.AVAILPHYSICAL),SUM(A.AVAILORDERED),SUM(A.PHYSICALINVENT) FROM INVENTSUMDELTA A WHERE ((A.DATAAREAID=@P1) AND (((A.ITEMID=@P2) AND (A.ISAGGREGATED=@P3)) AND (A.TTSID=@P4))) AND EXISTS (SELECT 'x' FROM INVENTDIM B WHERE ((B.DATAAREAID=@P5) AND ((((B.INVENTDIMID=A.INVENTDIMID) AND (B.CONFIGID=@P6)) AND (B.INVENTLOCATIONID=@P7)) AND (B.INVENTBATCHID=@P8))))


Same query good performance



declare @p2 int
set @p2=180157423
declare @p3 int
set @p3=16
declare @p4 int
set @p4=1
declare @p5 int
set @p5=1
exec sp_cursorexecute 1073741904,@p2 output,@p3 output,@p4 output,@p5 output,N'ens',N'V89710821',0,5666550353,N'ens',N'PO16628',N'AS',N'OFS028748'
select @p2, @p3, @p4, @p5


Parameters same query good performance



In SQLServer Profiler you can see that the same query executes in 0ms or over 10000ms



And if I try to execute the same query (10000ms) with the same parameters directly it takes 0ms



What is happening?



Thanks!! :)










share|improve this question
















I will see you what is the problem...



SELECT 
SUM(A.POSTEDQTY),
SUM(A.POSTEDVALUE),
SUM(A.PHYSICALVALUE),
SUM(A.DEDUCTED),
SUM(A.RECEIVED),
SUM(A.RESERVPHYSICAL),
SUM(A.RESERVORDERED),
SUM(A.REGISTERED),
SUM(A.PICKED),
SUM(A.ONORDER),
SUM(A.ORDERED),
SUM(A.ARRIVED),
SUM(A.QUOTATIONRECEIPT),
SUM(A.QUOTATIONISSUE),
SUM(A.AVAILPHYSICAL),
SUM(A.AVAILORDERED),
SUM(A.PHYSICALINVENT)
FROM
INVENTSUM A WITH(NOLOCK)
WHERE
((A.DATAAREAID = @P1)
AND ((A.ITEMID = @P2)
AND (A.CLOSED = @P3)))
AND EXISTS (SELECT 'x' FROM INVENTDIM B WITH (NOLOCK)
WHERE ((B.DATAAREAID = @P4)
AND ((((B.INVENTDIMID = A.INVENTDIMID) AND (B.CONFIGID = @P5))
AND (B.INVENTLOCATIONID = @P6)) AND (B.INVENTBATCHID = @P7))))


Query slow performance



declare @p2 int
set @p2=180157421
declare @p3 int
set @p3=16
declare @p4 int
set @p4=1
declare @p5 int
set @p5=1
exec sp_cursorexecute 1073741895,@p2 output,@p3 output,@p4 output,@p5 output,N'ens',N'V89710821',0,N'ens',N'PO16628',N'AS',N'OFS028748'
select @p2, @p3, @p4, @p5


Query parameters slow performance



SELECT SUM(A.POSTEDQTY),SUM(A.POSTEDVALUE),SUM(A.PHYSICALVALUE),SUM(A.DEDUCTED),SUM(A.RECEIVED),SUM(A.RESERVPHYSICAL),SUM(A.RESERVORDERED),SUM(A.REGISTERED),SUM(A.PICKED),SUM(A.ONORDER),SUM(A.ORDERED),SUM(A.ARRIVED),SUM(A.QUOTATIONRECEIPT),SUM(A.QUOTATIONISSUE),SUM(A.AVAILPHYSICAL),SUM(A.AVAILORDERED),SUM(A.PHYSICALINVENT) FROM INVENTSUMDELTA A WHERE ((A.DATAAREAID=@P1) AND (((A.ITEMID=@P2) AND (A.ISAGGREGATED=@P3)) AND (A.TTSID=@P4))) AND EXISTS (SELECT 'x' FROM INVENTDIM B WHERE ((B.DATAAREAID=@P5) AND ((((B.INVENTDIMID=A.INVENTDIMID) AND (B.CONFIGID=@P6)) AND (B.INVENTLOCATIONID=@P7)) AND (B.INVENTBATCHID=@P8))))


Same query good performance



declare @p2 int
set @p2=180157423
declare @p3 int
set @p3=16
declare @p4 int
set @p4=1
declare @p5 int
set @p5=1
exec sp_cursorexecute 1073741904,@p2 output,@p3 output,@p4 output,@p5 output,N'ens',N'V89710821',0,5666550353,N'ens',N'PO16628',N'AS',N'OFS028748'
select @p2, @p3, @p4, @p5


Parameters same query good performance



In SQLServer Profiler you can see that the same query executes in 0ms or over 10000ms



And if I try to execute the same query (10000ms) with the same parameters directly it takes 0ms



What is happening?



Thanks!! :)







sql performance sql-server-2016 sql-server-profiler






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 21:45









marc_s

576k12811111258




576k12811111258










asked Nov 19 '18 at 15:48









TBPTBP

23




23








  • 1





    Don't make us read your profiler output. Get the code that is being executed and put it in your post instead of using 4 images and links.

    – dfundako
    Nov 19 '18 at 15:50











  • Thanks! Edited! :)

    – TBP
    Nov 19 '18 at 15:55











  • you need to get the execution plans

    – Martin Smith
    Nov 19 '18 at 22:00











  • It's also possible you're suffering from parameter sniffing.

    – Eric Brandt
    Nov 20 '18 at 20:47














  • 1





    Don't make us read your profiler output. Get the code that is being executed and put it in your post instead of using 4 images and links.

    – dfundako
    Nov 19 '18 at 15:50











  • Thanks! Edited! :)

    – TBP
    Nov 19 '18 at 15:55











  • you need to get the execution plans

    – Martin Smith
    Nov 19 '18 at 22:00











  • It's also possible you're suffering from parameter sniffing.

    – Eric Brandt
    Nov 20 '18 at 20:47








1




1





Don't make us read your profiler output. Get the code that is being executed and put it in your post instead of using 4 images and links.

– dfundako
Nov 19 '18 at 15:50





Don't make us read your profiler output. Get the code that is being executed and put it in your post instead of using 4 images and links.

– dfundako
Nov 19 '18 at 15:50













Thanks! Edited! :)

– TBP
Nov 19 '18 at 15:55





Thanks! Edited! :)

– TBP
Nov 19 '18 at 15:55













you need to get the execution plans

– Martin Smith
Nov 19 '18 at 22:00





you need to get the execution plans

– Martin Smith
Nov 19 '18 at 22:00













It's also possible you're suffering from parameter sniffing.

– Eric Brandt
Nov 20 '18 at 20:47





It's also possible you're suffering from parameter sniffing.

– Eric Brandt
Nov 20 '18 at 20:47












1 Answer
1






active

oldest

votes


















0














The first time you execute the query, the db engine have to prepare the statement send to db search the data and return the result. The second time the statement is already prepared and the data is on the cache. That is why the second run is always faster



https://littlekendra.com/2016/11/25/why-is-my-query-faster-the-second-time-it-runs-dear-sql-dba-episode-23/






share|improve this answer
























  • But the same query is executed a lot of times and first is executing ok, then poor performance and after good, it's a random problem I can't understand.

    – TBP
    Nov 19 '18 at 16:03











  • the other reason is usually a slow disk.

    – Juan Carlos Oropeza
    Nov 19 '18 at 18:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378215%2frandom-slow-query-performance-in-sql-server-2016%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The first time you execute the query, the db engine have to prepare the statement send to db search the data and return the result. The second time the statement is already prepared and the data is on the cache. That is why the second run is always faster



https://littlekendra.com/2016/11/25/why-is-my-query-faster-the-second-time-it-runs-dear-sql-dba-episode-23/






share|improve this answer
























  • But the same query is executed a lot of times and first is executing ok, then poor performance and after good, it's a random problem I can't understand.

    – TBP
    Nov 19 '18 at 16:03











  • the other reason is usually a slow disk.

    – Juan Carlos Oropeza
    Nov 19 '18 at 18:18
















0














The first time you execute the query, the db engine have to prepare the statement send to db search the data and return the result. The second time the statement is already prepared and the data is on the cache. That is why the second run is always faster



https://littlekendra.com/2016/11/25/why-is-my-query-faster-the-second-time-it-runs-dear-sql-dba-episode-23/






share|improve this answer
























  • But the same query is executed a lot of times and first is executing ok, then poor performance and after good, it's a random problem I can't understand.

    – TBP
    Nov 19 '18 at 16:03











  • the other reason is usually a slow disk.

    – Juan Carlos Oropeza
    Nov 19 '18 at 18:18














0












0








0







The first time you execute the query, the db engine have to prepare the statement send to db search the data and return the result. The second time the statement is already prepared and the data is on the cache. That is why the second run is always faster



https://littlekendra.com/2016/11/25/why-is-my-query-faster-the-second-time-it-runs-dear-sql-dba-episode-23/






share|improve this answer













The first time you execute the query, the db engine have to prepare the statement send to db search the data and return the result. The second time the statement is already prepared and the data is on the cache. That is why the second run is always faster



https://littlekendra.com/2016/11/25/why-is-my-query-faster-the-second-time-it-runs-dear-sql-dba-episode-23/







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 15:53









Juan Carlos OropezaJuan Carlos Oropeza

36.4k63876




36.4k63876













  • But the same query is executed a lot of times and first is executing ok, then poor performance and after good, it's a random problem I can't understand.

    – TBP
    Nov 19 '18 at 16:03











  • the other reason is usually a slow disk.

    – Juan Carlos Oropeza
    Nov 19 '18 at 18:18



















  • But the same query is executed a lot of times and first is executing ok, then poor performance and after good, it's a random problem I can't understand.

    – TBP
    Nov 19 '18 at 16:03











  • the other reason is usually a slow disk.

    – Juan Carlos Oropeza
    Nov 19 '18 at 18:18

















But the same query is executed a lot of times and first is executing ok, then poor performance and after good, it's a random problem I can't understand.

– TBP
Nov 19 '18 at 16:03





But the same query is executed a lot of times and first is executing ok, then poor performance and after good, it's a random problem I can't understand.

– TBP
Nov 19 '18 at 16:03













the other reason is usually a slow disk.

– Juan Carlos Oropeza
Nov 19 '18 at 18:18





the other reason is usually a slow disk.

– Juan Carlos Oropeza
Nov 19 '18 at 18:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378215%2frandom-slow-query-performance-in-sql-server-2016%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)