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 6879921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:54:30+00:00 2026-05-27T04:54:30+00:00

We have a ‘merge’ script that is used to assign codes to customers. Currently

  • 0

We have a ‘merge’ script that is used to assign codes to customers. Currently it works by looking at customers in a staging table and assigning them unused codes. Those codes are marked as used and the staged records, with codes, loaded to a production table. The staging table gets cleared and life is peachy.

Unfortunately we are working with a larger data set now (both customers and codes) and the process is taking WAY to long to run. I’m hoping the wonderful community here can look at the code here and offer either improvements upon it or another way of attacking the problem.

Thanks in advance!

Edit – Forgot to mention part of the reason for some of the checks in this is that the staging table is ‘living’ and can have records feeding into it during the script run.

whenever sqlerror exit 1

-- stagingTable: TAB_000000003134
-- codeTable: TAB_000000003135
-- masterTable: TAB_000000003133

-- dedupe staging table
delete from TAB_000000003134 a
where ROWID > (
  select min(rowid)
  from TAB_000000003134 b
  where a.cust_id = b.cust_id
  );
commit;

delete from TAB_000000003134
where cust_id is null;
commit;


-- set row num on staging table
update TAB_000000003134
set row_num = rownum;
commit;

-- reset row nums on code table
update TAB_000000003135
set row_num = NULL;
commit;

-- assign row nums to codes
update TAB_000000003135
set row_num = rownum
where dateassigned is null
and active = 1;
commit;

-- attach codes to staging table
update TAB_000000003134 d
set (CODE1, CODE2) =
(
  select CODE1, CODE2
  from TAB_000000003135 c
  where d.row_num = c.row_num
);
commit;

-- mark used codes compared to template
update TAB_000000003135 c
set dateassigned = sysdate, assignedto = (select cust_id from TAB_000000003134 d where c.CODE1 = d.CODE1)
where exists (select 'x' from TAB_000000003134 d where c.CODE1 = d.CODE1);
commit;

-- clear and copy data to master
truncate table TAB_000000003133;
insert into TAB_000000003133 (
        <custmomer fields>, code1, code2, TIMESTAMP_
        )
select <custmomer fields>, CODE1, CODE2,SYSDATE
from TAB_000000003134;
commit;

-- remove any staging records with code numbers
delete from TAB_000000003134
where CODE1 is not NULL;
commit;

quit
  • 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-27T04:54:30+00:00Added an answer on May 27, 2026 at 4:54 am
    • Combine statements as much as possible. For example, combine the first two deletes by simply adding “or cust_id is null” to the first delete. This will definitely reduce the number of reads, and may also significantly decrease the amount of data written. (Oracle writes blocks, not rows, so even if the two statements work with different rows they may be re-writing the same blocks.)
    • It’s probably quicker to insert the entire table into another table than to update every row. Oracle does a lot of extra work for updates and deletes, to maintain concurrency and consistency. And updating values to NULL can be especially expensive, see update x set y = null takes a long time for some more details. You can avoid (almost all) UNDO and REDO with direct-path inserts: make sure the table is in NOLOGGING mode (or the database is in NOARCHIVELOG mode), and insert using the APPEND hint.
    • Replace the UPDATEs with MERGEs. UPDATEs can only use nested loops, MERGEs can also use hash joins. If you’re updating a large amount of data a MERGE can be significantly faster. And MERGEs don’t have to read a table twice if it’s used for the SET and for a EXISTS. (Although creating a new table may also be faster.)
    • Use /*+ APPEND */ with the TAB_000000003133 insert. If you’re truncating the table, I assume you don’t need point-in-time recovery of the data, so you might as well insert it directly to the datafile and skip all the overhead.
    • Use parallelism (if you’re not already). There are side-affects and dozens of factors to consider for tuning, but don’t let that discourage you. If you’re dealing with large amounts of data, sooner or later you’ll need to use parallelism if you want to get the most out of your hardware.
    • Use better names. This advice is more subjective, but in my opinion I think using good names is extremely important. Even though it’s all 0s and 1s at some level, and many programmers think that cryptic code is cool, you want people to understand and care about your data. People just won’t care as much about TAB_000000003135 as something like TAB_CUSTOMER_CODES. It’ll be harder to learn, people are less likely to change it because it looks so complicated, and people are less likely to see errors because the purpose isn’t as clear.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a fun issue with sharepoint calendar view filtering. That code works fine: SPSecurity.RunWithElevatedPrivileges(delegate()
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have a rather abstract question for you all. I'm looking at getting involved in
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a look at one of my websites: moskah.com The problem is that it
Have a simple contact us XPage created. Have server side validation in place that
Have some dates in my local Oracle 11g database that are in this format:
Have anyone used Redmine Documentor which lets you convert PHP to HTML to Redmine
Have an app that can use tts to read text messages. It can also
Have a client who is looking at developing a site where clients get access

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.