Search code examples
sqlsql-serverstored-proceduressql-execution-plan

Stored Procedure Execution Plan - Data Manipulation


I have a stored proc that processes a large amount of data (about 5m rows in this example). The performance varies wildly. I've had the process running in as little as 15 minutes and seen it run for as long as 4 hours.

For maintenance, and in order to verify that the logic and processing is correct, we have the SP broken up into sections:

  1. TRUNCATE and populate a work table (indexed) we can verify later with automated testing tools.

  2. Join several tables together (including some of these work tables) to product another work table

Repeat 1 and/or 2 until a final output is produced.

My concern is that this is a single SP and so gets an execution plan when it is first run (even WITH RECOMPILE). But at that time, the work tables (permanent tables in a Work schema) are empty.

I am concerned that, regardless of the indexing scheme, the execution plan will be poor.

I am considering breaking up the SP and calling separate SPs from within it so that they could take advantage of a re-evaluated execution plan after the data in the work tables is built. I have also seen reference to using EXEC to run dynamic SQL which, obviously might get a RECOMPILE also.

I'm still trying to get SHOWPLAN permissions, so I'm flying quite blind.


Solution

  • Are you able to determine whether there are any locking problems? Are you running the SP in sufficiently small transactions?

    Breaking it up into subprocedures should have no benefit.

    Somebody should be concerned about your productivity, working without basic optimization resources. That suggests there may be other possible unseen issues as well.