如何在mysql中优化以下查询?

时间:2018-06-05 14:13:02

标签: mysql

我有这个复杂的mysql查询,在几千条记录的数据集上,查询性能大大降低。 *_shifted_AllocationDate列是视图的计算列(vw_transaction_details)。 我怎样才能提高它的性能?

SELECT
    PointsAllocationId,
    (
        select 
            count(1) 
        from vw_transaction_details v 
        where v.DealerId = p.DealerId AND v.PointsAllocationId <= p.PointsAllocationId
    ) `TransactionNumberOfMember`,
    (
        select 
            count(1) 
        from vw_transaction_details v 
        where v.DealerId = p.DealerId 
          AND DATE(v.hour_min_shifted_AllocationDate) = DATE(p.hour_min_shifted_AllocationDate)
          AND v.PointsAllocationId <= p.PointsAllocationId
    ) `TransactionNumberOfDayForMember`,
    (
        select 
            count(1) 
        from vw_transaction_details v 
        where v.DealerId = p.DealerId 
          AND DATE(v.AllocationDate) between DATE(TIMESTAMPADD(Day, -weekday(p.AllocationDate),p.AllocationDate)) and DATE(TIMESTAMPADD(Day, 6-weekday(p.AllocationDate),p.AllocationDate)) 
          AND v.PointsAllocationId <= p.PointsAllocationId
    ) `TransactionNumberOfWeekForMember`,
    (
        select 
            count(1)
        from vw_transaction_details v 
        where v.DealerId = p.DealerId 
          AND Month(v.day_hour_min_shifted_AllocationDate) = Month(p.day_hour_min_shifted_AllocationDate) 
          AND year(v.day_hour_min_shifted_AllocationDate) = year(p.day_hour_min_shifted_AllocationDate) 
          AND v.PointsAllocationId <= p.PointsAllocationId
    ) `TransactionNumberOfMonthForMember`,
    (
        select 
            count(1)
        from vw_transaction_details v
        where v.DealerId = p.DealerId 
          AND year(v.month_day_hour_min_shifted_AllocationDate) = year(p.month_day_hour_min_shifted_AllocationDate) 
          AND v.PointsAllocationId <= p.PointsAllocationId
    ) `TransactionNumberOfYearForMember`
    , (select 
            count(1) 
        from vw_transaction_details v 
        where v.DealerId = p.DealerId) as `TotalTransactionsOfThatMember`
    , (select 
            count(1) 
        from vw_transaction_details v 
        where v.DealerId = p.DealerId 
          AND DATE(v.hour_min_shifted_AllocationDate) = DATE(p.hour_min_shifted_AllocationDate)
      ) as `TotalTransactionsOfThatMemberOnThatDay` 
    , (select 
            count(1)
        from vw_transaction_details v 
        where v.DealerId = p.DealerId 
         AND  Month(v.day_hour_min_shifted_AllocationDate) = Month(p.day_hour_min_shifted_AllocationDate) 
         AND  year(v.day_hour_min_shifted_AllocationDate) = year(p.day_hour_min_shifted_AllocationDate)) 
         as TotalTransactionsOfThatMemberOnThatMonth
    , (select 
            count(1) 
        from vw_transaction_details v 
        where v.DealerId = p.DealerId 
          AND DATE(v.AllocationDate) between DATE(TIMESTAMPADD(Day, -weekday(p.AllocationDate),p.AllocationDate)) and DATE(TIMESTAMPADD(Day, 6-weekday(p.AllocationDate),p.AllocationDate)) 
      ) as TotalTransactionsOfThatMemberOnThatWeek 
    , (select 
            count(1)
        from vw_transaction_details v 
        where v.DealerId = p.DealerId 
          AND year(v.month_day_hour_min_shifted_AllocationDate) = year(p.month_day_hour_min_shifted_AllocationDate)
      ) as TotalTransactionsOfThatMemberOnThatYear
    , (select if(count(1) < 12, 0, min(x.tCount)) from (
            SELECT COUNT(1) tCount, MONTH(v.month_day_hour_min_shifted_AllocationDate) `month`, v.DealerId, YEAR(v.month_day_hour_min_shifted_AllocationDate) `year`
            FROM vw_transaction_details v
            GROUP BY MONTH(v.month_day_hour_min_shifted_AllocationDate), v.DealerId, YEAR(v.month_day_hour_min_shifted_AllocationDate)
        ) as x
        GROUP BY x.DealerId, x.`year`
        having x.DealerId = p.DealerId AND x.`year` =  YEAR(p.month_day_hour_min_shifted_AllocationDate)
    ) as MinMonthlyTransaction
    ,(select max(x.tCount) from (
            SELECT COUNT(1) tCount, MONTH(v.month_day_hour_min_shifted_AllocationDate) `month`, v.DealerId, YEAR(v.month_day_hour_min_shifted_AllocationDate) `year`
            FROM vw_transaction_details v
            GROUP BY MONTH(v.month_day_hour_min_shifted_AllocationDate), v.DealerId, YEAR(v.month_day_hour_min_shifted_AllocationDate)
        ) as x
        GROUP BY x.DealerId, x.`year`
        having x.DealerId = p.DealerId AND x.`year` =  YEAR(p.month_day_hour_min_shifted_AllocationDate)
    ) as MaxMonthlyTransaction
