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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:08:08+00:00 2026-05-21T21:08:08+00:00

I am trying to implement simple program in Java that will be used to

  • 0

I am trying to implement simple program in Java that will be used to populate a MySQL database from a CSV source file. For each row in the CSV file, I need to execute following sequence of SQL statements (example in pseudo code):

execute("INSERT INTO table_1 VALUES(?, ?)");
String id = execute("SELECT LAST_INSERT_ID()");
execute("INSERT INTO table_2 VALUES(?, ?)");
String id2 = execute("SELECT LAST_INSERT_ID()");
execute("INSERT INTO table_3 values("some value", id1, id2)");
execute("INSERT INTO table_3 values("some value2", id1, id2)");
...

There are three basic problems:
1. Database is not on localhost so each single INSERT/SELECT has latency and this is the basic problem
2. CSV file contains millions of rows (like 15 000 000) so it takes too long.
3. I cannot modify the database structure (add extra tables, disable keys etc).

I was wondering how can I speed up the INSERT/SELECT process? Currently 80% of the execution time is consumed by communication.

I already tried to group the above statements and execute them as batch but because of LAST_INSERT_ID it does not work. In any other cases it takes too long (see point 1).

  • 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-21T21:08:09+00:00Added an answer on May 21, 2026 at 9:08 pm

    Feed the data into a blackhole

    CREATE TABLE  `test`.`blackhole` (
      `t1_f1` int(10) unsigned NOT NULL,
      `t1_f2` int(10) unsigned NOT NULL,
      `t2_f1` ... and so on for all the tables and all the fields.
    ) ENGINE=BLACKHOLE DEFAULT CHARSET=latin1;
    

    Note that this is a blackhole table, so the data is going nowhere.
    However you can create a trigger on the blackhole table, something like this.

    And pass it on using a trigger

    delimiter $$
    
    create trigger ai_blackhole_each after insert on blackhole for each row
    begin
      declare lastid_t1 integer;
      declare lastid_t2 integer;
    
      insert into table1 values(new.t1_f1, new.t1_f2);
      select last_insert_id() into lastid_t1;
      insert into table2 values(new.t2_f1, new.t2_f1, lastid_t1);
      etc....
    end$$
    
    delimiter ;
    

    Now you can feed the blackhole table with a single insert statement at full speed and even insert multiple rows in one go.

    insert into blackhole values(a,b,c,d,e,f,g,h),(....),(...)...
    

    Disable index updates to speed things up

    ALTER TABLE $tbl_name DISABLE KEYS;
    ....Lot of inserts
    ALTER TABLE $tbl_name ENABLE KEYS;
    

    Will disable all non-unique key updates and speed up the insert. (an autoincrement key is unique, so that’s not affected)

    If you have any unique keys and you don’t want MySQL to check for them during the mass-insert, make sure you do an alter table to eliminate the unique key and enable it afterwards.
    Note that the alter table to put the unique key back in will take a long time.

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

Sidebar

Related Questions

I'm trying to implement a simple activity that will let user to insert a
Am trying to implement a generic way for reading sections from a config file.
i am trying to implement a simple server application in java. all it does
I'm new to Flex SDK and trying to implement a simple project using Doug
I am trying to do a very simple command line library for interactive Java
I'm trying to get a simple program using JOGL to compile via the command
I'm trying to figure out how to structure a program using Java's generics, and
I'm trying to implement a very, very simple accessibility test for Swing so I
Hi I am trying to implements a simple Java NIO server; which registers the
I'm trying to implement a program which runs a function limited by a fixed

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.