1

My app is using an external SQLite database. The database is created using DB Browser for SQLite software. I am using the following method to query my table with the column ENGLISH (same as en_word). However, problem is the query is slow when my database become large.

public static final String ENGLISH = "en_word";
public static final String TABLE_NAME = "words";

    String sql = "SELECT * FROM " + TABLE_NAME +
                    " WHERE " + ENGLISH + " LIKE ? ORDER BY LENGTH(" + ENGLISH + ") LIMIT 100";

            SQLiteDatabase db = initializer.getReadableDatabase();

            Cursor cursor = null;
            try {
                cursor = db.rawQuery(sql, new String[]{"%" + englishWord.trim() + "%"});

                List<Bean> wordList = new ArrayList<Bean>();
                while(cursor.moveToNext()) {
                    String english = cursor.getString(1);
                    String mal = cursor.getString(2);
                    wordList.add(new Bean(english, mal));
                }

                return wordList;
            } catch (SQLiteException exception) {
                exception.printStackTrace();
                return null;
            } finally {
                if (cursor != null)
                    cursor.close();
            }

I tried to create index using DB Browser for SQLite.

CREATE INDEX `kindx` ON `words` ( `en_word` )

However, do I need to modify my code so that my app will query the database using this index? If so, how to do that?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
user2872856
  • 2,003
  • 2
  • 21
  • 54

1 Answers1

0

The problem is that SQLite, like most relational databases, can use an index when the parameter to a 'like' clause ends with a wildcard, it cannot use an index when the parameter begins with a wildcard.

So, for this type of query, the index will not be used, and you wind up with a full table scan. This is why it is slower with a large number of rows.

You are actually attempting to do what is known as "full text search", which is not really possible to do efficiently without database features to support it directly.

I have not tried it, but I see that SQLite does have full-text search capabilities, and that it is supported on Android. See Full text search example in Android for an example.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67