2017-10-05 103 views
0

我有這樣的查詢查詢沒有where子句 - 性能調優 - mysql

需要40分鐘才能執行。我無法添加任何where子句,因爲我需要所有的數據

表的配置如下,下面是解釋計劃的快照。

我知道它是一個不合理的查詢,因爲它訪問每個數據。但是,有什麼方法可以改善計時嗎?

select 
    pl.CompanyId, 
    pl.FarmerCropId, 
    ai.ActivityId, 
    ai.ActivityName, 
    ai.ActivityPlanId, 
    ai.ActivityPlanName, 
    ai.ActivityScheduleId, 
    ai.DAS, 
    (case when fca.CompletionDate = '1900-01-01' then null else fca.CompletionDate end)CompletionDate, 
    fca.IsClosed, 
    fca.LastModifiedDate, 
    ai.AttributeDataTypeId, 
    ai.AttributeId, 
    ai.AttributeName, 
    fcaa.FarmerCropActivityAttributeId, 
    fcaa.Reading, 
    ai.DataTypeId, 
    ai.DataTypeName, 
    ai.SequenaceNo, 
    fca.isactive, 
    pl.partitionflag, 
    fca.ExpectedStartDate, 
    fca.ExpectedClosureDate, 
    ai.ActivityMasterIsActive, 
    ai.ActivityPlanIsActive, 
    ai.ActivityScheduleIsActive, 
    ai.AttributeDataTypeIsActive, 
    ai.AttributeMasterIsActive, 
    case when isclosed = '1' then 'Closed On Time' 
       when isclosed = '2' then 'In Progress' 
       when isclosed = '3' then 'Pending' 
       when isclosed ='5' then 'Late Closed' 
       when isclosed ='4' then 'Not Started' 
       end as ClosureStatus , 
       fca.FarmerCropActivityID 
from 
activityinformation ai 
join FarmerCropActivity fca 
    on ai.activityscheduleid = fca.activityscheduleid 
    and fca.isactive = 1 
left join FarmerCropActivityAttributes fcaa 
    on fcaa.farmercropactivityid = fca.farmercropactivityid 
    and fcaa.AttributeDataTypeID = ai.AttributeDataTypeID 
join plot pl on 
pl.farmercropid = fca.farmercropid 
where pl.partitionflag = 1; 

activityinformation

1.Total行 - 137653

2.Indexes -

idx_activityinformation_combinedids(ActivityMasterIsActive, ActivityPlanIsActive , ActivityScheduleIsActive,AttributeDataTypeIsActive 
,AttributeMasterIsActive) 

3.Columns -

source 
      ActivityId 
      ActivityName 
      ActivityPlanId 
      ActivityPlanName 
      CropTypeId 
      ActivityScheduleId 
      ActivityintervalDays 
      AttributeDataTypeId 
      SequenaceNo 
      AttributeId 
      AttributeName 
      DAS 
      DataTypeID 
      DataTypeName 
      talendate 
      companyid 
      ActivityMasterIsActive 
      ActivityPlanIsActive 
      ActivityScheduleIsActive 
      AttributeDataTypeIsActive 
      AttributeMasterIsActive 
      Min 
      Max 

farmercropactivity

1.Total行 - 2721502

指標 -

idx_fca_activityscheduleid(activityscheduleid), 

idx_fca_activityid(ActivityID), 

idx_fca_farmercropid(FarmerCropId), 

idx_fca_composite(ActivityScheduleID , IsActive , ActivityID , FarmerCropId , FarmerCropActivityID), 

idx_fca_composite1(ActivityScheduleID , IsActive , FarmerCropId , FarmerCropActivityID , ActivityID , CompletionDate , IsClosed , 
LastModifiedDate , ExpectedStartDate , ExpectedClosureDate) 

3.Columns -

