首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何优化以下查询?

如何优化以下查询?
EN

Stack Overflow用户
提问于 2013-04-09 00:34:13
回答 4查看 90关注 0票数 0

我有两个表: maskedindextable和dictionarytable。

字典表有四列:

代码语言:javascript
复制
ID (primary key)
filelocation
dictionaryEncryptedIndex
dictionaryEncryptedWord

值如下所示:

代码语言:javascript
复制
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

该表有70万个值

maskedindextable有四列:

代码语言:javascript
复制
ID (primary key)
filelocation
maskedIndexEncryptedIndex
maskedIndexEncryptedValue

值如下所示:

代码语言:javascript
复制
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

此表还包含70万个值

表dictionarytable中的dictionaryencryptedindex和filelocation组合对应于表掩码in中的maskedIndexEncryptedIndex和filelocation。我想找到对应的maskedindexEncrypted值,它的加密字是EAD64zef6z。目前,我使用的是以下查询:

代码语言:javascript
复制
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秒)。有人知道如何优化我的查询吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-04-09 00:47:41

更简单的语句如何?

代码语言:javascript
复制
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和JOIN列上的索引,即DictionaryTable.dictionaryEncryptedWordDictionaryTable.filelocationDictionaryTable.dictionaryEncryptedIndexMaskedIndexTable.filelocationMaskedIndexTable.maskedIndexEncryptedIndex

票数 2
EN

Stack Overflow用户

发布于 2013-04-09 00:38:22

根据我的经验,每当查询速度较慢时,WHERE子句中提到的列都没有索引。

尝试索引WHERE子句中出现的所有列。

票数 1
EN

Stack Overflow用户

发布于 2013-04-09 00:49:05

乍一看,WHERE子句中的子select似乎没有任何作用。您还使用了LIKE而不是简单的=。试试这个:

代码语言:javascript
复制
SELECT DISTINCT MIT.filelocation, 
   dictionaryencryptedindex,
   maskedindexencryptedvalue
FROM MaskedIndexTable MIT
    INNER JOIN DictionaryTable DT
    ON MIT.maskedIndexEncryptedIndex = 
       DT.dictionaryEncryptedIndex  
WHERE dictionaryEncryptedWord = 'EAD64zef6z';
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15884383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档