Parameterized query creating many plans

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I have some queries that are parameterized, but they are still creating a new execution plan each time. I am using SQL Server 2016.



Queries are like:



(@P1 varchar(1043),@P2 varchar(6))
UPDATE table
SET FILEDATA=@P1
WHERE FILEID=@P2


This query is not using the already generated execution plan from the cache, rather it is creating a new plan each time.



This image shows the plancount an individual query is creating and the queries







share|improve this question


















  • 2




    Something like this happening: blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…
    – James Z
    Aug 30 at 17:18






  • 1




    James Z's link is where I started understanding this also. Then prompted this question which might help your developers: dba.stackexchange.com/questions/195937/…
    – Peter
    Aug 30 at 18:14










  • Show how the application constructs and executes the query. That's not it - that's just how the plan cache shows it.
    – Aaron Bertrand♦
    Aug 30 at 23:15
















up vote
2
down vote

favorite












I have some queries that are parameterized, but they are still creating a new execution plan each time. I am using SQL Server 2016.



Queries are like:



(@P1 varchar(1043),@P2 varchar(6))
UPDATE table
SET FILEDATA=@P1
WHERE FILEID=@P2


This query is not using the already generated execution plan from the cache, rather it is creating a new plan each time.



This image shows the plancount an individual query is creating and the queries







share|improve this question


















  • 2




    Something like this happening: blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…
    – James Z
    Aug 30 at 17:18






  • 1




    James Z's link is where I started understanding this also. Then prompted this question which might help your developers: dba.stackexchange.com/questions/195937/…
    – Peter
    Aug 30 at 18:14










  • Show how the application constructs and executes the query. That's not it - that's just how the plan cache shows it.
    – Aaron Bertrand♦
    Aug 30 at 23:15












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have some queries that are parameterized, but they are still creating a new execution plan each time. I am using SQL Server 2016.



Queries are like:



(@P1 varchar(1043),@P2 varchar(6))
UPDATE table
SET FILEDATA=@P1
WHERE FILEID=@P2


This query is not using the already generated execution plan from the cache, rather it is creating a new plan each time.



This image shows the plancount an individual query is creating and the queries







share|improve this question














I have some queries that are parameterized, but they are still creating a new execution plan each time. I am using SQL Server 2016.



Queries are like:



(@P1 varchar(1043),@P2 varchar(6))
UPDATE table
SET FILEDATA=@P1
WHERE FILEID=@P2


This query is not using the already generated execution plan from the cache, rather it is creating a new plan each time.



This image shows the plancount an individual query is creating and the queries









share|improve this question













share|improve this question




share|improve this question








edited Aug 30 at 17:28









Paul White♦

46.5k14251398




46.5k14251398










asked Aug 30 at 17:03









Hardik Talwar

111




111







  • 2




    Something like this happening: blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…
    – James Z
    Aug 30 at 17:18






  • 1




    James Z's link is where I started understanding this also. Then prompted this question which might help your developers: dba.stackexchange.com/questions/195937/…
    – Peter
    Aug 30 at 18:14










  • Show how the application constructs and executes the query. That's not it - that's just how the plan cache shows it.
    – Aaron Bertrand♦
    Aug 30 at 23:15












  • 2




    Something like this happening: blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…
    – James Z
    Aug 30 at 17:18






  • 1




    James Z's link is where I started understanding this also. Then prompted this question which might help your developers: dba.stackexchange.com/questions/195937/…
    – Peter
    Aug 30 at 18:14










  • Show how the application constructs and executes the query. That's not it - that's just how the plan cache shows it.
    – Aaron Bertrand♦
    Aug 30 at 23:15







2




2




Something like this happening: blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…
– James Z
Aug 30 at 17:18




Something like this happening: blogs.msmvps.com/jcoehoorn/blog/2014/05/12/…
– James Z
Aug 30 at 17:18




1




1




James Z's link is where I started understanding this also. Then prompted this question which might help your developers: dba.stackexchange.com/questions/195937/…
– Peter
Aug 30 at 18:14




James Z's link is where I started understanding this also. Then prompted this question which might help your developers: dba.stackexchange.com/questions/195937/…
– Peter
Aug 30 at 18:14












Show how the application constructs and executes the query. That's not it - that's just how the plan cache shows it.
– Aaron Bertrand♦
Aug 30 at 23:15




Show how the application constructs and executes the query. That's not it - that's just how the plan cache shows it.
– Aaron Bertrand♦
Aug 30 at 23:15










1 Answer
1






active

oldest

votes

















up vote
8
down vote













The length of @P1 is different in all of those.



You're getting different plans because you're not explicitly setting parameter lengths in your code.



Any difference in strings, no matter how minor, will generate a new plan. Data type sizes, spaces, comments, all of them will cause new entries in the plan cache. Here's a demo video illustrating the problem.






share|improve this answer






















  • @HardikTalwar link updated, but the video doesn't show you how to set lengths in your app code, it just shows you how different things affect plan caching, etc.
    – sp_BlitzErik
    Aug 30 at 17:24










  • I followed the video and did the same show in that video thereby running that stored procedure I am getting some queries which are exactly same there is no difference of gaps or anything they are same but still executing different times
    – Hardik Talwar
    Aug 30 at 17:54










  • @HardikTalwar I'm not sure if that's a different question than your original one or not. If it's a different question, feel free to ask it on the main site. If it's another point about your original question, please update it with the relevant information.
    – sp_BlitzErik
    Aug 30 at 17:56










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "182"
;
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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f216330%2fparameterized-query-creating-many-plans%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
8
down vote













