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??
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
SQLiteOpenHelperthat creates a table with aUNIQUEconstraint on a column to cause conflicts now-and-then.Bundled in any old
Activitywe add the following simple test method:As you can see, this would cause a conflict every 20th
INSERTor so. CallingInsertHelper#replace(..)causes the helper to use aINSERT OR REPLACEon conflicts.Now, let’s run this test code with & without a transaction surrounding it.
Everything is started like this:
And the results? Without a transaction the
INSERTs take 41072ms. With transactions they take 940ms. In short, FFS, start usingInsertHelpers and transactions.