FROM
    `vw_transaction_details` as `p`
where P.AllocationDate between P_stratDateTime and P_endDateTime
AND P.PointsAllocationId not in (
    select CorrespondingToPointAllocationId from offer_point_allocation where OfferRuleId = P_OfferId
)
Order by P.PointsAllocationId asc
LIMIT 100;

Contrxt Info

此查询用于取出有关点分配的不同参数,这些参数充当根据这些参数执行某些操作的进程的输入。这些参数包括total number of transaction on that day/week/month/year per dealerindex(1 base) of transaction on day/week/month/year per dealer

我已经编辑了我的查询,在select语句中包含了标识列PointsAllocationId。实际上它是原始查询的一部分,但我已经删除了所有非计算列,试图在发布之前使查询变小。

解释输出

| id |     select_type      |         table          | partitions | type |                                                                                                                                   possible_keys                                                                                                                                    |                               key                               | key_len |       ref        | rows | filtered |                    Extra                     |
|----|----------------------|------------------------|------------|------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------|---------|------------------|------|----------|----------------------------------------------|
|  1 | PRIMARY              | p                      | NULL       | ref  | Ix_AllocationType,Ix_AllocationDate,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate                   | IX_allocationType_StatusTypeId                                  | 9       | const,const      |  153 |    45.99 | Using index condition; Using where           |
| 16 | UNCACHEABLE SUBQUERY | offer_point_allocation | NULL       | ref  | CorrespondingToPointAllocationId_idx,Offer_Rule_OfferRuleId_idx                                                                                                                                                                                                                    | Offer_Rule_OfferRuleId_idx                                      | 4       | const            |    1 |   100.00 | Using index                                  |
| 14 | DEPENDENT SUBQUERY   | <derived15>            | NULL       | ALL  | NULL                                                                                                                                                                                                                                                                               | NULL                                                            | NULL    | NULL             |  153 |   100.00 | Using temporary; Using filesort              |
| 15 | DERIVED              | p                      | NULL       | ref  | Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate                                     | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 9       | const,const      |  153 |   100.00 | Using index; Using temporary; Using filesort |
| 12 | DEPENDENT SUBQUERY   | <derived13>            | NULL       | ALL  | NULL                                                                                                                                                                                                                                                                               | NULL                                                            | NULL    | NULL             |  153 |   100.00 | Using temporary; Using filesort              |
| 13 | DERIVED              | p                      | NULL       | ref  | Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate                                     | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 9       | const,const      |  153 |   100.00 | Using index; Using temporary; Using filesort |
| 11 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate         | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 13      | const,const,func |   57 |   100.00 | Using where; Using index                     |
| 10 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate         | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 13      | const,const,func |   57 |   100.00 | Using where; Using index                     |
|  9 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate         | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 13      | const,const,func |   57 |   100.00 | Using where; Using index                     |
|  8 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate         | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 13      | const,const,func |   57 |   100.00 | Using where; Using index                     |
|  7 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate         | IX_allocationType_StatusTypeId_dealerId                         | 13      | const,const,func |   57 |   100.00 | Using where; Using index                     |
|  6 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | PRIMARY,IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 13      | const,const,func |   57 |    33.33 | Using where; Using index                     |
|  5 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | PRIMARY,IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 13      | const,const,func |   57 |    33.33 | Using where; Using index                     |
|  4 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | PRIMARY,IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 13      | const,const,func |   57 |    33.33 | Using where; Using index                     |
|  3 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | PRIMARY,IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | 13      | const,const,func |   57 |    33.33 | Using where; Using index                     |
|  2 | DEPENDENT SUBQUERY   | p                      | NULL       | ref  | PRIMARY,IX_PointAllocation_DealerId,Ix_AllocationType,ix_allocationTypeId,ix_statustypeid,IX_allocationType_StatusTypeId,IX_allocationType_StatusTypeId_dealerId,ix_pointAllocation_DealerId_Status_allocationType,ix_pointAllocation_DealerId_Status_allocationType_allocatioDate | IX_allocationType_StatusTypeId_dealerId                         | 13      | const,const,func |   57 |    33.33 | Using where; Using index                     |

