MySQL查询性能提升

时间:2017-05-05 11:32:17

标签: mysql

我的MySQL性能有问题。我怎样才能改善它? 情况如下:

  • 表“backlogsap”有大约4百万个条目。
  • 创建索引
  • 此表有FK,其他表格有FK到此表=>不能 创建分区。
  • 此查询需要大约140秒才能完成:

    select 
        idmaterial,
        materialgroup, 
        materialgroupcategory,
        name,
        dispatchgroup,
        idsupplier, 
        group_concat(distinct sellingorganizationname) as sellingorganizationnames,
        group_concat(distinct idordertype) as idordertypes,
        group_concat(distinct idpositiontype) as idpositiontypes,
        sum(newOrUpdated and isCritical) as classA,
        sum(newOrUpdated and not isCritical) as classB,
        sum(processingstate <3) as classC,
    
        (select count(innerBacklogsAp.idmaterial) 
            from backlogsap as innerBacklogsAp
            where innerBacklogsAp.idmaterial = src.idmaterial and IsDeleted = 0) as countReplacementVehiclerRequests
    
    from 
        (select 
            backlogsap.idmaterial as idmaterial,
            backlog.processingstate as processingstate,
            material.idsupplier as idsupplier,
            backlogsap.sellingorganizationname as sellingorganizationname,
            backlogsap.idpositiontype as idpositiontype,
            backlogsap.idordertype as idordertype,
            materialindistributioncenter.dispatchgroup as dispatchgroup,
            material.name as name,
            material.idmaterialgroup as materialgroup,
            materialgroup.idmaterialgroupcategory as materialgroupcategory,
            (processingstate = 0 or processingstate = 1) as newOrUpdated,
            ((cancellation.state is not null and cancellation.state = 0 ) or
                 (reminderrequest.state is not null and (reminderrequest.state = 2 or reminderrequest.state = 0))
                ) as isCritical
        from backlogsap 
        join backlog using (idbacklogsap)
        left join cancellation using (idcancellation)
        left join reminderrequest on backlog.IdReminderRequest = reminderrequest.idreminder
        left join material using (idmaterial)
        left join materialindistributioncenter using (idmaterial, iddistributioncenter)
        left join materialgroup using (idmaterialgroup)
    
         where (idcancellation is null or cancellation.State not in (1)) and
             backlogsap.isdeleted = 0 and
             backlogsap.idordertype not in ('ZAP', 'ZAK', 'ZAKO', 'ZAKZ', 'ZAPM') and
             iddistributioncenter = 1469990
        ) as src
    group by idmaterial
    order by classA desc, classB desc, classC, idmaterial desc
    
  • 解释

    id  select_type         table                           type            possible_keys                                           key                                                                 key_len     ref     rows    Extra
    1   PRIMARY             <derived3>                      ALL                                                                                                                                                             26960   Using temporary; Using filesort
    3   DERIVED             backlogsap                      index_merge     PRIMARY,fk_BacklogSap_OrderType1_idx,
                                                                            fk_BacklogSap_MaterialInDistributionCenter1_idx,
                                                                            perform_backlogsap_isdeleted,
                                                                            fk_BacklogSap_DistributionCenter_idx                    perform_backlogsap_isdeleted,fk_BacklogSap_DistributionCenter_idx   1,4                 35946   Using intersect(perform_backlogsap_isdeleted,fk_BacklogSap_DistributionCenter_idx); Using where
    3   DERIVED             backlog                         eq_ref          idBacklogSAP_UNIQUE,
                                                                            fk_Backlog_BacklogSap1_idx,
                                                                            fk_Backlog_Cancellation1_idx                            idBacklogSAP_UNIQUE                                                 4           ...backlogsap.IdBacklogSap  1   
    3   DERIVED             cancellation                    eq_ref          PRIMARY                                                 PRIMARY                                                             4           ...backlog.IdCancellation   1   Using where
    3   DERIVED             reminderrequest                 eq_ref          PRIMARY                                                 PRIMARY                                                             4           ...backlog.IdReminderRequest    1   
    3   DERIVED             material                        eq_ref          PRIMARY                                                 PRIMARY                                                             45          ...backlogsap.IdMaterial    1   
    3   DERIVED             materialindistributioncenter    eq_ref          PRIMARY,
                                                                            unqiue_IdDistributionCenter_IdMaterial,
                                                                            fk_MaterialDistributionCenter_DistributionCenter1_idx,
                                                                            fk_MaterialDistributionCenter_Material1_idx             PRIMARY                                                             49          const,...backlogsap.IdMaterial  1   
    3   DERIVED             materialgroup                   eq_ref          PRIMARY                                                 PRIMARY                                                             137         ....material.IdMaterialGroup    1   
    2   DEPENDENT SUBQUERY  innerBacklogsAp                 ref             perform_backlogsap_isdeleted,
                                                                            idx_backlogsap_IdMaterial                               idx_backlogsap_IdMaterial                                           45          func    8   Using where
    

1 个答案:

答案 0 :(得分:0)

解决:创建组合索引(idmaterial,IsDeleted)