表架构
对于这两个表,CREATE查询如下:
表1: (file_path_key,dir_path_key)
create table 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 = / home / playstation /
表2: (file_path_key,hash_key)
create table Table2(file_path_key varchar(500)not null,hash_key bigint(20)not null,外键(file_path_key)引用 删除级联上的更新级联上的表1(file_path_key) 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.
在这种特殊情况下,Table1有大约40,000个条目,Table2有5,000,000个条目,这使我当前的查询非常慢。
从表1中选择不同的s1.file_path_key作为s1 join(从表2中选择*,其中hash_key = H)为s1.file_path_key = s2.file_path_key上的s2和s1.dir_path_key!= D;
答案 0 :(得分:1)
我建议先将Table2中的条目选入临时表:
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;
答案 1 :(得分: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;