سؤال

I've chosen greenDAO because it's site states that it's one of the fastest ORM systems for android. To my dissapointment it takes it like 40 seconds to insert 600 records on Samsung i9001. I'm not sure if I'm doing anything wrong.

Could you suggest anything to lessen the amount of time it takes to perform those operations ?

generator code:

private static void addNewsArticle(Schema schema) {
    Entity article = schema.addEntity("NewsArticle");
    article.addIdProperty().autoincrement();
    article.addStringProperty("title").notNull();
    article.addStringProperty("body").notNull();
    article.addStringProperty("shortDescription").notNull();
    article.addStringProperty("thumb");
    article.addDateProperty("date").notNull();
}

insertions

Date now = Calendar.getInstance().getTime();
for (int i = 0; i < 600; i++) {
    NewsArticle testArticle = new NewsArticle();
    testArticle.setTitle("title-text" + i);
    testArticle.setBody("body-text" + i);
    testArticle.setShortDescription("short-text" + i);
    testArticle.setDate(now);
    newsArticleDao.insert(testArticle);
}
هل كانت مفيدة؟

المحلول

As I suspected things weren't executed within a single sql statement. In order to achieve it just use insertInTx on a DAO object.

Here's the above code with slight changes that make it run in like half a second

NewsArticle[] newsArticles = new NewsArticle[600];
NewsArticle testArticle;
    for (int i = 0; i < 600; i++) {
         testArticle = new NewsArticle();
         testArticle.setTitle("title-text" + i);
         testArticle.setBody("body-text" + i);
         testArticle.setShortDescription("short-text" + i);
         testArticle.setDate(now);
         newsArticles[i] = testArticle;
    }
newsArticleDao.insertInTx(newsArticles);

نصائح أخرى

you can also create a new Runnable that does all the inserts runs within the DaoSession.runInTx function

daoSession.runInTx(new Runnable {
    public void run(){
        Date now = Calendar.getInstance().getTime();
        for (int i = 0; i < 600; i++) {
             NewsArticle testArticle = new NewsArticle();
                     testArticle.setTitle("title-text" + i);
                     testArticle.setBody("body-text" + i);
                     tesArticle.setShortDescription("short-text" + i);
                     testArticle.setDate(now);
                     newsArticleDao.insert(testArticle);
        }
   }
});

An excellent article on speeding up SQLITE insert operations for Android is given here. Tryout this I could optimize the sync modules speed dramatically from 1.2 hours to 14 mins in syncing 39MB DB.

http://tech.vg.no/2011/04/04/speeding-up-sqlite-insert-operations/

Let me know in case if you need code spinnets.

I tried below mentioned and got the best performance

        DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "db-name",
                null);
        SQLiteDatabase dbGreen= helper.getWritableDatabase();

        dbGreen.beginTransaction();

        //Perform your insertion here
        dbGreen.setTransactionSuccessful();
        dbGreen.endTransaction();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top