2013-04-08 25 views
0

我有兩個表:maskedindextable和dictionarytable。如何優化以下查詢?

的dictionarytable有四列:

ID (primary key) 
filelocation 
dictionaryEncryptedIndex 
dictionaryEncryptedWord 

和價值觀是這樣的:

ID | filelocation | dictionaryencryptedindex | dictionaryEncryptedWord 
0 | c:/test.txt | 0x01 | EAD64zef6z 
1 | c:/test.txt | 0x02 | 5ez4f 
2 | c:/test.txt | 0x03 | ze654f 
3 | c:/john.txt | 0x01 | 6ze5f4 
4 | c:/john.txt | 0x02 | ze5f68z4f 
5 | c:/john.txt | 0x03 | EAD64zef6z 
6 | c:/smith.txt | 0x01 | er6g4 
7 | c:/smith.txt | 0x02 | z6e58g4 
8 | c:/smith.txt | 0x03 | a64zef6z 
9 | c:/laura.txt | 0x01 | EAD64zef6z 

這個表有700個000值

的maskedindextable有四列:

ID (primary key) 
filelocation 
maskedIndexEncryptedIndex 
maskedIndexEncryptedValue 

個值是這樣的:

ID | filelocation | maskedIndexEncryptedIndex | maskedIndexEncryptedValue 
0 | c:/test.txt | 0x01 | 5z1ef 
1 | c:/test.txt | 0x02 | e6rh546er4g 
2 | c:/test.txt | 0x03 | z6ef548ezf 
3 | c:/john.txt | 0x01 | (y48try68 
4 | c:/john.txt | 0x02 | iu47k 
5 | c:/john.txt | 0x03 | 6tkj 
6 | c:/smith.txt | 0x01 | 6ez5r48 
7 | c:/smith.txt | 0x02 | 6i5 
8 | c:/smith.txt | 0x03 | 6e5rg 
9 | c:/laura.txt | 0x01 | ze654f 

此表還具有700個000值

表dictionarytable的dictionaryencryptedindex & filelocation結合表maskedindextable的maskedIndexEncryptedIndex & filelocation對應。 我想找到相應的maskedindexencryptedvalue,加密的單詞是EAD64zef6z。目前我使用以下查詢:

SELECT DISTINCT MaskedIndexTable.filelocation, 
     dictionaryencryptedindex, 
     maskedindexencryptedvalue 
FROM MaskedIndexTable 
INNER JOIN DictionaryTable 
     ON MaskedIndexTable.maskedIndexEncryptedIndex = 
      DictionaryTable.dictionaryEncryptedIndex 
WHERE MaskedIndexTable.Id IN 
(SELECT DictionaryTable.Id 
FROM DictionaryTable 
WHERE `dictionaryEncryptedWord` LIKE 'EAD64zef6z') 
AND `dictionaryEncryptedWord` LIKE 'EAD64zef6z'; 

但是,此查詢運行速度非常慢(需要3秒鐘)。有誰知道我可以如何優化我的查詢?

+1

索引,根據Marjeta的回答可能是最好的選擇。另外,在每一個有點幫助的部門中,你都使用LIKE而沒有通配符。使用=可能會使你獲得納秒或者兩個納秒。 – 2013-04-08 16:45:44

+0

大多數數據庫引擎提供了一種檢查查詢計劃的方法,以確定查詢的哪個部分導致性能問題(例如缺少索引)。雖然在這種情況下,我懷疑這是你的子選擇,而不是缺失的索引。 – RickF 2013-04-08 17:02:56

回答

2

如何簡單的聲明

SELECT dt.filelocation, dictionaryencryptedindex, maskedindexencryptedvalue 
FROM MaskedIndexTable mit 
INNER JOIN DictionaryTable dt 
    ON mit.maskedIndexEncryptedIndex = dt.dictionaryEncryptedIndex 
    AND mit.filelocation = dt.filelocation 
WHERE dt.dictionaryEncryptedWord = 'EAD64zef6z'; 

你並不需要,如果你不使用通配符LIKE。您也可以在JOIN聲明的ON條款中添加多個條件。

您需要在WHERE索引和連接列,即DictionaryTable.dictionaryEncryptedWordDictionaryTable.filelocationDictionaryTable.dictionaryEncryptedIndexMaskedIndexTable.filelocationMaskedIndexTable.maskedIndexEncryptedIndex

+0

非常感謝您的幫助,這是完美的作品,是一個巨大的進步。 – user2241821 2013-04-08 17:06:45

1

根據我的經驗,只要查詢速度慢,就會發現WHERE子句中提到的列沒有索引。

嘗試索引出現在WHERE子句中的所有列。

+0

如果您需要幫助創建INDEXES,您可以從這裏開始:http://www.w3schools.com/sql/sql_create_index。asp – 2013-04-08 16:40:21

0

儘量去除選擇在clausure

SELECT DISTINCT MaskedIndexTable.filelocation, dictionaryencryptedindex 
,  maskedindexencryptedvalue 

FROM MaskedIndexTable INNER JOIN DictionaryTable ON MaskedIndexTable.maskedIndexEncryptedIndex = DictionaryTable.dictionaryEncryptedIndex 
WHERE `dictionaryEncryptedWord` LIKE 'EAD64zef6z'; 
1

一目瞭然,子選擇在WHERE子句似乎起不到任何作用。您也正在使用LIKE而不是簡單的=。試試這個:

SELECT DISTINCT MIT.filelocation, 
    dictionaryencryptedindex, 
    maskedindexencryptedvalue 
FROM MaskedIndexTable MIT 
    INNER JOIN DictionaryTable DT 
    ON MIT.maskedIndexEncryptedIndex = 
     DT.dictionaryEncryptedIndex 
WHERE dictionaryEncryptedWord = 'EAD64zef6z';