2017-05-05 149 views
0

我有一個MySQL性能問題。我該如何改進它? 情況如下:MySQL查詢性能改進

  • 表「backlogsap」有大約4 mio條目。
  • 創建索引
  • 此表具有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 
    

回答

0

解決:創建聯合索引(idmaterial,請將isDeleted)