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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:33:34+00:00 2026-06-01T14:33:34+00:00

This is a common scenario, but i wanted to find out which way is

  • 0

This is a common scenario, but i wanted to find out which way is the performance optimized way and best practice.

I have a table with 4 columns: id, name, and two other fields. Id is the PK and name is a unique key. I’m reading data from excel file, populate the values of each row in a Domain object and then saving it. When saving, i want to see whether a record already exists for the same name and if exists, i want to update it. Else save it as a new record.

I can do it with normal select query for the name and check for null, and based on that insert or update but i have thousands of rows to be read from excel files and a non-functional requirement requested is the performance.

So please advice me on which is the best way to handle this senario? i haven’t started coding my persistence layer part yet, so i can switch to an ORM or plain jdbc according to your suggestion.

Edited:
If i use name as primary key, then i think i can use saveOrUpdate or merge from an ORM, to fullfill my need. Is it a good idea???
Thanks & regards,
Prasath.

  • 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-06-01T14:33:35+00:00Added an answer on June 1, 2026 at 2:33 pm

    I think the fastest way would be to carry out all the insert/updates in the database itself rather than connecting to it and using a large number of statements.

    Note, this is Oracle specific, but other databases may have similar concepts.

    I would use the following approach: First save the Excel data as a CSV file on the database server (/mydatadir/mydata.csv), then in Oracle I would be using an external table:

    create or replace directory data_dir as '/mydatadir/';
    create table external_table (
      id number(18),
      name varchar2(30),
      otherfield1 varchar2(40),
      otherfield2 varchar2(40))
    organization external (
      type oracle_loader
      default directory data_dir
      access parameters
      ( fields terminated by ',' )
      location ('mydata.csv')
    )
    

    (Note, the external table wouldn’t have to be set up every time)

    Then you can use the following command to merge the data into your table:

    merge into yourtable t
    using external_table e
    on t.name = e.name
    when matched then
       update set t.id = e.id, 
                  t.otherfield1 = e.otherfield1, 
                  t.otherfield2 = t.otherfield2
    when not matched then
       insert (t.id, t.name, t.otherfield1, t.otherfield2)
       values (e.id, e.name, e.otherfield1, e.otherfield2)
    

    This will upsert the rows in yourtable in one Oracle command, so all the work will be carried out by the database.

    EDIT:

    This merge command can be issued over plain JDBC (though I prefer using Spring’s SimpleJdbcTemplate)

    EDIT2:

    In MySQL you can use the following construct to perform the merge:

    insert into yourtable (id, name, otherfield1, otherfield2)
    values (?, ?, ?, ?), 
           (?, ?, ?, ?), 
           (?, ?, ?, ?) --repeat for each row in the Excel sheet...
    on duplicate Key update
    set otherfield1 = values(otherfield1),
        otherfield2 = values(otherfield2)
    

    This can be issued as a plain JDBC statement and is going to be better than a separate update and insert, and you can call these in batches of (say) a hundred rows from the spreadsheet. This would mean 1 JDBC call for every 100 rows in your Excel sheet and should perform well. That’ll allow you to do it without external tables (you’d need a UNIQUE index on the name column for this to work, I wouldn’t change the primary key as this could cause you problems with foreign keys if you needed to change somebody’s name).

    MySQL also has the concept of external tables, which I think would be faster still than inserting the data as batches as per above. As long as the csv file is uploaded to the correct location, the import should work quickly.

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

Sidebar

Related Questions

I'm looking for sort of a 'best practice' way to tackle this common scenario.
This should be a common scenario, but could not find any relevant post yet..
This is a very common scenario: displaying images in a ListView which have to
I think this must be a common scenario-- but I can't seem to find
I have this common issue of 'new line' in php-csv which is making me
I'm taking over a project and wanted to understand if this is common practice
I think this is a pretty common scenario, but I haven't been able to
I think this is a pretty common scenario: I have a webpage that's returning
This seems like a common scenario with an obvious solution, but somehow I haven't
I have a weird scenario but this was how it was designed before me.

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.