我要连接两个表。 表 unique_nucleosome_re 有大约 600,000 条记录。 另一个表有 20,000 条记录。 奇怪的是性能和 EXPLAIN 的答案是不同的 在 WHERE 子句中的条件。 当它是 WHERE n.chromosome = 'X' 大约用了 3 分钟。 当它是 WHERE n.chromosome = '2L' 花了 30 多分钟,连接断开了。
SELECT n.name , t.transcript_start , n.start
FROM unique_nucleosome_re AS n
INNER JOIN tss_tata_range AS t
ON t.chromosome = n.chromosome
WHERE (t.transcript_start > n.end AND t.ts_minus_250 < n.start )
AND n.chromosome = 'X'
ORDER BY t.transcript_start
;
这是来自 EXPLAIN 的答案。 当 WHERE 为 n.chromosome = 'X'
'1', 'SIMPLE', 'n', 'ALL', 'start_idx,end_idx,chromo_start', NULL, NULL, NULL, '606096', '48.42', 'Using where; Using join buffer'
当 WHERE 为 n.chromosome = '2L'
'1', 'SIMPLE', 'n', 'ref', 'start_idx,end_idx,chromo_start', 'chromo_start', '17', 'const', '68109', '100.00', 'Using where'
X 或 2L 的记录数几乎相同。 我花了最后几天,但我无法弄清楚。这可能是一个我看不到的简单错误,也可能是一个错误。 你能帮帮我吗?
最佳答案
首先,在没有看到任何索引信息的情况下,我会在您的 TSS_TData_Range 上的 Chromosome 键和 transcript_start(但至少是染色体键)上有一个索引。我还假设您的 unique_nucleosome_re 表上的染色体上有一个索引。然后,TSS 似乎是您的 SHORT 表,因此我会将其移至查询的第一个位置并调用“STRAIGHT_JOIN”子句的使用...
SELECT STRAIGHT_JOIN
n.name,
t.transcript_start,
n.start
FROM
tss_tdata_range t,
unique_nucleosome_re n
where
t.chromosome = 'X'
and t.chromosome = n.chromosome
and t.transcript_start > n.end
and t.ts_minus_250 < n.start
order by
t.transcript_start
如果它适合你,我也会对性能感兴趣......
关于MySQL 慢查询和 EXPLAIN 给出了奇怪的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4661102/