Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Wondering why this happens??

Author  Topic 

mrtweaver
Yak Posting Veteran

67 Posts

Posted - 2010-02-25 : 13:45:53
I have a stored proceedure, inside the stored proceedure is 4 queries, the first query feeds data to the remaining 3.

The problem: If I leave the query as is it takes on average 3 to 5 minutes to run, sometimes longer.

My temp solution: What I did was take the first query and use it as a view. So now it is not only the first query in the stored proceedure but it is also a view. Took the second query and pointed it to the view to get the data it requires. The last two queries were still dependant on the first.

Now when I run the proceedure it takes 4 seconds to get the data. This to me does not make sence because what I have read about stored proceedures is that they are sequential in nature. You know do step one, when finished do step 2, and so on.

So could someone tell me what part I am missing as to why I had to make the changes I did. Thanks and have a great day.

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-02-25 : 14:09:50
Would have to see the code, but probably has to do with table scanning vs index seeks.

The sp can't generate a proper execution plan since it doesn't know what the previous queries will return
Go to Top of Page
   

- Advertisement -