2015-01-28 5 views
11

मैं एंड्रॉइड पर स्क्लाइट प्रदर्शन को मापने के लिए कुछ प्रयोग कर रहा हूं। मैं परिणामों के साथ थोड़ा निराश था। मैंने जो किया वह तालिका में 10.000 प्रश्नों को डालने और में 130-140 सेकंडलिया गया लेकिन इन शर्तों के साथ; सत्ता मेंएंड्रॉइड स्क्लाइट प्रदर्शन

1. सैमसंग Galaxy S3 बचत मोड

2. सम्मिलित किए गए डेटा (या वर्ग) 3 तार और एक नाव (SQLite के लिए वास्तविक) है

3. सम्मिलित घटना asynctask में किया जा रहा है।

4. asynctask में, मैं एक प्रगति उस में पारित कर दिया टाइमर पाठ के साथ संवाद दिखा रहा (System.currentTimeMillis - सेकंड आदि blala)

class AddStudentsTask extends AsyncTask<Void,Integer,Void> 
{ 
    ProgressDialog prgDialog; 
    int max = 10000; 
    Student s; 
    long seconds = System.currentTimeMillis(); 


    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     prgDialog = new ProgressDialog(MainActivity.this); 
     prgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     prgDialog.setMessage(seconds+""); 
     prgDialog.setMax(max); 
     prgDialog.setCancelable(false); 
     prgDialog.show(); 

    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(); 
     prgDialog.setProgress(values[0]); 
     sList.add(s); 
     String s = (System.currentTimeMillis()-seconds)/100+""; 
     if(s.length()>2) 
      s = s.substring(0,s.length()-1) + "." + s.charAt(s.length()-1); 
     else if(s.length() == 2) 
      s = s.charAt(0) + "." + s.charAt(1); 
     prgDialog.setMessage(s + " seconds passed."); 

    } 

    @Override 
    protected Void doInBackground(Void... voids) { 

     for(int a = 0;a< max; a++) 
     { 
      Random r = new Random(); 
      s = new Student(); 

      s.setGpa(r.nextFloat()*4); 
      s.setLastName("asdasd"); 
      s.setFirstName("Oh My Fuckig God"); 
      s.setAddress("1sadasd"); 
      s.setId(sda.insert(s)); 
      publishProgress(a); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     prgDialog.dismiss(); 
     sa.notifyDataSetChanged(); 
    } 
} 

5. मैं insertOrThrow विधि के साथ contentValues ​​उपयोग कर रहा हूँ हेल्परडब कक्षा में। इस पुराने धीमी कोड

public long insert(Student s) 
{ 
    SQLiteDatabase db = sh.getWritableDatabase(); 
    ContentValues cv = new ContentValues(); 
    cv.put(StudentHelper.FIRSTNAME,s.getFirstName()); 
    cv.put(StudentHelper.LASTNAME,s.getLastName()); 
    cv.put(StudentHelper.ADDRESS,s.getAddress()); 
    cv.put(StudentHelper.GPA,s.getGpa()); 
    s.setId(db.insertOrThrow(StudentHelper.TABLE_NAME, null, cv)); 
    return s.getId(); 
} 

6. यह कार्य गतिविधि के onCreate विधि में किया जाता है।

तो मैं यहां क्या गलत कर रहा हूं या इससे बहुत अधिक उम्मीद कर रहा हूं? क्या ये परिणाम ठीक हैं या बुरे हैं?

मैं अपना कोड सुधारने के लिए क्या कर सकता हूं?

संपादित

तो मैं इस के लिए मेरे डालने कोड बदल गया और यह 4.5 सेकंड तक कम हो !!!

public ArrayList<Long> insertMany(ArrayList<Student> stus) 
{ 
    ArrayList<Long> ids = new ArrayList(); 
    String sql = "INSERT INTO "+StudentHelper.TABLE_NAME+"" + 
      "("+StudentHelper.FIRSTNAME+","+StudentHelper.LASTNAME+"," + 
      " "+StudentHelper.GPA+") values(?,?,?)"; 
    SQLiteDatabase db = sh.getWritableDatabase(); 
    db.beginTransaction(); 

    for(Student s:stus) { 
     SQLiteStatement stmt = db.compileStatement(sql); 

     stmt.bindString(1, s.getFirstName()); 
     stmt.bindString(2, s.getLastName()); 
     stmt.bindDouble(3, s.getGpa()); 

     s.setId(stmt.executeInsert()); 
     ids.add(s.getId()); 
     stmt.clearBindings(); 
    } 

    db.setTransactionSuccessful(); 
    db.endTransaction(); 

    return ids; 
} 
+0

