title: The same SQL runs slower or faster with the same parameters
body: <p>I have a query that runs extremely fast (1 sec) see below:</p>
  
  <pre><code>SELECT *
  FROM    ( select ROW_NUMBER() OVER ( ORDER BY  [Rank] DESC ) AS RowNum, *
            FROM [product].[FnSearchKeyword]('basic', null, null, null, null, null, null,           null)
      ) AS RowConstrainedResult
  WHERE   RowNum     = 1 AND RowNum &lt; 30
  </code></pre>
  
  <p>Bad if I put this query <strong>inside a procedure</strong> it takes 15 seconds to run as follows:</p>
  
  <pre><code> CREATE  anydata 
      @keywords nvarchar(4000),
      @minimunRate int, 
      @priceFrom decimal, 
      @priceTo decimal, 
      @relaeseStart datetime,
      @releaseEnd datetime,
      @categoryList nvarchar(4000), 
      @storeList nvarchar(4000),
      @rowBegin int,
      @rowEnd int,
      @orderBy int,
      @isAdult bit = null
   AS 
   SELECT *
   FROM    ( select ROW_NUMBER() OVER ( ORDER BY  [Rank] DESC ) AS RowNum, *
             FROM [product].[FnSearchKeyword]('basic', @minimunRate , @priceFrom , @priceTo      , null, null, null, null)
      ) AS RowConstrainedResult
   WHERE   RowNum     = 1 AND RowNum &lt; 30
  </code></pre>
  
  <p>When I play the SQL, the return delay 15 sec:</p>
  
  <pre><code> exec anydata  null, null, null, null, null, null, null, null, null, null, null
  </code></pre>
  
  <p>Note:</p>
  
  <ul>
  <li><p>Parameters of Function FnSearchKeyword have the same declared type in AnyData Procedure.</p></li>
  <li><p>I'm passing all values "NULL" in the exec procedure, just to have the same result as shown above query that runs fast. No changes in the scope of the parameters</p></li>
  </ul>
  
  <p>Honestly, I noticed that the "Execution Plan" is changed when I run the query through the Procedure, but do not know how to solve this, since the parameters of the query are the same.</p>
  
  <p>Unfortunately I need to be put inside a PROC query for other reasons not mentioned here not to further complicate the issue.</p>
  
  <p>About Duplicated Message: Its question isn´t duplicated because it its slow running in anyone system (Query Analyzer, C# asp.net, others).</p>