Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:25:06+00:00 2026-06-14T14:25:06+00:00

I have done some measurements regarding the IO on my SQLite database. It takes

  • 0

I have done some measurements regarding the IO on my SQLite database. It takes around 6 seconds to load some object from this database. I have a class which extends Asynctask, this class job is to load the last 10 entries in my database. This class is constructed this way:

import View.CustomAdapter;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.util.Log;
import android.view.*;

public class LoadCases extends AsyncTask<String, Integer, ArrayList<Case>> {

    ProgressBar progressBar;
    ArrayList<Case> lastTenCases; 
    CustomAdapter cAdapter; 
    Context context;
    ListView lastCases;
    DatabaseHandler db;
    TextView loading;
    int options;

    public LoadCases(ProgressBar progressBar, Context context, ListView lastCases, TextView loading, ArrayList<Case> lastTenCases) {
        this.progressBar = progressBar;
        this.context = context;
        this.lastCases = lastCases;
        this.loading = loading;
        this.lastTenCases = lastTenCases;
        db = new DatabaseHandler(context);
    }

    public void onPreExecute() {
        progressBar.incrementProgressBy(1);
    }


    @Override
    protected ArrayList<Case> doInBackground(String... params) {
        long start = System.nanoTime();
        lastTenCases = db.getAllCases(10);
        long end = System.nanoTime();
        Log.d("TIME TO LOAD CASES FROM BASE: ", Long.toString((end-start)/1000000000)); 
        return lastTenCases;
    }

    public void onPostExecute(ArrayList<Case> result) {
        progressBar.setVisibility(View.GONE);
        loading.setVisibility(View.GONE);
        cAdapter = new CustomAdapter(context, lastTenCases);    
        cAdapter.notifyDataSetChanged();
        lastCases.setAdapter(cAdapter); 

}
}

As you may see, the measurements is done just before I call the method getAllCases(10)

long start = System.nanoTime();
lastTenCases = db.getAllCases(10);
long end = System.nanoTime();
Log.d("TIME TO LOAD CASES FROM BASE: ", Long.toString((end-start)/1000000000)); 

This will give me about 6 seconds to load 10 cases from the database, as I mentioned earlier.

Now, over to the getAllCases method

public ArrayList<Case> getAllCases(int take) {

    ArrayList<Case> caseList = new ArrayList<Case>();
    String selectQuery = "SELECT  *, (strftime('%s', DATE) * 1000) AS DATENOW FROM " + TABLE_CASES + " ORDER BY " + DATE + " DESC LIMIT " + Integer.toString(take) + " ;";

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Case c = new Case();

            long millis = cursor.getLong(cursor.getColumnIndexOrThrow("DATENOW"));
            Date addedOn = new Date(millis);
            c.setCaseNumber(cursor.getString(0));
            c.setDate(addedOn.toLocaleString());
            c.setStatus(cursor.getString(cursor.getColumnIndex(STATUS)));
            caseList.add(c);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return caseList;
}

This method will construct Case objects, and add this to an ArrayList. This ArrayList will be return. Yeah, I guess you understand the code. But 6 seconds, really? What am I doing wrong? How can I speed up this operation?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:25:07+00:00Added an answer on June 14, 2026 at 2:25 pm

    It depends on Your database structure (e.g. if TABLE_CASES is just another complex query) and number of fields in TABLE_CASES, but at least the following points might be improved:

    • strftime('%s', DATE) * 1000) – do You really need to calculate it every time when You need to get CASE? Storing of that value instead of calculation will definitely speed up the query;
    • Not sure if ArrayList needed, probably CursorLoader could be used;
    • Based on this tip calling of new Date(millis) probably should be moved out of the loop;
    • Calls to getColumnIndexOrThrow and getColumnIndex should be also moved out of the loop;
    • toLocaleString is deprecated in API level 1 please use DateFormat

    Finally, I would suggest to follow this guide for traceview and determine if the delay actually happens in SQLite or in the loop just after it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done some searching around, and from what it looks like so far,
I have done some looking and I found this: http://www.codeproject.com/Articles/14439/The-ScrollableListBox-Custom-Control-for-ASP-NET-2 but to me it
i have done some code in jquery but where i commented in this bellow
Have done some research into this topic, but found no relevant answers. What I
I have done some Googling around it but couldn't find any relevant information. log4j
From my previous question I was getting NullPointor exception. So i have done some
I do not think this is a duplicate. I have done some reading but
I have a set of measurements done regularly, but some are missing: measurement_date value
I have done some research on this question before deciding to ask it. I
I have done some investigation around SOA and Component Based Architecture, and it seems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.