MySQL 慢查询和 EXPLAIN 给出了奇怪的答案

标签 mysql optimization

我要连接两个表。 表 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/

相关文章:

php - 检查 MySQL 上是否存在表

javascript - 下拉列表中的值

mysql - JOIN 表可能没有记录的复杂 JOIN 查询

c++ - Z3:C++ 优化超时

c++ - 如何从运行时值转换为模板参数?

mysql - InnoDB : Cannot open table from the internal data dictionary of InnoDB though the . 表的 frm 文件存在

带有偏差的MySQL随机记录

c++ - 虚函数和性能 - C++

optimization - 关于 g++ -O 选项

r - 在列表中获取匹配索引的快速方法