我有三個表sqlite3的數據庫:SQLite數據庫優化
CREATE TABLE document (
id Int PRIMARY KEY NOT NULL,
root_id Int,
name Varchar(100),
active Tinyint
);
CREATE INDEX IDX_documentId ON document (id);
CREATE INDEX IDX_documentName ON document (name);
CREATE TABLE dictionary (
id Int PRIMARY KEY NOT NULL,
word Varchar(100) NOT NULL
);
CREATE INDEX IDX_dictionaryId ON dictionary (id);
CREATE UNIQUE INDEX IDX_dictionaryWord ON dictionary (word ASC);
CREATE TABLE document_index (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
document_id Int NOT NULL,
word_id Int NOT NULL,
FOREIGN KEY(document_id) REFERENCES document(id),
FOREIGN KEY(word_id) REFERENCES dictionary(id)
);
CREATE INDEX IDX_documentIndexId ON document_index (id);
CREATE INDEX IDX_documentIndexDocId ON document_index (document_id);
CREATE INDEX IDX_documentIndexWordId ON document_index (word_id);
而且我的SQL腳本,以選擇所有文件至極包含從字典單詞:
SELECT document.id, document.name
FROM document
INNER JOIN document_index on document_index.document_id=document.id
INNER JOIN dictionary on dictionary.id=document_index.word_id
WHERE dictionary.word LIKE @pQuery
AND document.active = 1
AND document.root_id in (@pRoot1, @pRoot2, @pRoot3, @pRoot4, @pRoot5, @pRoot6, @pRoot7)
如果詞典包含〜= 400,000條記錄,文檔〜= 1000條記錄和document_index〜= 500,000條記錄,查詢在我的iPad 2上執行約30秒鐘。
如何優化查詢或更改數據庫的結構(例如添加索引)來減少查詢時間?
那麼和docuent.root_id上的索引直接跳出來,我懷疑像是你真正的問題,但。什麼是作爲參數傳遞(主要) –