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

  • SEARCH
  • Home
  • 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 7087609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:41:43+00:00 2026-05-28T07:41:43+00:00

I am running a store procedure which is taking a lot of time ,

  • 0

I am running a store procedure which is taking a lot of time , due to which i have to increase the timeout to nearly 3 minutes, can any one provide assistance on how i can decrease the time for the store procedure to execute , it works fine on 100-500 codes but when the code exceeds to somehow 20000 , it slows up and takes upto 3-4 minutes , below is the sp, can anyone help me optimizing it.

@GUID uniqueidentifier
-- Insert statements for procedure here
SELECT     
        COUNT(Code.allocatedVendorID) AS Amt, 
        MIN(Code.dateAllocated) AS dateAllocated, 
        derivedtbl_1.startID, derivedtbl_2.endID, 
        tbVendor.name, 
        Code.unitCost, Code.isFree, Code.isAcademic, 
        Code.isVoided, Code.GUID, Code.expiryDate
FROM         
       Code 
        INNER JOIN
              (SELECT     MIN(CodeID) AS startID, GUID
                FROM          Code AS tbAccessCode_1
                GROUP BY GUID) AS derivedtbl_1 
                ON Code.GUID = derivedtbl_1.GUID 
        INNER JOIN
              (SELECT     MAX(accessCodeID) AS endID, GUID
                FROM          Code AS tbAccessCode_1
                GROUP BY GUID) AS derivedtbl_2 
                ON Code.GUID = derivedtbl_2.GUID 
        INNER JOIN  tbVendor 
            ON Code.allocatedVendorID = tbVendor.vendorID
WHERE     (Code.GUID = @GUID) AND (Code.allocatedVendorID > 0)
GROUP BY
        derivedtbl_1.startID, derivedtbl_2.endID, 
        tbVendor.name, 
        Code.unitCost, Code.isFree, Code.isAcademic, 
        Code.isVoided, Code.GUID, Code.expiryDate

ORDER BY dateAllocated DESC
  • 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-05-28T07:41:43+00:00Added an answer on May 28, 2026 at 7:41 am

    You can use this to eliminate too many scans of Code table:

       SELECT     COUNT(Code.allocatedVendorID) AS Amt, 
                  MIN(Code.dateAllocated) AS dateAllocated,      
                  Code.startID, 
                  Code.endID, 
                  tbVendor.name, 
                  Code.unitCost, Code.isFree, Code.isAcademic, 
                  Code.isVoided, Code.GUID, Code.expiryDate
        FROM         
          (select Code.*,
                min(accessCodeID) over (partition by Code.GUID) startID,
                max(accessCodeID) over (partition by Code.GUID) endID
          from Code
          where (Code.GUID = @GUID) AND (Code.allocatedVendorID > 0)
          ) Code
        INNER JOIN
              tbVendor ON Code.allocatedVendorID = tbVendor.vendorID  
        GROUP BY  Code.startID, 
                  Code.endID, 
                  tbVendor.name, 
                  Code.unitCost, Code.isFree, Code.isAcademic, 
                  Code.isVoided, Code.GUID, Code.expiryDate
        ORDER BY dateAllocated DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I know which stored procedure is running in SQL Server 2005. And
I have a Stored procedure which schedules a job. This Job takes a lot
I have a stored procedure which is erroring with Timeout expired. The code involved
I have a stored procedure running in MS SQL 2008 R2 which runs fine
I have a SQL stored procedure in which one statement is taking 95% of
I have a somewhat-long-running stored procedure being called from a PB application. I want
What's the best way to know which stored procedure are currently running in a
I have a stored procedure which is building a dynamic sql query and then
I have a stored procedure which returns XML to the caller using a SELECT
I have a stored procedure which executes some dynamic sql, shown below. I've tried

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.