The length of @P1 is different in all of those.



You're getting different plans because you're not explicitly setting parameter lengths in your code.



Any difference in strings, no matter how minor, will generate a new plan. Data type sizes, spaces, comments, all of them will cause new entries in the plan cache. Here's a demo video illustrating the problem.






share|improve this answer






















  • @HardikTalwar link updated, but the video doesn't show you how to set lengths in your app code, it just shows you how different things affect plan caching, etc.
    – sp_BlitzErik
    Aug 30 at 17:24










  • I followed the video and did the same show in that video thereby running that stored procedure I am getting some queries which are exactly same there is no difference of gaps or anything they are same but still executing different times
    – Hardik Talwar
    Aug 30 at 17:54










  • @HardikTalwar I'm not sure if that's a different question than your original one or not. If it's a different question, feel free to ask it on the main site. If it's another point about your original question, please update it with the relevant information.
    – sp_BlitzErik
    Aug 30 at 17:56














up vote
8
down vote













The length of @P1 is different in all of those.



You're getting different plans because you're not explicitly setting parameter lengths in your code.



Any difference in strings, no matter how minor, will generate a new plan. Data type sizes, spaces, comments, all of them will cause new entries in the plan cache. Here's a demo video illustrating the problem.






share|improve this answer






















  • @HardikTalwar link updated, but the video doesn't show you how to set lengths in your app code, it just shows you how different things affect plan caching, etc.
    – sp_BlitzErik
    Aug 30 at 17:24










  • I followed the video and did the same show in that video thereby running that stored procedure I am getting some queries which are exactly same there is no difference of gaps or anything they are same but still executing different times
    – Hardik Talwar
    Aug 30 at 17:54










  • @HardikTalwar I'm not sure if that's a different question than your original one or not. If it's a different question, feel free to ask it on the main site. If it's another point about your original question, please update it with the relevant information.
    – sp_BlitzErik
    Aug 30 at 17:56












up vote
8
down vote










up vote
8
down vote









The length of @P1 is different in all of those.



You're getting different plans because you're not explicitly setting parameter lengths in your code.



Any difference in strings, no matter how minor, will generate a new plan. Data type sizes, spaces, comments, all of them will cause new entries in the plan cache. Here's a demo video illustrating the problem.






share|improve this answer














The length of @P1 is different in all of those.



You're getting different plans because you're not explicitly setting parameter lengths in your code.



Any difference in strings, no matter how minor, will generate a new plan. Data type sizes, spaces, comments, all of them will cause new entries in the plan cache. Here's a demo video illustrating the problem.







share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 30 at 17:23

























answered Aug 30 at 17:06









sp_BlitzErik

19.4k1161101




19.4k1161101











  • @HardikTalwar link updated, but the video doesn't show you how to set lengths in your app code, it just shows you how different things affect plan caching, etc.
    – sp_BlitzErik
    Aug 30 at 17:24










  • I followed the video and did the same show in that video thereby running that stored procedure I am getting some queries which are exactly same there is no difference of gaps or anything they are same but still executing different times
    – Hardik Talwar
    Aug 30 at 17:54










  • @HardikTalwar I'm not sure if that's a different question than your original one or not. If it's a different question, feel free to ask it on the main site. If it's another point about your original question, please update it with the relevant information.
    – sp_BlitzErik
    Aug 30 at 17:56
















  • @HardikTalwar link updated, but the video doesn't show you how to set lengths in your app code, it just shows you how different things affect plan caching, etc.
    – sp_BlitzErik
    Aug 30 at 17:24










  • I followed the video and did the same show in that video thereby running that stored procedure I am getting some queries which are exactly same there is no difference of gaps or anything they are same but still executing different times
    – Hardik Talwar
    Aug 30 at 17:54










  • @HardikTalwar I'm not sure if that's a different question than your original one or not. If it's a different question, feel free to ask it on the main site. If it's another point about your original question, please update it with the relevant information.
    – sp_BlitzErik
    Aug 30 at 17:56















@HardikTalwar link updated, but the video doesn't show you how to set lengths in your app code, it just shows you how different things affect plan caching, etc.
– sp_BlitzErik
Aug 30 at 17:24




@HardikTalwar link updated, but the video doesn't show you how to set lengths in your app code, it just shows you how different things affect plan caching, etc.
– sp_BlitzErik
Aug 30 at 17:24












I followed the video and did the same show in that video thereby running that stored procedure I am getting some queries which are exactly same there is no difference of gaps or anything they are same but still executing different times
– Hardik Talwar
Aug 30 at 17:54




I followed the video and did the same show in that video thereby running that stored procedure I am getting some queries which are exactly same there is no difference of gaps or anything they are same but still executing different times
– Hardik Talwar
Aug 30 at 17:54












@HardikTalwar I'm not sure if that's a different question than your original one or not. If it's a different question, feel free to ask it on the main site. If it's another point about your original question, please update it with the relevant information.
– sp_BlitzErik
Aug 30 at 17:56




@HardikTalwar I'm not sure if that's a different question than your original one or not. If it's a different question, feel free to ask it on the main site. If it's another point about your original question, please update it with the relevant information.
– sp_BlitzErik
Aug 30 at 17:56

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f216330%2fparameterized-query-creating-many-plans%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery