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 1023495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:37:51+00:00 2026-05-16T11:37:51+00:00

I have a stored procedure in a MS-SQL 2005 database that: Creates two temp

  • 0

I have a stored procedure in a MS-SQL 2005 database that:

  • Creates two temp tables
  • Executes a query with 7 joins but is not otherwise terribly complex
  • Inserts the results into one of the temp tables
  • Executes two more queries (no joins to “real” tables) that puts records from one of the temp tables into the other.
  • Returns a result set from the second temp table
  • Drops both temp tables

The SP takes two parameters, which are then used in the first query.

When I run the SP for a given set of parameters, it takes 3 minutes to execute.

When I execute the contents of the SP as a regular T-SQL batch (declaring and setting the parameters beforehand), it takes 10 seconds. These numbers are consistent across multiple sequential runs.

This is a huge difference and there’s no obvious functional changes. What could be causing this?

UPDATE

Reindexing my tables (DBCC REINDEX) sped up the SP version dramatically. The SP version now takes 1 second, while the raw SQL takes 6.

That’s great as a solution to the immediate problem, but I’d still like to know the “why”.

  • 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-16T11:37:51+00:00Added an answer on May 16, 2026 at 11:37 am

    It might have been exactly due to the fact that in SP the execution plan was cached and it was not optimal for the data set. When data set depends greatly on the parameters or changes considerably between invocations it’s better to specify ‘with recompile’ in ‘create proc’. You lose a fraction of a second on recompilation, but may win minutes on execution.

    PS Why cannot I comment? Only “Your Answer” is available.

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

Sidebar

Ask A Question

Stats

  • Questions 533k
  • Answers 533k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT page, COUNT(*) FROM hits WHERE agent LIKE '%Googlebot%'… May 17, 2026 at 12:25 am
  • Editorial Team
    Editorial Team added an answer I have fixed it, using GC.Collect; still can't believe it… May 17, 2026 at 12:25 am
  • Editorial Team
    Editorial Team added an answer Here's what I'd do: First, the div #navigation that wraps… May 17, 2026 at 12:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Working in SQL Server 2005, I have a stored procedure that inserts a record
I have a table in a SQL Server 2005 Database that is used a
I have a 'reference' SQL Server 2005 database that is used as our global
I have a CLR stored procedure that references an assembly created in VS 2008
I have an assortment of database objects (tables, functions, views, stored procedures) each scripted
I have a user defined function in a SQL Server 2005 database which returns
We have mirroring set up between 2 SQL Server 2005 standard editions. There are
I'm using Visual Studio 2005 to script out a database so that I can
I have a need to take the parameters passed in to a Stored Procedure
I'm trying to get a stored procedure to work for a co-worker who is

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.