1

I am downloading a json file that countain 5000 objects. This is done in 10 seconds, but when i am trying to save all those elements in my database, that take a huge time, more than 5 minutes.

Did any one face this problem? any explanation, why it take all this time? How can i fix this problem?

I used this answer to fix my problem: https://stackoverflow.com/a/3501572/833219

Community
  • 1
  • 1
haythem souissi
  • 3,263
  • 7
  • 50
  • 77
  • Make sure you download the file completely first, then insert to database. Otherwise you may be downloading the same json file over and over before every insert. – Dzhuneyt Nov 20 '12 at 14:05
  • I'm assuming that you open database, insert an item and close it... use bulk insert... – Buda Gavril Nov 20 '12 at 14:05
  • 1
    You should try to insert as many items as possible under one transaction. Inserting one item in one transaction is not efficient. – olshevski Nov 20 '12 at 14:07
  • Depends on android version, http://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html might give improvement. – sandrstar Nov 20 '12 at 14:10

1 Answers1

2

Try to use transactions to do multiple inserts. You can only do very few commits per second but lots of inserts per transaction.

http://www.sqlite.org/faq.html#q19

Jens
  • 2,050
  • 1
  • 14
  • 30