简体   繁体   English

从多维数组获取值太慢

[英]Getting values from multi-dimensional array too slow

Good Day, I've been working on an update in an android project, and came across an issue. 美好的一天,我一直在进行android项目中的更新,遇到了一个问题。 I have to read questions from an SQLite database which i've done successfully by loading it into a multi-dimensional array as shown below in my database helper class: 我必须从SQLite数据库中读取问题,这已经通过将其加载到多维数组中而成功完成,如下数据库助手类中所示:

public String getSome(int s,int t, String Table_Name){

        String selectQuery = "SELECT  * FROM " + Table_Name;
              SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        int rows = cursor.getCount();
        int num=0;
        int col = 0;
        String[][] base = new String[rows][13]; 

        if (cursor.moveToFirst()) {
            do {
                for (col=0;col<13;++col ){
                base[num][col] = (cursor.getString(col));} 
                ++num;
            } while (cursor.moveToNext());

            return base[s][t];
        }



        return null;

    }

With that done, i read the questions as such in my question class: 完成之后,我在问题类中阅读了这样的问题:

public void database_calls(){
          setCourseTag(courseTag);
          myDbHelper = new DataBaseHelper(getActivity());


            try {
                myDbHelper.createDataBase();
            } catch (IOException ioe) {
            throw new Error("Unable to create database");
            }

            try {
                myDbHelper.openDataBase();
            }catch(SQLException sqle){
                throw sqle;
            }

            String no= myDbHelper.getSome(ques,0, getCourseTag());
            String qu = myDbHelper.getSome(ques,1, getCourseTag());
            String a = myDbHelper.getSome(ques,2, getCourseTag());
            String b = myDbHelper.getSome(ques,3, getCourseTag());
            String c = myDbHelper.getSome(ques,4, getCourseTag());
            String d = myDbHelper.getSome(ques,5, getCourseTag());
            ans = myDbHelper.getSome(ques,6, getCourseTag());
            img = Integer.parseInt(myDbHelper.getSome(ques,8, getCourseTag()));
            exp = myDbHelper.getSome(ques,9, getCourseTag());
            year = myDbHelper.getSome(ques,10, getCourseTag());
            questionImage = myDbHelper.getSome(ques,11, getCourseTag());
            length = myDbHelper.getMax(getCourseTag());
        }

So recently, i tried to use the year column, (ie, column 10) to qualify the questions chosen for each quiz session, so that the user may be able to select the questions from any year, he/she wants to attempt. 因此最近,我尝试使用year列(即第10列)来限定为每个测验会话选择的问题,以便用户可以从他/她想要尝试的任何年份中选择问题。 In order to do this, i used a loop at the beginning of the activity to filter out only the required year past questions. 为了做到这一点,我在活动开始时使用了一个循环来仅过滤掉所需的过去一年的问题。 Then i transferred the indices of each question to a set, bal , from where it is iterated and so on.. 然后,我将每个问题的索引从迭代的地方转移到集合bal ,依此类推。

public void countYearQuestions(){

        for(int y = 0; y < length; ++y){
            //year = myDbHelper.getSome(y,10, getCourseTag()); 

            if (selectedYear.equals(myDbHelper.getSome(y,10, getCourseTag())))
              bal.add(y);
        }
    }

Here, length is the size of the entire question database, for the course, (indicated by getCourseTag()). 在这里,长度是课程中整个问题数据库的大小(由getCourseTag()表示)。 The code works quite alright. 该代码工作正常。 But it takes a whole 8-9secs!! 但这需要整个8-9秒!! for the activity to load. 活动加载。 Any help on how to reduce this loading time would be appreciated. 任何有关如何减少此加载时间的帮助将不胜感激。

The way you're doing this is pretty efficient in slowing everything down (and your helper helps you with it a lot): 您执行此操作的方式非常有效,可以减慢所有操作的速度(并且您的助手可以为您提供很多帮助):

String no= myDbHelper.getSome(ques,0, getCourseTag());

In each such line you execute a query, create a 2D array holding the whole table and throw nearly everything away. 在执行此类查询的每一行中,创建一个包含整个表的2D数组,并将几乎所有内容扔掉。 And you fail to close the Cursor . 并且您无法关闭Cursor

So you need 12 values from a table and read instead the whole table 12 times. 因此,您需要从一个表中获取12个值,然后读取整个表12次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM