2013-08-18 100 views
0

我有SQLite數據庫表。雖然搜索它非常緩慢。需要20-30秒才能找到合適的單詞。原因是我正在使用(我必須)在我的查詢中加入2-3個WHERE子句。如何創建和使用索引?

query = "SELECT" + " e.id AS _id," + " ke.id AS ke_id," 
       + " ke.fk as ke_fk," + " ke.value as ke_value," 
       + " re.id AS ke_id," + " re.fk as re_fk," 
       + " re.value as re_value," + " s.id AS s_id," 
       + " s.fk as s_fk," + " g.id AS g_id," + " g.fk as g_fk," 
       + " g.lang," + " g.value as g_value" + " FROM entry e" 
       + "  INNER JOIN k_ele ke ON e.id = ke.fk" 
       + "  INNER JOIN r_ele re ON e.id = re.fk" 
       + "  INNER JOIN sense s ON e.id = s.fk" 
       + "  INNER JOIN gloss g ON s.id = g.fk" 
       + " WHERE g.lang IS NULL AND g.value like '" 
       + lookingFor + "%' GROUP BY g.value LIMIT 5"; 

這是表之間的關係: enter image description here

我已閱讀有關索引(伯,唯一的)。我知道索引時的排序值。我的問題是如何創建(我不是在談論爲id創建PRIMARY UNIQUE INDEX,我說的是不在id列中進行搜索),並使用該索引來加速搜索查詢?以編程方式或在應用程序(SQLite專家專業)?如果我創建索引每次搜索時創建索引?

請給出您的意見或建議。謝謝。

+0

您是否使用交易? – Pavlos

+0

不好意思,但它對我有什麼幫助? –

+0

此查詢的[EXPLAIN QUERY PLAN](http://www.sqlite.org/eqp.html)的輸出是什麼? –

回答

0

LIKE在任何情況下都非常緩慢,並且不使用索引(如果我錯了,請糾正我)。如果您的目標是全文搜索,請查看SQLite的FTS3功能。那裏有很多樣品。例如:FTS3 blog