FarmerCropActivityID 
      FarmerCropId 
      ActivityScheduleID 
      CompletionDate 
      ExpectedStartDate 
      ExpectedClosureDate 
      Suggestions 
      Comments 
      IsClosed 
      IsActive 
      LastModifiedDate 
      LastModifiedBy 
      ActivityID 
      ActivityReading 
      IsMandatory 
      ActivityCategoryID 
      ClientId 

farmercropactivityattributes

1.Total行 - 4993953

  • 索引 -

    idx_fcaa_farmercropactivityid(FarmerCropActivityID),idx_fcaa_AttributeDataTypeID(AttributeDataTypeID)

  • 列 -

    FarmerCropActivityAttributeID FarmerCropActivityID AttributeDataTypeID Reading ClientId

  • 情節

    1.Total行 - 其中partitionflag = 1 --- 328838個

    2.indexes -

    idx_composite2(companyid , partitionflag , FarmerCropId) 
    

    3。列 -

    source 
          companyid 
          Company 
          FarmerId 
          FarmerCode 
          FarmerName 
          LandId 
          PlotName 
          Latitude 
          Longitude 
          FarmerCropId 
          Village 
          DistrictId 
          CropId 
          crop 
          VarietyId 
          Variety 
          SowingDate 
          SowingWeek 
          SowingWeekRange 
          SowingMonth 
          SowingMonthName 
          SowingYear 
          DeclaredArea 
          AuditedArea 
          SowingArea 
          IsAudited 
          IsGeoTagged 
          ExpectedHarvestPerAcre 
          FirstExpectedHarvestDate 
          FirstActualHarvestDate 
          HarvestArrivalStatus 
          PlannedQuantity 
          ExpectedQuantity 
          CurrentQuantity 
          HarvestQuantity 
          RegistrationDate 
          RegistrationWeek 
          RegistrationWeekRange 
          RegistrationMonth 
          RegistrationMonthName 
          RegistrationYear 
          ActivityPlanId 
          ActivityPlanName 
          HarvestPlanId 
          HarvestPlanName 
          LastModifiedDate 
          Address 
          Territory 
          Taluka 
          ReestimateDate 
          NewHarvestDate 
          NewHarvestQuantity 
          ReestimateReasonCode 
          ReestimateReason 
          Comments 
          FarmerCropHarvestreestimateId 
          IsReestimated 
          ReestimateDaysShift 
          CurrentQuantityWithoutReestimate 
          HarvestUnitId 
          harvestunit 
          ExtendedHarvestUnitId 
          ExtendedHarvestUnit 
          ConversionFactor 
          MobileNumber 
          Coordinates 
          PreferredSKUTypeId 
          PreferredSKUName 
          Capacity 
          Soiltypeid 
          soildesc 
          Irrigationtypeid 
          irrigationtypedesc 
          lastmodifiedon 
          partitionflag 
          areaunitid 
          areaunitname 
          archivedate 
          talendate 
          geoid 
          idtoroot 
          nametoroot 
          RevisedFirstExpectedHarvestDate 
          HarvestStatusId 
          HarvestStatus 
    

    enter image description here

    感謝 瑞斯

    +1

    您可以發送較少的數據。就是這樣。你正在發送約136k行。它需要一段時間才能發送,而另一方則需要接受/解析它。你所要求的是優化你找到那些136k的部分 - 但這只是你的性能問題的1/3,正如你所看到的那樣 - 找到要加入的行的速度非常快。 – Mjh

    +0

    您的查詢是什麼? –

    +0

    道歉!已經錯過了查詢,現在已經包含在主要問題 –

    回答

    0

    需要:

    fcaa: INDEX(FarmerCropActivityID, AttributeDataTypeID, Reading) 
    

    Reading必須是最後一個,另外兩個的順序是可選的。)這是優於你有兩個單列索引,它是「覆蓋」。

    plot: INDEX(partitionflag , FarmerCropId, companyid) 
    

    companyid必須最後,不是第一。再次,這比你有更好的,它是'覆蓋'。添加此索引。並刪除現有的 - 但僅限於其他查詢不需要的情況。