![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have a question I created a SP that by using dynamic sql access SP in all databases with certain name and get the output into a temp table and returns it's contents. The stored procedure in other databases is very simple couple of joins and where clause for the parameter passed.. The problem is, there are a lot of activities happening on server, so when the execution plan is flashed, it might take a minute to execute, in the mean time the second execution will take 1 to 5 second. |
#3
| |||
| |||
|
|
Not really sure what you mean when you say "the execution plan is "fl[u]shed". Do you mean the execution plan of the procedures you call? Keep in mind that each procedure has its own plan. It may be the same code, but as far as SQL Server are concerned, they are the same procedure. So if you start with an empty cache, there will be a compilation for each database. However, it does not sound that the execution plan is the issue. For procedure with a single SELECT statement with a few joins, it should not take a minute to build the execution plan. It is more likely than that it depends on whether the data is in cache or not. Since I don't know about the query or the tables, I cannot really say what you should do about - except than to examine the execution plans, and see if you could add indexes to help. Also, you should investigate whether you have the same plan in all databases. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
#4
| |||
| |||
|
|
I ran a profiler when I execute dbo.P_main after an hour of not executing it with EventClass SP:Recompile and in EventSubClass I get 3 which means Object not found at compile time. If I execute this SP all time without a big pause I don't get this event in profiler, that is why I guessed that the execution plan is gone. In the mean time the P_ALL_DB doesn't take long time, if I run it alone without calling it from the P_Main. BTW the main procedure executes this sp from 30 databases. I really cannot understand why such a big difference between the first and all consecutive executions and how I can fix it. |
|
I was thinking to PIN tables but I would have to do it for 30 databases. Is it a good idea? Every table used, might have from 50 to 2000 rows depending on database. |
#5
| |||
| |||
|
|
All and all, there is no reason why P_main should be slow on its own. It reads some fairly small system tables, and runs a cursor over 30 rows. You say that the various P_ALL_DB runs quickly when they run on their own, but there is one difference when you run it from P_Main: the procedure will run in the context of a transaction defined by the INSERT statement. I'm not really sure how that could matter, but then again, I don't know what is in those P_ALL_DB. But there could be blocking issues. One way to test this is to remove the INSERT, and run P_main and see how that affects the execution time. Pinning tables is very rarely if ever a good idea, and this feature has been nullified in SQL 2005. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Hello, |
#6
| |||
| |||
|
|
All and all, there is no reason why P_main should be slow on its own. It reads some fairly small system tables, and runs a cursor over 30 rows. You say that the various P_ALL_DB runs quickly when they run on their own, but there is one difference when you run it from P_Main: the procedure will run in the context of a transaction defined by the INSERT statement. I'm not really sure how that could matter, but then again, I don't know what is in those P_ALL_DB. But there could be blocking issues. One way to test this is to remove the INSERT, and run P_main and see how that affects the execution time. I removed the INSERT into temp table and it does runs faster and actually by a lot. But I need a record set to be returned in one set, how can I achieve without using temp table. |
#7
| |||
| |||
|
|
Inna (med... (AT) hotmail (DOT) com) writes: All and all, there is no reason why P_main should be slow on its own. It reads some fairly small system tables, and runs a cursor over 30 rows. You say that the various P_ALL_DB runs quickly when they run on their own, but there is one difference when you run it from P_Main: the procedure will run in the context of a transaction defined by the INSERT statement. I'm not really sure how that could matter, but then again, I don't know what is in those P_ALL_DB. But there could be blocking issues. One way to test this is to remove the INSERT, and run P_main and see how that affects the execution time. I removed the INSERT into temp table and it does runs faster and actually by a lot. But I need a record set to be returned in one set, how can I achieve without using temp table. How about posting the code of the procedure you call? Not that I'm sure that it helps, but right now I am completely in the dark of what is going on. -- Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books... Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx- Hide quoted text - - Show quoted text - |
#8
| |||
| |||
|
|
The stored procedure in all databases optimised well, all fields are indexed. |
|
So I was thinking since this is development server , we don't rebuild indexes and if we go on production maybe it will be better... |
|
There is another trick I was thinking about, which I am not sure is good and possible to do, maybe I should create a job, which checks if the execution plan is there (this I am not sure I can do) |
![]() |
| Thread Tools | |
| Display Modes | |
| |