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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:52:47+00:00 2026-06-04T14:52:47+00:00

I want to insert 10,00,000 rows into the database, but the it takes too

  • 0

I want to insert 10,00,000 rows into the database, but the it takes too long time time in insertion like.

e.g. Now I am trying it with 2055 rows and it takes 3 minutes to upload this data into the database.. and this time is too much for 2055 entries.

The following is my method of inserting the data into the database:

      public void insert_database(Context context,String field1,String field2,String field3,String field4,String field5,String field6 ,String field7,String field8,String field9,String field10)
{

    try
    {
        //RayAllen_Database.beginTransaction();
        RayAllen_Database.execSQL(" insert or replace into "+ TableName_csv+" values( '"+field1+"' ,'"+field2+"','"+field3+"','"+field4+"','"+field5+"','"+field6+"','"+field7+"','"+field8+"','"+field9+"','"+field10+"');");


    }
    catch(Exception e)
    {
        //Log.i("Database Exception", "Exception");
        e.printStackTrace();
    }

}

and in the another class called: Parsing Data: here I am parsing the csv file and while parsing:

try {

CSVReader reader=new CSVReader(new FileReader(filename));
String [] nextLine;

//create database
obj.create_database(context);
obj.OpenDatabase(context);
//reader.readNext();

while ((nextLine=reader.readNext())!=null)
{
          //here I am calling the insert_database function
    }
 }

so here It is parsing row one by one and calling the insert method to insert the entry into the database..

But it is too much time taking.. How can I improve the performance of this??

  • 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-04T14:52:49+00:00Added an answer on June 4, 2026 at 2:52 pm

    Quick example time why you should do the right thing instead of “wrong”. This was tested running on ICS 4.0.4, which has horrible INSERT-performance.

    First, a simple SQLiteOpenHelper that creates a table with a UNIQUE constraint on a column to cause conflicts now-and-then.

    class SimpleHelper extends SQLiteOpenHelper {
        // InsertHelpers are a really good idea - they format a prepared statement
        // for you automatically.
        InsertHelper mInsert;
        public SimpleHelper(Context context) {
            super(context, "tanika.db", null, 1);
        }
        @Override
        public void onOpen(SQLiteDatabase db) {
            super.onOpen(db);
            mInsert = new InsertHelper(db, "target");
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE target (\n" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
                    "val1 TEXT NOT NULL,\n" +
                    "val2 TEXT NOT NULL,\n" +
                    "val3 TEXT NOT NULL,\n" +
                    // Let's make one unique so we can get some juicy conflicts
                    "val4 TEXT NOT NULL UNIQUE\n" +
                    ")");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
    

    Bundled in any old Activity we add the following simple test method:

    long test(final int n) {
        long started = System.currentTimeMillis();
        ContentValues values = new ContentValues();
    
        for (int i = 0; i < n; i++) {
            values.clear();
            // Every 20th insert, generate a conflict in val4
            String val4 = String.valueOf(started + i);
            if (i % 20 == 0) {
                val4 = "conflict";
            }
            values.put("val1", "Value1");
            values.put("val2", "Value2");
            values.put("val3", "Value3");
            values.put("val4", val4);
            mHelper.mInsert.replace(values);
        }
        return System.currentTimeMillis() - started;
    }
    

    As you can see, this would cause a conflict every 20th INSERT or so. Calling InsertHelper#replace(..) causes the helper to use a INSERT OR REPLACE on conflicts.

    Now, let’s run this test code with & without a transaction surrounding it.

    class Test1 extends AsyncTask<Integer, Void, Long> {
        @Override
        protected Long doInBackground(Integer... params) {
            return test(params[0]);
        }
        @Override
        protected void onPostExecute(Long result) {
            System.out.println(getClass().getSimpleName() + " finished in " + result + "ms");
        }
    }
    
    class Test2 extends AsyncTask<Integer, Void, Long> {
        protected Long doInBackground(Integer... params) {
            SQLiteDatabase db = mHelper.getWritableDatabase();
            db.beginTransaction();
            long started = System.currentTimeMillis();
            try {
                test(params[0]);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
            }
            return System.currentTimeMillis() - started;
        }
        @Override
        protected void onPostExecute(Long result) {
            System.out.println(getClass().getSimpleName() + " finished in " + result + "ms");
        }
    }
    

    Everything is started like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        mHelper = new SimpleHelper(this);
        mHelper.getWritableDatabase(); // Forces the helper to initialize.
        new Test1().execute(2055);
        new Test2().execute(2055);
    }
    

    And the results? Without a transaction the INSERTs take 41072ms. With transactions they take 940ms. In short, FFS, start using InsertHelpers and transactions.

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

Sidebar

Related Questions

I have roughly 100,000 Objects that I want to insert into the database. I
I want to insert a LARGE number of rows like this: INSERT INTO All
I want to insert values into a SQL Server table, but every time I
I want to insert say 50,000 records into sql server database 2000 at a
I want to insert a row into the Database using SqlDataAdapter. I've 2 tables
I want to insert a vector into a set like this: set<vector<prmEdge> > cammini;
Table table1 has got 500,000 rows. I want insert only 200,000 of them to
I have lists of about 20,000 items that I want to insert into a
I want to insert this margin, but I don't know why... I want it
I want to insert data into a table where I don't know the next

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.