मैं नहीं दिख रहा है 'endTransaction' या' setTransactionSuccessful' अगर आप सिर्फ अपने 'insert' विधि में' beginTransaction' जोड़ा तो आप पूरे मुद्दे याद किया। –

+0

@ एम-वाजेएह आप सही हैं। मैंने अपनी पोस्ट संपादित की। रचनात्मक आलोचना के लिए धन्यवाद !! –

+1

नहीं, अभी भी गलत है। आपको बस इतना करना 'beginTransaction' और फिर 10.000 सम्मिलन और उसके बाद' setTransactionSuccessful' और उसके बाद 'endTransaction' है। काम करने के लिए अपने 'AddStudentsTask' को संशोधित करें। –

उत्तर

25

उपयोग SQLite transaction अप गति

उपयोग शुरू लेनदेन & समाप्ति लेन-देन SQLite अनुकूलन

प्रत्येक SQL विवरण, SQLite क्रम करके एक नया लेनदेन ब्लॉक में संलग्न है डिफ़ॉल्ट रूप से के लिए के लिए। सोहेन आप एक मूल डीबी ऑपरेशन करते हैं जैसे INSERT, एक लेनदेन ब्लॉक बनाया जाएगा और इसके चारों ओर लपेटा जाएगा।

SQLite रनटाइम आपके लिए लेनदेन का प्रबंधन करने के लिए केवल तभी सलाह दी जाती है जब आपका दिनचर्या डेटा सेट पर केवल एक डीबी ऑपरेशन करता है। हालांकि, यदि आप कई डीबी संचालन कर रहे हैं (INSERT लूप के अंदर) कहें, यह बहुत महंगा हो जाता है, क्योंकि इसे प्रत्येक कथन के लिए जर्नल फ़ाइल को फिर से खोलने, लिखने और बंद करने की आवश्यकता होती है। आप उल्लेख कर सकते हैं

  1. Android SQLite database: slow insertion

  2. http://www.androidcode.ninja/android-sqlite-transaction-tutorial/

  3. http://www.techrepublic.com/blog/software-engineer/turbocharge-your-sqlite-inserts-on-android/

  4. http://www.android-app-market.com/sqlite-optimization-in-android-programming-sqlite-optimization-in-android-apps.html

+0

केवल एक लिंक के ऊपर मदद करें ताकि आप कुछ स्पष्टीकरण प्रदान कर सकें – duggu

+3

धन्यवाद जवाब। यह 20 गुना तेज हो गया। इसमें 7.5 सेकंड –

+0

@MertSerimer आपका स्वागत है !!! मैंने इसे अपने कुछ ऐप्स में भी प्रस्तुत किया। –

1

ठीक है, आप इतने सारे रिकॉर्ड है और हर रिकार्ड के लिए आप getWritableDatabase() फोन और फिर इसे डालने, जो प्रदर्शन के लिए बुरा है।

आप लेन-देन का उपयोग करने की जरूरत है। इस प्रश्न को देखें Android SQLite database: slow insertion। मूल रूप से आप, beginTransaction() फोन अपने सम्मिलन और फिर setTransactionSuccessful() फोन endTransaction() से पहले की जरूरत है। आप यह देखकर आश्चर्यचकित होंगे कि आपको इसके साथ कितना प्रदर्शन लाभ मिलेगा।

8

आप एसक्यूएल लेनदेन इस तरह एंड्रॉयड में उपयोग कर सकते हैं। यह बड़ा बैचों में डेटाबेस में एकाधिक पंक्तियों को सम्मिलित करने के बेहतर है तो बनाने ही प्रतिबद्ध हर डाला पंक्ति के लिए (SQLlite datafile जो बहुत धीमी है में लिखना)।

public void insert(List<Student> students) 
{ 
    SQLiteDatabase db = sh.getWritableDatabase(); 
    ContentValues cv = new ContentValues(); 

    db.beginTransaction(); 

    try { 
     for (Student s : students) { 
       cv.put(StudentHelper.FIRSTNAME,s.getFirstName()); 
       cv.put(StudentHelper.LASTNAME,s.getLastName()); 
       cv.put(StudentHelper.ADDRESS,s.getAddress()); 
       cv.put(StudentHelper.GPA,s.getGpa()); 

       db.insertOrThrow(StudentHelper.TABLE_NAME, null, cv) 
     } 
     setTransactionSuccessful(); 
    } finally { 
     db.endTransaction(); 
    } 
} 
संबंधित मुद्दे