以下是查看

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `root`@`%` 
   SQL SECURITY DEFINER
VIEW `vw_transaction_details` AS
    SELECT 
        `p`.*,
        FN_ADD_OFFER_VW_MIN(`p`.`AllocationDate`) AS `min_shifted_AllocationDate`,
        FN_ADD_OFFER_VW_HOUR(FN_ADD_OFFER_VW_MIN(`p`.`AllocationDate`)) AS `hour_min_shifted_AllocationDate`, 
        FN_ADD_OFFER_VW_DAY(FN_ADD_OFFER_VW_HOUR(FN_ADD_OFFER_VW_MIN(`p`.`AllocationDate`))) AS `day_hour_min_shifted_AllocationDate`,FN_ADD_OFFER_VW_MONTH(FN_ADD_OFFER_VW_DAY(FN_ADD_OFFER_VW_HOUR(FN_ADD_OFFER_VW_MIN(`p`.`AllocationDate`)))) AS `month_day_hour_min_shifted_AllocationDate`
FROM
    `pointsallocation` `p`
WHERE
    ((`p`.`AllocationType` = 2401)
        AND (`p`.`StatusTypeId` = 2501))
ORDER BY `p`.`DealerId`

和函数FN_ADD_OFFER_VW_*是这样的:

CREATE DEFINER=`root`@`%` FUNCTION `FN_ADD_OFFER_VW_DAY`(
 P_time DATETIME
) RETURNS datetime
return date_add(P_time, interval @offer_vw_day day)

1 个答案:

答案 0 :(得分:0)

为了提高性能,我建议避免重复的相关子查询,而是使用条件聚合。

原始查询有点奇怪,从vw_transaction_details返回每一行,但不包括行中的任何标识符或任何其他信息,只包括其他行的计数。

我会做这样的事情:

