I created 3 tables like :
db.execSQL("CREATE TABLE IF NOT EXISTS programs (id_program INTEGER PRIMARY KEY AUTOINCREMENT , id_channel TEXT , nom_prgrm TEXT , debut TEXT , fin TEXT , FOREIGN KEY(id_channel) REFERENCES channels(id_channel)) ");
db.execSQL("CREATE INDEX __program_index ON programs(nom_prgrm)");
db.execSQL("CREATE TABLE IF NOT EXISTS categories (id_categorie INTEGER PRIMARY KEY AUTOINCREMENT, nom_categorie TEXT)");
db.execSQL("CREATE UNIQUE INDEX __categorie_index ON categories(nom_categorie,id_categorie)");
db.execSQL("CREATE TABLE IF NOT EXISTS categories_programs (id_categorie INTEGER REFERENCES categories(id_categorie) ,id_program INTEGER REFERENCES programs(id_program), PRIMARY KEY(id_categorie,id_program))");
programs table contains alomost 2500 records and categories 150 so the link table has 3400 rec
when i execute this query it takes a very long time to show me the result i don’t know how to optimiz this !!
SELECT id_channel , nom_categorie , debut , fin , nom_prgrm FROM programs, categories_programs, categories WHERE ( (categories_programs.id_program = programs.id_program) AND (categories_programs.id_categorie = categories.id_categorie) AND ('"+sentence+"' LIKE ('%' || nom_categorie || '%')))
probably it’s the join wich is causing this problem ??
thanks for help
My problem is solved i tried the same code on an android 4.1 and it worked very fast but with an S2 ( android 2.3) it was slow !
Probably the cause of the slowness is
LIKE ('%' || nom...having the wildcard at the beginning will make the database to parse the whole contents on that column.
Also, it would improve a bit if you prepare the statement and then bind the paramerer (though it will not be significant).
[EDIT]
If this is not the problem, you have to consider that you are evaluating the LIKE clause 3400 times (and you have only 150 categories).
Try prefiltering the category table with something like :
(I haven’t tested the query so it may have some typo)
[/EDIT]