I have a problem that has been driving me crazy and I’d appreciate any thoughts you guys may have to offer. I import data from a CSV file (using openCSV) into a database, and previously this process has been completed in about 3-5 seconds including the download time from a server. The CSV file has 3035 records. I’ve tried separating the parsing from CSV and inserting to the table, but to no avail.
As strange as it may sound suddenly something has changed within my application where now this process takes much much longer.
After narrowing it down I’ve found that inserting into the table is extremely slow for SQLite. I’ve spent the last couple hours reverting back to older code to see if something has changed but to no luck.
I came across some similar question whose answer suggest me to use InsertHelper, which I did. Which made absolutely no difference in the import time. Currently inserting data takes 60 seconds, where it used to take just a couple.
public void importFromCSV()
{
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL("DELETE FROM EXAMS");
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot.getAbsolutePath() + "/Finals/Data/timetable.csv");
CSVReader reader;
int i = 0;
try {
reader = new CSVReader(new FileReader(file));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
this.addExam(new Exam(nextLine[0],
nextLine[1],
nextLine[2],
nextLine[3],
nextLine[4],
nextLine[5],
nextLine[6],
nextLine[7],
nextLine[8],
nextLine[9]));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void addExam(Exam exam) {
SQLiteDatabase db = this.getReadableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SESSION, exam.getSession());
values.put(KEY_AWARDING_BODY, exam.getAwardingBody());
values.put(KEY_QUALIFICATION, exam.getQualification());
values.put(KEY_TITLE, exam.getTitle());
values.put(KEY_EXAM_CODE, exam.getExamCode());
values.put(KEY_DURATION, exam.getDuration());
values.put(KEY_DATE, exam.getDate());
values.put(KEY_START_TIME, exam.getStartTime());
values.put(KEY_EXAM_NOTE, exam.getExamNote());
values.put(KEY_MY_NOTES, exam.getMyNotes());
db.insert(TABLE_NAME, null, values);
}
Your inserts may be slow because you are having to set up your references for every insert. Jeff Sharkey actually just talked about this problem in length at Google IO 1012. https://developers.google.com/events/io/sessions/gooio2012/103/ Basically you should be wrapping everything up in as few transactions as possible.