【发布时间】:2012-03-24 05:21:07
【问题描述】:
表架构
对于这两个表,CREATE 查询如下:
表 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 = /home/playstation/
Table2:(file_path_key,hash_key)
创建表 Table2(file_path_key varchar(500) not null, hash_key bigint(20) 不为空,外键 (file_path_key) 引用 Table1(file_path_key) on update cascade on delete cascade) 引擎 = 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 个条目,这使得我当前的查询非常慢。
select distinct s1.file_path_key from Table1 as s1 join (select * from Table2 where hash_key = H) as s2 on s1.file_path_key = s2.file_path_key and s1.dir_path_key !=D;
【问题讨论】:
-
密钥的(潜在)大小肯定没有帮助。看起来您不需要潜在的键范围 - 您会考虑切换到您加入的自动生成主键吗?这应该会大大减少你的表的大小——一方面,这意味着
file_path_key可以变成file(这可能会减少不匹配)。太糟糕了,您没有使用支持递归 CTE 的 RDBMS - 它们非常适合文件夹结构。
标签: mysql sql query-optimization