导入过程后奇怪的MYSQL查询问题

Posted

技术标签:

【中文标题】导入过程后奇怪的MYSQL查询问题【英文标题】:Strange MYSQL query issue after import process 【发布时间】:2021-01-02 00:43:07 【问题描述】:

我有一个我认为是完美的数据库副本。不幸的是,一个特定的查询现在非常慢。这些是 mysql Workbench 生成的查询分析图像。您可以看到在底部(较慢)连接顺序不同,这似乎是瓶颈。我不知道什么会导致加入顺序发生变化。有人有什么想法吗?

查询:

SELECT 
    mutation.ENTREZ_GENE_ID AS entrezGeneId,
    gene.HUGO_GENE_SYMBOL AS hugoGeneSymbol,
    COUNT(*) AS totalCount,
    COUNT(DISTINCT (mutation.SAMPLE_ID)) AS numberOfAlteredCases
FROM
    mutation
        INNER JOIN
    mutation_event ON mutation_event.MUTATION_EVENT_ID = mutation.MUTATION_EVENT_ID
        AND mutation_event.MUTATION_TYPE != 'Fusion'
        INNER JOIN
    genetic_profile ON mutation.GENETIC_PROFILE_ID = genetic_profile.GENETIC_PROFILE_ID
        INNER JOIN
    sample ON mutation.SAMPLE_ID = sample.INTERNAL_ID
        INNER JOIN
    gene ON mutation.ENTREZ_GENE_ID = gene.ENTREZ_GENE_ID
WHERE
    mutation.SAMPLE_ID IN (SELECT 
            sample.INTERNAL_ID
        FROM
            sample
                INNER JOIN
            patient ON sample.PATIENT_ID = patient.INTERNAL_ID
                INNER JOIN
            genetic_profile ON patient.CANCER_STUDY_ID = genetic_profile.CANCER_STUDY_ID
        WHERE
            genetic_profile.STABLE_ID = 'ccle_broad_2019_mutations'
                AND sample.STABLE_ID IN (...)
                AND sample.INTERNAL_ID = mutation.SAMPLE_ID
                AND genetic_profile.GENETIC_PROFILE_ID = mutation.GENETIC_PROFILE_ID)
GROUP BY mutation.ENTREZ_GENE_ID

用非常截断的样本列表解释慢速数据库:

+=====================================================================+======================================================================+=======+
|                   Query execution plan on DB2  (Slow)               |                   Query execution plan on DB1                        | Same? |
+=====================================================================+======================================================================+=======+
| id : 1,                                                             | id : 1,                                                              | Yes   |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| select_type : SIMPLE,                                               | select_type : SIMPLE,                                                | Yes   |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| table : mutation,                                                   | table : mutation,                                                    | Yes   |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| partitions : null,                                                  | partitions : null,                                                   | Yes   |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| type : ref,                                                         | type : ref,                                                          | Yes   |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| possible_keys : UQ_MUTATION_EVENT_ID_GENETIC_PROFILE_ID_SAMPLE_ID,  | possible_keys : UQ_MUTATION_EVENT_ID_GENETIC_PROFILE_ID_SAMPLE_ID,   |       |
|       GENETIC_PROFILE_ID,GENETIC_PROFILE_ID_2,GENETIC_PROFILE_ID_3, |        GENETIC_PROFILE_ID,GENETIC_PROFILE_ID_2,GENETIC_PROFILE_ID_3, |       |
|       ENTREZ_GENE_ID,SAMPLE_ID,MUTATION_EVENT_ID,                   |        ENTREZ_GENE_ID,SAMPLE_ID,MUTATION_EVENT_ID,                   | Yes   |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| key : GENETIC_PROFILE_ID,                                           | key : GENETIC_PROFILE_ID_2,                                          | No    |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| key_len : 4,                                                        | key_len : 8,                                                         | No    |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| ref : const,                                                        | ref : const,cgds_public.sample.INTERNAL_ID,                          | No    |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| rows : 792,                                                         | rows : 68,                                                           | No    |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| filtered : 0.01,                                                    | filtered : 100.00,                                                   | No    |
+---------------------------------------------------------------------+----------------------------------------------------------------------+-------+
| Extra : Using where; Using index                                    | Extra : null                                                         | No    |
+--------------------------------------------------------------------------------------------------------------------------------------------+-------+

【问题讨论】:

Please edit your question 并包含您正在使用的查询以及 SHOW CREATE TABLE tablenameEXPLAIN your query(对于两个 db.tables)结果。 @tcadidot0 感谢您的回复。我刚刚发布了查询。对于 SHOW CREATE TABLE 和 EXPLAIN,所有表都需要这些吗? 也许您可以在上面发布的查询上运行EXPLAIN。只需将EXPLAIN 放在查询顶部(SELECT 之前)并运行它。 “我有一个我认为是完美的数据库副本” 你是如何复制数据库的?请显示您使用的确切 SQL 命令。 @tcadidot0 我为两个数据库添加了 out of explain(相同的查询)。请注意,我将示例列表减少了两个。 【参考方案1】:

当您插入/删除/更新表的大量数据时,您应该重新分析您的表:

如果您的数据分布不均匀,请使用直方图。

https://dev.mysql.com/doc/refman/8.0/en/analyze-table.html

【讨论】:

以上是关于导入过程后奇怪的MYSQL查询问题的主要内容,如果未能解决你的问题,请参考以下文章

MySql Bit字段查询的奇怪问题

centos下升级mysql后遇到的小问题

MySQL表分区奇怪行为(突然查询慢)

为 .NET 数据访问层使用 MySql 存储过程

Mysql 查询与 order by。奇怪的执行计划

MySQL查询过程