Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

greenDAO 40 seconds to insert 600 records

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);
}
like image 711
midnight Avatar asked Oct 05 '12 14:10

midnight


1 Answers

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);
like image 123
midnight Avatar answered Oct 21 '22 06:10

midnight