• It's exactly the same code, variables included. Part of what's missing is that there is a cartesian product in the code. Basically one of those..."let's look at a million rows where the ID is in (select another million rows)..." so the code, when executed outside the procedure produces an 80 million row select..

    That part of the code execution plan is NOT in the procedure execution plan and I suspect that the optimizer might be rewriting that particular part when it's compiling.