SELECT v.dealerid

     , COUNT(1) AS `TotalTransactionsOfThatMember`

     , COUNT(IF(
                     v.pointsallocationid                             <= p.pointsallocationid
       ,1,NULL)) AS `TransactionNumberOfMember`

     , COUNT(IF(
                     v.pointsallocationid <= p.pointsallocationid
                 AND DATE(v.hour_min_shifted_allocationdate)           = DATE(p.hour_min_shifted_allocationdate)
       ,1,NULL)) AS `TransactionNumberOfDayForMember`

     , COUNT(IF(
                     v.pointsallocationid                             <= p.pointsallocationid
                 AND DATE(v.allocationdate) >= DATE(p.allocationdate) + INTERVAL 0-WEEKDAY(p.allocationdate) DAY
                 AND DATE(v.allocationdate) <= DATE(p.allocationdate) + INTERVAL 6-WEEKDAY(p.AllocationDate) DAY
       ,1,NULL)) AS `TransactionNumberOfWeekForMember`

     , COUNT(IF(
                     v.pointsallocationid                             <= p.pointsallocationid
                 AND MONTH(v.day_hour_min_shifted_allocationdate)      = MONTH(p.day_hour_min_shifted_allocationdate)
                 AND  YEAR(v.day_hour_min_shifted_allocationdate)      =  YEAR(p.day_hour_min_shifted_allocationdate)
       ,1,NULL)) AS `TransactionNumberOfMonthForMember`

     , COUNT(IF(
                     v.pointsallocationid                             <= p.pointsallocationid
                 AND YEAR(v.day_hour_min_shifted_allocationdate)       = YEAR(p.day_hour_min_shifted_allocationdate)
       ,1,NULL)) AS `TransactionNumberOfYearForMember`

     , COUNT(IF(
                     v.pointsallocationid                             <= p.pointsallocationid
                 AND DATE(v.hour_min_shifted_allocationdate)           = DATE(p.hour_min_shifted_allocationdate)
       ,1,NULL)) AS `TotalTransactionsOfThatMemberOnThatDay`

     , COUNT(IF(
                     MONTH(v.day_hour_min_shifted_allocationdate)      = MONTH(p.day_hour_min_shifted_allocationdate)
                 AND  YEAR(v.day_hour_min_shifted_allocationdate)      =  YEAR(p.day_hour_min_shifted_allocationdate)
       ,1,NULL)) AS `TotalTransactionsOfThatMemberOnThatMonth`

     , COUNT(IF(
                     DATE(v.allocationdate) >= DATE(p.allocationdate) + INTERVAL 0-WEEKDAY(p.allocationdate) DAY
                 AND DATE(v.allocationdate) <= DATE(p.allocationdate) + INTERVAL 6-WEEKDAY(p.allocationdate) DAY
       ,1,NULL)) AS `TotalTransactionsOfThatMemberOnThatWeek`

     , COUNT(IF(
                     YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate)
       ,1,NULL)) AS `TotalTransactionsOfThatMemberOnThatYear`

     , IF(
         COUNT(DISTINCT DATE_FORMAT( v.month_day_hour_min_shifted_allocationdate ,'%Y-%m') < 12
         , 0
         , LEAST (
             COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  1 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  2 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  3 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  4 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  5 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  6 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  7 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  8 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  9 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) = 10 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) = 11 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) = 12 ,1,NULL))
           )
         )
       ) AS `MinMonthlyTransaction`

     , GREATEST(
             COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  1 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  2 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  3 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  4 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  5 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  6 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  7 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  8 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) =  9 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) = 10 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) = 11 ,1,NULL))
           , COUNT(IF( YEAR(v.month_day_hour_min_shifted_allocationdate) = YEAR(p.month_day_hour_min_shifted_allocationdate) AND MONTH(v.month_day_hour_min_shifted_allocationdate) = 12 ,1,NULL))
       ) AS `MaxMonthlyTransaction`

  FROM vw_transaction_details p
  JOIN vw_transaction_details v
    ON v.dealerid = p.dealerid
    -- anti-join
  LEFT
  JOIN offer_point_allocation opa
    ON opa.offerruleid   = P_OfferId
   AND opa.correspondingtopointallocationid = p.pointsallocationid
 WHERE opa.offerruleid IS NULL
   AND p.pointsallocationid IS NOT NULL
   AND p.allocationdate >=      P_stratDateTime
   AND p.allocationdate <=      P_endDateTime
 GROUP
    BY p.uniqueid
     , p.pointsallocationid
     , p.allocationdate
     , p.day_hour_min_shifted_allocation_date
     , p.month_day_hour_min_shifted_allocationdate
     , v.dealerid
 ORDER
    BY p.pointsallocationid ASC