Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:07:13+00:00 2026-06-13T08:07:13+00:00

After query optimization I got results that are ok and I wanted to alter

  • 0

After query optimization I got results that are ok and I wanted to alter stored procedure, but got much worst results after SP execution, than it was after query execution!

Firstly, I think at number of reads. What can be reason for so different results?

Query is identical like in SP, only difference is that in query I declared parameter, but in SP that was input parameter. Value that is set to parameter is also same. To avoid ‘recorded data’ first I recompiled SP and after that done DROP and CREATE, but results were also much different.

Query is like this (table and column names are changed because of simplification, and number of columns is reduced):

DECLARE @Var1 varchar(20)    
SET @Var1 = @Var1 + '%'       

DECLARE @Var2 TIMESTAMP    
SELECT @Var2 = CONVERT(TIMESTAMP, ID, 0)    
FROM    
 X_TIMESTAMPS (NOLOCK)    
WHERE    
 TABLE = 'T1'     


declare @Var3 varbinary(8)    
SELECT @Var3 = max(IdTimeStamps)    
FROM    
 T1 (NOLOCK)     

SELECT o.c1  
  , o.c2   
  , o.c3    
  , v.c4    
  , v.c5   
  , p.c6    
  , p.c7    
  , va.c8    
  , isnull(s.c9, '') AS c9    
  , CASE o.c10    
     WHEN 1 THEN    
      0    
     ELSE    
      1    
    END c10   
  , o.c11    
FROM    
 T1 o (NOLOCK)    
 JOIN T2 p (NOLOCK)    
  ON o.c1 = p.c12    
 JOIN T3 i (NOLOCK)    
  ON (o.c13 = i.c14)    
 JOIN T4 v (NOLOCK)    
  ON (v.c4 = i.c15)    
 LEFT JOIN T5 s (NOLOCK)    
  ON (o.c16 = s.c17)    
 JOIN T6 va (NOLOCK)    
  ON o.c11 = va.c18    
WHERE    
 o.c1 LIKE @Var1    
 AND o.c2 > @Var2 

And procedure is like this:

CREATE PROCEDURE [dbo].[SP1] @Var1 varchar(20) ='' 
WITH RECOMPILE  
AS        
BEGIN

    PREVIOUS QUERY WITHOUT DECLARATION FOR @Var1

END

TnX in advance!

Nemanja

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:07:15+00:00Added an answer on June 13, 2026 at 8:07 am

    It’s because different execution plans are used for query with constants and sp with parameeters. You can try a few tricks

    Create inline table function and try it

    create function sf_test
    (
        @param1 int
    )
    returns table
    as
    return
        your query using @in_param1
    

    or

    declare additional parameters in your procedure like this

    create procedure sp_test
    (
        @param1 int
    )
    as
    begin
        declare @in_param1 int
    
        select @in_param1 = @param1
    
        your query using @in_param1
    end
    

    you can also try using option with recompile in your procedure, or use dynamic SQL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webservice that I want to query for updates immediately after a
I receive for example 26 results after a query using php. I want to
Trying to filter the results after a query. Need to find a string inside
I'm trying to avoid involving php after this query. I have a table that
I've got a stored procedure which used to take around 25s to execute. I've
After the query execution the result I need to take is UserNames part. I'm
Is there any reason why I should close the connection after a query or
After running this query statement with my database: select * from articles where content
After a Select Query I have the following table: id CN SN DC 1
How do I view the warning(s) after a SELECT query? The result of one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.