2012-03-06 108 views
0

表结构优化SQL查询

对于两个表中,创建查询在下面给出:

表1:(file_path_key,dir_path_key)

创建表table1 (file_path_key varchar(500),dir_path_key varchar(500),主键(file_path_key))engine = innodb;

实施例,file_path_key = /home/playstation/a.txt
dir_path_key = /家庭/游戏机/

表2:(file_path_key,HASH_KEY)

创建表表2(file_path_key varchar(500)非空,hash_key bigint(20)非空,外键(file_path_key)引用 表1(file_path_key)on upd吃级联删除级联) engine = innodb;

目的

Given a hash value *H* and a directory string *D*, I need to find all those 
hashes which equal to *H* from Table2, such that, the corresponding file entry 
doesn't have *D* as it's directory. 

在这种特殊情况下,表1有大约40,000项和表2拥有500万个词条,这让我当前的查询很慢。

从表1中选择不同s1.file_path_key为S1加入(SELECT * FROM表2其中HASH_KEY = H)为S2上s1.file_path_key = s2.file_path_key和s1.dir_path_key = d!;

+0

你的关键的(潜在的)尺寸肯定没有帮助。它看起来并不像你需要潜在的关键范围 - 你会考虑切换到你加入的自动主键吗?这应该会大大减少你的表的大小 - 首先,这意味着'file_path_key'可以变成'file'(这可能会减少不匹配)。太糟糕了,你不使用支持递归CTE的RDBMS--它们完美适用于文件夹结构。 – 2012-03-06 17:39:25

回答

1

子选择是真的不必要地减慢查询速度。

您应该删除它,并用简单的连接替换它,将所有与非连接有关的条件移动到WHERE子句中。

你也应该在Table1.dir_path_key和Table2.hash_key列添加索引:

ALTER TABLE Table1 
    ADD INDEX dir_path_key dir_path_key(255); 

ALTER TABLE Table2 
    ADD INDEX hash_key (hash_key); 

尝试像这样的查询:

select distinct s1.file_path_key 
from Table1 as s1 
join Table2 as s2 on s1.file_path_key = s2.file_path_key 
where s1.dir_path_key !=D 
and s2.hash_key =H; 
+0

当然,我会试试这个。你如何着手为列添加索引? – Gooner 2012-03-06 17:33:16

+0

我添加了用于创建索引的示例DDL。当心这将锁定表几分钟,所以你不应该这样做在现场生产数据库。 – 2012-03-06 17:40:50

+0

那么,表格一旦填入我的用例就不会更新。那应该不是问题? – Gooner 2012-03-06 17:47:08

1

我建议选择从表2的条目到一个临时表第一:

SELECT * FROM Table2 INTO #Temp WHERE hash_key = H 

然后加入临时表中的SELECT语句:

select distinct s1.file_path_key from Table1 as s1 join #Temp as s2 on s1.file_path_key = s2.file_path_key and s1.dir_path_key !=D; 
+0

这对查询执行时间有什么影响吗? – Gooner 2012-03-06 17:24:13

+0

当我在过去将这一点付诸实践时,我通常会注意到一个公平的差异。 – aaroncatlin 2012-03-06 17:26:48

+0

当然,我会试试这个。 – Gooner 2012-03-06 17:48:47