我有两个表(它们更大,有200,000行,这只是一个例子)

Table1:temptable
id         |Chromosome | Start | End | Reference | Alternative | QUAL | MQ
........................................................................
NULL         chr1       12334  12335    A          TT            10   20
NULL         chr1       12334  12335    C          TT            5    3
NULL         chr2       123    123      A           T            1    2
NULL         chr2       34     34       A           T            11   60
NULL         chr3       12     12       A           T            6    NULL


Table2:TableVariants2
id          | Chromosome | Start | End | Reference | Alternative
.............................................................
1            chr1          12334  12335   A        TT
2            chr1          12334  12335   C        TT
3            chr2          123    123     A         T
4            chr2          34     34      A         T
5            chr3          12     12      A         T


我想基于相等的染色体,开始,结束,参考和替代合并它们,并获得:

id         |Chromosome | Start | End | Reference | Alternative | QUAL | MQ
........................................................................
1           chr1       12334  12335     A             TT         10     20
2           chr1       12334  12335     C             TT         5      3
3           chr2       123    123       A              T         1      2
4           chr2       34     34        A              T         11     60
5           chr3       12     12        A              T         6     NULL


我已经尝试了以下方法,但是对于大表来说它太慢了,我需要更快的东西

SELECT B.Maxid, A.Chromosome, A.Start, A.End, A.Reference, A.Alternative , A.QUAL,A.MQ
FROM temptable A
INNER JOIN ( SELECT `Chromosome`, `Start`, `End`, `Reference`,`Alternative`, MAX(id) AS Maxid FROM TableVariants2 GROUP BY `Chromosome`, `Start`, `End`, `Reference`,`Alternative`) B
ON A.Chromosome=B.Chromosome AND A.Start=B.Start AND A.End=B.End AND A.Reference=B.Reference AND A.Alternative=B.Alternative



除了INNER JOIN以外,还有其他选择可以加快查询速度吗?

最佳答案

您可以在编写查询时遵循以下几点:

1. Analyze your data.
2. Make sure you have created necessary indexes.
3. In Select statement write only required column, avoid unnecessary columns.
4. Do not write subquery or co-related query until and unless is required.


让我们看看您是否可以按照以下步骤进行操作。
但是内部联接是基本联接,如果必须联接两个表,则无法替换。

关于mysql - MYSQL:使用BIG TABLES替代INNER JOIN(太慢了),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54922052/

10-09 00:58