title: Most Efficient Way to Insert 5000+ Android Contacts
body: <p>I realize this has been somewhat touched upon in various places including <a href="https://stackoverflow.com/questions/5596354/insertion-of-thousands-of-contact-entries-using-applybatch-is-slow">here</a> on Stack Overflow, but I'm looking for any other solutions that people might have used.  So with that in mind...</p>
  
  <p>I'm developing an application where a user can initially sync all his contacts with a desktop application OTA.  This is done through a web service call that grabs a set of 100 contacts from the server, downloads and parses the information, inserts the contacts into the Android Contact DB, acknowledges receipt of these contacts, and then repeats the previous steps with the next set of 100 contacts until the sync is complete.  This process works well when a user has contacts on the order or 1000-2000, but a typical user of this application can easily have 5000-6000 contacts (with power users having upwards of 10000+) in which case things take far longer than I'd like.  For example, a sample set of approximately 5300 contacts can take about 13.5 minutes to complete.  Not bad, but I'd like it to be at least as efficient as iOS which runs about 8 minutes for the same data set if possible.</p>
  
  <p>I've logged the time it takes for each step and, unsurprisingly, the bottleneck appears to be with inserting the data into the Android contract DB.  After scouring the web I've found little help with regards to inserting thousands of contacts, but what I have found seems to fall into these three groups:</p>
  
  <p>1) ContentProviderOperation -- The Google recommended way which gave me my baseline of 13.5 minutes for 5300 contacts.</p>
  
  <p>2) Bulk Inserts -- I read that builkInsert tends to be more efficient than applyBatch, but when I tried to implement this myself it actually took 25 minutes for the same 5300 contacts.  I have a feeling a lot of this is due to the fact that I need to insert the RawContact information and then save the resulting URI for use in creating the ContactsContract.Data for the bulkInsert which comes more naturally via the backValueReference in the ContentProviderOperation.  Additionally, I looked at the android source code and I don't get the feeling that bulkInsert is terribly efficient. </p>
  
  <p>3) Creating an optimized bulk insert using the DatabaseUtils.InsertHelper and transactions -- Unfortunately, this seems geared towards those people who created their own content provider because you need access to the underlying DB as an instance variable and I've yet to see how that could be done with the native contacts DB.</p>
  
  <p>Does anyone have any experience with inserting 5000+ contacts or any other possible ideas I could look into to help reduce my time?  Or should the ContentProviderOperation be considered as optimized as it's going to get?</p>