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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:07:37+00:00 2026-05-26T06:07:37+00:00

I have a large table foo_large and a relatively small (a few hundred thousand

  • 0

I have a large table foo_large and a relatively small (a few hundred thousand rows) table foo_small. The large table has a primary-key column “id”; the small one also has “id” column that is a foreign key to foo_large. I want to update foo_small so that for every row its col_y has a value equal to col_x in the corresponding row of foo_large. The most straightforward way seems to be like this:

update foo_small sm
set col_y = (
  select col_x
  from foo_large
  where id = sm.id);

This is, however, very inefficient. For each row of foo_small, the corresponding row of foo_large is accessed by the index on its primary key. Though foo_small is small compared to foo_large, it still leads to several hundred thousand of index scans on that table. A better solution would be to hash foo_small in memory and perform a (possibly parallelized) full scan on foo_large once, updating the matching rows of foo_small encountered. I can do that in the following way:

update
(
  select /*+ ordered use_hash(lg) parallel(lg 2) */
    sm.*, lg.col_x
  from
    foo_small sm,
    foo_large lg
  where sm.id = lg.id
)
set col_y = col_x;

This query finishes within a minute. Unfortunately, it has another disadvantage: It requires that the user launching this query has a privilege to update not only foo_small, but also foo_large even though the latter table is not actually updated. Is there a solution to force the latter execution plan without updating a join? I know I could write a piece of procedural PL/SQL code with bulk fetch/update and probably keep most of the performance gain, but I suppose there must be a way to do it in a single query.

Thanks in advance.

  • 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-26T06:07:37+00:00Added an answer on May 26, 2026 at 6:07 am

    Here follows the ultimate query, suggested by Shannon, that does the job:

    merge /*+ leading(sm) full(lg) use_hash(lg) parallel(lg 2) */
      into foo_small sm
    using foo_large lg
      on (lg.id = sm.id)
    when matched then
      update set sm.col_y = lg.col_x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large table (~1M rows now, soon ~10M) that has two ranked
I have a pretty large table (20M records) which has a 3 column index
Have a large table here with no primary key and im trying to add
I have a large table (between 74 and 88million rows) which is the middle
I have a large table (2,000,000 rows) and I'd like to print each record
I have a large table (several million rows) in a MySQL (version 5.0) database
We have a large MyISAM table to which rows get inserted to the bottom
I have a large table with numbers only and a small font-size, which makes
I have a large table (potentially millions of rows) that in its naive form
I have large table (~1,000,000 rows) that may contain duplicates values. the table contain

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.