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

  • SEARCH
  • Home
  • 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 6330229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:50:01+00:00 2026-05-24T17:50:01+00:00

This question applies to stored procedures written in SQL-92 i.e. Oracle PL/SQL, SQL Server

  • 0

This question applies to stored procedures written in SQL-92
i.e. Oracle PL/SQL, SQL Server T-SQL or DB2 SQL

I’m maintaining an 11000 line stored procedure.
I find that by the end of the stored procedure I need to report out 80 columns of data.

There are 3 distinct phases in this stored procedure.

  1. Data Gathering (copying data from the live tables into the stored procedure intermediate tables)
    I need to do data gathering for consistency because the LIVE data (i.e. in the member table) at line 30 might have changed by the time the stored procedure execution gets to line 10,000
    Commit state atomicity is maintained here (no commit until all the data needed is copied over)
  2. Calculation (lots of SQL, complicated enough so that Cursors or Views will not do the job)
  3. Writing back to permanent tables (invoices, AR, payments)
    Commit state atomicity is maintained here (no commit until all the data needed is copied over)

The “intermediate” tables are only used in the stored procedure.
They are indexed for joins down the line but do not have
PK/FK referential integrity constraints or unique indexes
as these would slow down execution considerably in addition
to pointing back to LIVE data (that is in flux)

When you get to 80 columns of data that you need to report by the end of a stored
procedure you run up against RDBMS limitations (index limits, memory limits,
SQL join COST limits, out of control paging and data going to virtual
memory/swap when the DB thinks it should use HASH instead of using NESTED LOOPS)

I have normalized LIVE data (that gets written to and read from 24/7 by data entry users)

It occurred to me that the way to optimize the space taken up by intermediate tables
used in the stored procedure (at step 2) would be to find composite primary keys and assign
each a unique id (a surrogate PK) thereby referencing n columns with 1 column. Then, I
would reconstitute this data at the end of step 2 and have it ready to
write back to at the start of step 3. This would add more processing to
step 2 but less data would get copied around. Also debugging would take
more steps (tracing back ids to actual data in the intermediate
table data after execution completes)

Has anyone run into this scenario with lengthy stored procedures?
Has anyone created a surrogate key (replacing a compound PK with a one-column PK) in
intermediate tables that are only used in stored procedures?

Has that paid off in terms of execution time and memory/space used during
execution?

  • 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-24T17:50:01+00:00Added an answer on May 24, 2026 at 5:50 pm

    I’ve built a few lenghty SProcs and I’ve always gone for an Indentity column surrogate key. Is it possible to rethink what’s being done and create separate temp tables for each intermediate step?

    I’ve had to do this in the past. At the end, I “stitched” all the separate temp tables into my final output.

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

Sidebar

Related Questions

I believe this question applies to any of the For Html helpers, but my
I believe this question applies equally well to C# as to Java, because both
I have a HashMap (although I guess this question applies to other collections) of
I'm using Castle ActiveRecord, but this question applies to NHibernate, too, since a solution
I'm coming largely from a c++ background, but I think this question applies to
(This question specifically in C#, but applies generally to .NET) I have a largish
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
I'm using IntelliJ IDEA 9.0.2 but I assume this question applies to previous versions
AFAIK, this question applies equally to C and C++ Step 6 of the translation
TSQL here. Specifically Server 2008(literally just upgraded) Concerning stored procedures: Try/Catch I was trying

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.