テーブルスキーマ
2つのテーブルについて、CREATEクエリを以下に示します。
表1: (file_path_key、dir_path_key)
create table Table1(
file_path_key varchar(500),
dir_path_key varchar(500),
primary key(file_path_key))
engine = innodb;
表2 :(file_path_key、hash_key)
create table Table2(
file_path_key varchar(500) not null,
hash_key bigint(20) not null,
foreign key (file_path_key) references Table1(file_path_key) on update cascade on delete cascade)
engine = innodb;
目的:
file_path Fとそのdir_path文字列Dが与えられた場合、Fのハッシュのセットに少なくとも1つのハッシュがあるが、ディレクトリ名がDではないすべてのファイル名を見つける必要があります。ファイルF1がFと複数のハッシュを共有している場合は、それを何度も繰り返す必要があります。
表1のfile_path_key列と表2のhash_key列にインデックスが付けられていることに注意してください。
この特定のケースでは、Table1には約350,000エントリがあり、Table2には31,167,119エントリがあるため、現在のクエリは遅くなります。
create table temp
as select hash_key from Table2
where file_path_key = F;
select s1.file_path_key
from Table1 as s1
join Table2 as s2
on s1.file_path_key join
temp on temp.hash_key = s2.hash_key
where s1.dir_path_key != D
このクエリを高速化するにはどうすればよいですか?